hooks(); } private function hooks() { add_action( 'wpml_troubleshooting_after_setup_complete_cleanup_end', array( $this, 'troubleshooting_action' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); add_action( 'wp_ajax_wpml_count_orphans', array( $this, 'count_orphans_action' ) ); add_action( 'wp_ajax_wpml_delete_orphans', array( $this, 'delete_orphans_action' ) ); } public function count_orphans_action() { if ( wpml_is_action_authenticated( 'wpml_orphan_comment' ) ) { wp_send_json_success( $this->get_orphan_comments( true ) ); } else { wp_send_json_error( 'Wrong Nonce' ); } } public function get_orphan_comments( $return_count = false, $limit = 10 ) { if ( $return_count ) { $columns = 'count(c.comment_id)'; } else { $columns = 'DISTINCT c.comment_id'; } $sql = " SELECT {$columns} FROM {$this->wpdb->prefix}comments c INNER JOIN {$this->wpdb->prefix}icl_translations tc ON c.comment_id = tc.element_id INNER JOIN {$this->wpdb->posts} p ON c.comment_post_ID = p.ID INNER JOIN {$this->wpdb->prefix}icl_translations tp ON p.ID = tp.element_id AND CONCAT('post_', p.post_type) = tp.element_type WHERE tc.element_type = 'comment' AND tp.language_code <> tc.language_code LIMIT 0, %d "; $sql_prepared = $this->wpdb->prepare( $sql, $limit ); if ( $return_count ) { $results = $this->wpdb->get_var( $sql_prepared ); } else { $comments = $this->wpdb->get_col( $sql_prepared ); $num_rows = $this->wpdb->num_rows; $results = array( $comments, $num_rows ); } return $results; } public function delete_orphans_action() { if ( wpml_is_action_authenticated( 'wpml_orphan_comment' ) ) { $result = false; $data = isset( $_POST['data'] ) ? $_POST['data'] : array(); $how_many = null; if ( isset( $data['how_many'] ) && is_numeric( $data['how_many'] ) ) { $how_many = (int) $data['how_many']; } if ( $how_many ) { $result = $this->delete_orphans( $how_many ); } wp_send_json_success( $result ); } else { wp_send_json_error( 'Wrong Nonce' ); } } /** * @param $hook */ public function enqueue_scripts( $hook ) { wp_register_script( 'wpml-orphan-comments', ICL_PLUGIN_URL . '/res/js/orphan-comments.js', array( 'jquery' ), ICL_SITEPRESS_VERSION, true ); if ( WPML_PLUGIN_FOLDER . '/menu/troubleshooting.php' === $hook ) { wp_enqueue_script( 'wpml-orphan-comments' ); } } public function troubleshooting_action() { ?>