' . 'affiliates_robot_cleaner_ajax_nonce = \'' . wp_create_nonce( 'affiliates-robot-cleaner-ajax-nonce' ) . '\';' . ''; } /** * Ajax wp_ajax_affiliates_robot_cleaner_clean cleaner handler. */ public static function clean() { global $wpdb; $result = false; if ( !current_user_can( AFFILIATES_ADMINISTER_AFFILIATES ) ) { exit; } if ( check_ajax_referer( 'affiliates-robot-cleaner-ajax-nonce', 'affiliates_robot_cleaner_ajax_nonce' ) ) { set_time_limit( 0 ); // remove all hits that are related to robots and don't have a referral related $hits_table = _affiliates_get_tablename( 'hits' ); $referrals_table = _affiliates_get_tablename( 'referrals' ); $robots_table = _affiliates_get_tablename( 'robots' ); $user_agents_table = _affiliates_get_tablename( 'user_agents' ); $query = "DELETE FROM $hits_table WHERE hit_id IN " . "( SELECT DISTINCT hits.hit_id FROM " . "( " . "SELECT h.hit_id " . "FROM $hits_table h " . "LEFT JOIN ( " . "SELECT a.user_agent_id FROM $user_agents_table a " . "LEFT JOIN $robots_table r ON a.user_agent LIKE CONCAT( '%%', r.name, '%%' ) " . "WHERE r.robot_id IS NOT NULL " . ") AS rua ON h.user_agent_id = rua.user_agent_id " . "LEFT JOIN ( " . "SELECT COUNT(*) count, hit_id FROM $referrals_table WHERE hit_id IS NOT NULL GROUP BY hit_id " . ") AS r ON r.hit_id = h.hit_id " . "WHERE " . "rua.user_agent_id IS NOT NULL " . "AND r.count IS NULL OR r.count = 0 " . ") AS hits " . ")"; ob_start(); $rows = 0; $result = $wpdb->query( $query ); if ( $result ) { $rows = $wpdb->get_var( "SELECT ROW_COUNT()" ); } $ob = ob_get_clean(); affiliates_log_warning( $ob ); echo json_encode( $rows ); } exit; } public static function admin() { global $wpdb, $affiliates_version; if ( !current_user_can( AFFILIATES_ADMINISTER_AFFILIATES ) ) { wp_die( __( 'Access denied.', 'affiliates' ) ); } add_action( 'admin_footer', array( __CLASS__, 'admin_footer' ) ); wp_enqueue_script( 'affiliates-robot-cleaner', AFFILIATES_PLUGIN_URL . 'js/affiliates-robot-cleaner.js', array( 'jquery' ), $affiliates_version, true ); wp_localize_script( 'affiliates-robot-cleaner', 'affiliates_robot_cleaner', array( 'rows_deleted' => _x( 'Hits deleted:', 'robot cleaner', 'affiliates' ), 'failed' => _x( 'Could not delete any hits', 'robot cleaner', 'affiliates' ) ) ); $action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : null; if ( $action !== null && ( !isset( $_REQUEST['robot-cleaner-nonce'] ) || !wp_verify_nonce( $_REQUEST['robot-cleaner-nonce'], 'robot-cleaner-action' ) ) ) { wp_die( __( 'Access denied.', 'affiliates' ) ); } echo '
%s'; esc_html_e( 'Check for hits from robots that can be removed …', 'affiliates' ); echo '
'; echo ''; echo ''; } else { echo '
' . esc_html__( 'There are no robots defined.', 'affiliates' ) . ' ' . esc_html__( 'There must be at least one entry to proceed to clean up existing hits from robots.', 'affiliates' ) . '
'; } break; case 'confirm' : // show the current eligible count of hits per user agent identified as robots $hits_table = _affiliates_get_tablename( 'hits' ); $referrals_table = _affiliates_get_tablename( 'referrals' ); $robots_table = _affiliates_get_tablename( 'robots' ); $user_agents_table = _affiliates_get_tablename( 'user_agents' ); $query = "SELECT COUNT(*) AS count, rua.user_agent AS user_agent " . "FROM $hits_table h " . "LEFT JOIN ( " . "SELECT a.user_agent_id, a.user_agent FROM $user_agents_table a " . "LEFT JOIN $robots_table r ON a.user_agent LIKE CONCAT( '%%', r.name, '%%' ) " . "WHERE r.robot_id IS NOT NULL " . ") AS rua ON h.user_agent_id = rua.user_agent_id " . "LEFT JOIN ( " . "SELECT COUNT(*) count, hit_id FROM $referrals_table WHERE hit_id IS NOT NULL GROUP BY hit_id " . ") AS r ON r.hit_id = h.hit_id " . "WHERE " . "rua.user_agent_id IS NOT NULL " . "AND r.count IS NULL OR r.count = 0 " . "GROUP BY h.user_agent_id"; $robot_hits = $wpdb->get_results( $query ); if ( is_array( $robot_hits ) && count( $robot_hits ) > 0 ) { echo '' . esc_html__( 'The following matching hits have been found and can be cleaned up.', 'affiliates' ) . '
'; echo '| ' . esc_html__( 'Hits', 'affiliates' ) . ' | '; echo '' . esc_html__( 'User Agent', 'affiliates' ) . ' | '; echo '
|---|---|
| '; echo $robot_hit->count; echo ' | '; echo ''; echo esc_html( $robot_hit->user_agent ); echo ' | '; echo '
| '; printf( '%d', $count ); echo ' | '; echo ''; echo esc_html__( 'Total', 'affiliates' ); echo ' | '; echo '
'; esc_html_e( 'If you want to delete these matching hits, click the button to proceed.', 'affiliates' ); echo ' '; esc_html_e( 'Once you click the button, the hits will be deleted immediately.', 'affiliates' ); echo ' '; esc_html_e( 'It can take a while to clean up a large number of hits.', 'affiliates' ); echo ' '; esc_html_e( 'This action cannot be undone.', 'affiliates' ); echo '
'; echo ''; echo '
'; echo ''; echo ''; } else { echo '' . esc_html__( 'No matching hits have been found.', 'affiliates' ) . '
'; } break; } } } Affiliates_Robot_Cleaner::init();