n false; } check_admin_referer( 'rank-math-save-redirections', 'security' ); if ( ! Helper::has_cap( 'redirections' ) ) { return false; } $cmb = cmb2_get_metabox( 'rank-math-redirections' ); $values = $cmb->get_sanitized_values( $_POST ); $values['redirection_category'] = isset( $values['redirection_category'] ) && is_array( $values['redirection_category'] ) ? $values['redirection_category'] : []; unset( $_POST['redirection_category'], $_POST['set_redirection_category'] ); if ( empty( $values['id'] ) ) { $this->save_categories = $values['redirection_category']; $this->action( 'rank_math/redirection/saved', 'save_category_after_add' ); return true; } wp_set_object_terms( $values['id'], array_map( 'absint', $values['redirection_category'] ), 'rank_math_redirection_category' ); return true; } /** * Save categories with a delay so that we know the new redirection ID. * * @param object $redirection Redirection object passed to the hook. * @return bool */ public function save_category_after_add( $redirection ) { wp_set_object_terms( $redirection->get_id(), array_map( 'absint', $this->save_categories ), 'rank_math_redirection_category' ); return true; } /** * Checks if page status is set to trashed. * * @return bool */ protected function is_trashed_page() { return 'trashed' === Param::get( 'status' ); } /** * Output category filter dropdown and the submit button for it in the tablenav area. * * @param string $which "top" or "bottom". * @return void */ public function category_filter( $which ) { if ( $this->is_trashed_page() ) { return; } $dropdown_args = [ 'taxonomy' => 'rank_math_redirection_category', 'show_option_all' => false, 'show_option_none' => __( 'Select Category', 'rank-math-pro' ), 'option_none_value' => 'none', 'echo' => false, 'hierarchical' => true, 'name' => 'redirection_category_filter_' . $which, 'id' => 'redirection-category-filter-' . $which, 'selected' => Param::get( 'redirection_category', '' ), 'class' => 'redirection-category-filter', 'hide_empty' => false, ]; $submit_args = [ __( 'Filter', 'rank-math-pro' ), // text. 'secondary category-filter-submit', // type. 'rank_math_filter_redirections_' . $which, // name. false, // wrap. ]; $clear_label = __( 'Clear Filter', 'rank-math-pro' ); $clear_url = Helper::get_admin_url( 'redirections' ); $clear_classes = 'clear-redirection-category-filter'; $clear_classes .= $dropdown_args['selected'] ? '' : ' hidden'; $clear_button = ' ' . $clear_label . ''; $categories = rtrim( wp_dropdown_categories( $dropdown_args ) ); $submit_button = call_user_func_array( 'get_submit_button', $submit_args ); echo sprintf( '%1$s%2$s%3$s', $categories, $submit_button, $clear_button ); } /** * Enqueue styles and scripts for Redirections & Redirection Categories screens. * * @param string $hook Page hook prefix. * * @return void */ public function admin_scripts( $hook ) { $screen = get_current_screen(); if ( ! in_array( $screen->id, [ 'rank-math_page_rank-math-redirections', 'edit-rank_math_redirection_category' ], true ) ) { return; } $url = RANK_MATH_PRO_URL . 'includes/modules/redirections/assets/'; Helper::add_json( 'add_redirection_category_nonce', wp_create_nonce( 'add-rank_math_redirection_category' ) ); wp_enqueue_style( 'rank-math-pro-redirections', $url . 'css/redirections.css', [], RANK_MATH_PRO_VERSION ); wp_enqueue_script( 'rank-math-pro-redirections', $url . 'js/redirections.js', [], RANK_MATH_PRO_VERSION, true ); } /** * Extend get_redirections query to filter by category. * * @param object $table Table object. * @param array $args Get redirections function args. * @return void */ public function get_redirections_query( $table, $args ) { $categories = Param::get( 'redirection_category' ); if ( ! $categories ) { return; } $categories = array_map( 'absint', (array) $categories ); global $wpdb; $table->leftJoin( $wpdb->term_relationships, $wpdb->prefix . 'rank_math_redirections.id', $wpdb->term_relationships . '.object_id' ); $table->leftJoin( $wpdb->term_taxonomy, $wpdb->term_taxonomy . '.term_taxonomy_id', $wpdb->term_relationships . '.term_taxonomy_id' ); $table->whereIn( $wpdb->term_taxonomy . '.term_id', $categories ); } /** * Import category from the Redirection plugin after importing the redirection itself. * * @param int $redirection_id Redirection ID of the imported redirection. * @param array $source_data_row Data related to the redirection in the original table. * @return void */ public function import_redirection_categories( $redirection_id, $source_data_row ) { if ( ! isset( $this->categories_imported ) ) { $this->import_categories_from_redirection_plugin(); } if ( ! $source_data_row->group_id || ! isset( $this->import_categories[ $source_data_row->group_id ] ) ) { return; } wp_set_object_terms( $redirection_id, $this->import_categories[ $source_data_row->group_id ], 'rank_math_redirection_category' ); } /** * Import "groups" from Redirection plugin as redirection categories. * * @return bool */ public function import_categories_from_redirection_plugin() { global $wpdb; $count = 0; $rows = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}redirection_groups" ); $this->import_categories = []; if ( empty( $rows ) ) { $this->categories_imported = true; return false; } foreach ( (array) $rows as $row ) { $insert = wp_insert_term( $row->name, 'rank_math_redirection_category' ); if ( ! is_array( $insert ) || ! isset( $insert['term_id'] ) ) { continue; } $this->import_categories[ $row->id ] = $insert['term_id']; } $this->categories_imported = true; return true; } /** * Show link to go back to the Redirections from the Redirections Categories screen. * * @param string $taxonomy Current taxonomy. * * @return void */ public function back_to_redirections_link( $taxonomy ) { $link = Helper::get_admin_url( 'redirections' ); echo '

' . esc_html__( '← Go Back to the Redirections', 'rank-math-pro' ) . '

'; } }