wo_meta = new WOMeta( AdCommander::ns() ); $this->nonce = $this->nonce( basename( __FILE__ ), AdCommander::tax_group() ); add_action( AdCommander::tax_group() . '_edit_form_fields', array( $this, 'edit_term_fields' ), 10, 1 ); add_action( AdCommander::tax_group() . '_term_edit_form_top', array( $this, 'before_form_table' ), 10, 1 ); add_action( AdCommander::tax_group() . '_edit_form', array( $this, 'after_form_table' ), 10, 1 ); add_action( 'edited_' . AdCommander::tax_group(), array( $this, 'update_group_meta' ), 10, 1 ); add_action( 'created_' . AdCommander::tax_group(), array( $this, 'update_group_meta' ), 10, 1 ); add_action( 'set_object_terms', array( $this, 'set_object_terms_count' ), 10, 6 ); add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); foreach ( $this->get_action_keys() as $key ) { $key_underscore = self::_key( $key ); add_action( 'wp_ajax_' . $this->action_string( $key ), array( $this, 'action_' . $key_underscore ) ); } // Add info to the new columns add_action( 'manage_' . AdCommander::tax_group() . '_custom_column', array( $this, 'manage_group_column_data' ), 10, 3 ); add_filter( 'manage_edit-' . AdCommander::tax_group() . '_columns', array( $this, 'manage_group_columns' ) ); add_filter( 'quick_edit_enabled_for_taxonomy', array( $this, 'quick_edit_enabled_for_groups' ), 10, 2 ); $this->admin_targeting()->hooks(); } /** * Disable quick edit for groups. * * @param boolean $bool If quick edit is currently enabled. * @param mixed $taxonomy The current taxonomy. * * @return bool */ public function quick_edit_enabled_for_groups( $bool, $taxonomy ) { return ( $taxonomy === AdCommander::tax_group() ) ? false : $bool; } /** * Potentially update the number of ads in a group when a term is saved. * * @param int $object_id The term that was saved. * @param array $terms An array of object term IDs or slugs. * @param array $tt_ids An array of term taxonomy IDs. * @param string $taxonomy Taxonomy slug. * @param bool $append Whether to append new terms to the old terms. * @param array $old_tt_ids Old array of term taxonomy IDs. * * @return void */ public function set_object_terms_count( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) { if ( $taxonomy !== AdCommander::tax_group() ) { return; } /** * Compare tt_ids and $old_ttids and refresh any that are different. */ $this->refresh_group_count_transient( $tt_ids, $old_tt_ids ); } /** * Compare new and old term IDs. Remove the count transient for any terms that were updated. * * @param array $tt_ids An array of term taxonomy IDs. * @param array $old_tt_ids Old array of term taxonomy IDs. * * @return void */ private function refresh_group_count_transient( $tt_ids, $old_tt_ids ) { $term_ids = array_merge( array_diff( $tt_ids, $old_tt_ids ), array_diff( $old_tt_ids, $tt_ids ) ); if ( ! empty( $term_ids ) ) { foreach ( $term_ids as $term_id ) { delete_transient( GroupTermMeta::group_count_transient( $term_id ) ); } } } /** * Enqueue scripts if we're on a screen that needs them. * * @return void */ public function admin_enqueue_scripts() { if ( $this->is_screen( array( 'edit-' . AdCommander::tax_group() ) ) ) { wp_enqueue_script( 'jquery' ); wp_enqueue_script( 'jquery-ui-core' ); wp_enqueue_script( 'jquery-ui-sortable' ); wp_enqueue_script( 'jquery-ui-autocomplete' ); $this->enqueue_jquery_ui_styles(); $targeting_handle = $this->admin_targeting()->enqueue(); wp_register_script( 'worotate', AdCommander::assets_url() . 'js/rotate.js', array(), AdCommander::version(), array( 'in_footer' => true ) ); wp_enqueue_script( 'worotate' ); $handle = Util::ns( 'term-meta' ); wp_register_script( $handle, AdCommander::assets_url() . 'js/term-meta.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-sortable', 'worotate', $targeting_handle ), AdCommander::version(), array( 'in_footer' => true ) ); wp_enqueue_script( $handle ); Util::enqueue_script_data( $handle, array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'terms_url' => admin_url( 'term.php' ), 'tax_name' => AdCommander::tax_group(), 'post_type' => AdCommander::posttype_ad(), 'actions' => $this->get_ajax_actions(), 'save_button_text' => esc_html__( 'Save all settings', 'ad-commander' ), 'sort_handle_url' => self::sortable_handle_url(), ) ); } } /** * Get necessary action keys, which will be used to create wp_ajax hooks. * * @return array */ private function get_action_keys() { return array( 'delete-ad-from-group', 'update-ad-order', 'get-ads-for-search', 'add-ad-to-term', ); } /** * Creates an array of all of the necessary actions. * * @return array */ private function get_ajax_actions() { $actions = array(); foreach ( $this->get_action_keys() as $key ) { $actions[ self::_key( $key ) ] = array( 'action' => $this->action_string( $key ), 'security' => wp_create_nonce( $this->nonce_string( $key ) ), ); } return $actions; } /** * A wp_ajax hook that updates the order of ads in a group. * * @return void */ public function action_update_ad_order() { $action = 'update-ad-order'; check_ajax_referer( $this->nonce_string( $action ), 'security' ); if ( ! current_user_can( AdCommander::capability() ) ) { wp_die(); } if ( ! isset( $_POST['group_id'] ) ) { wp_die(); } $group_id = ( isset( $_POST['group_id'] ) ) ? absint( $_POST['group_id'] ) : null; $ad_order = ( isset( $_POST['ad_order'] ) && ! empty( $_POST['ad_order'] ) ) ? array_map( 'absint', $_POST['ad_order'] ) : array(); if ( update_term_meta( $group_id, $this->wo_meta->make_key( 'ad_order' ), $ad_order ) ) { wp_send_json_success( array( 'action' => $action, 'groupid' => $group_id, ) ); } wp_die(); } /** * A wp_ajax hook that deletes an ad from a group. * * @return void */ public function action_delete_ad_from_group() { $action = 'delete-ad-from-group'; check_ajax_referer( $this->nonce_string( $action ), 'security' ); if ( ! current_user_can( AdCommander::capability() ) ) { wp_die(); } if ( ! isset( $_POST['group_id'] ) || ! isset( $_POST['ad_id'] ) ) { wp_die(); } $group_id = ( isset( $_POST['group_id'] ) ) ? absint( $_POST['group_id'] ) : null; $ad_id = ( isset( $_POST['ad_id'] ) ) ? absint( $_POST['ad_id'] ) : null; if ( wp_remove_object_terms( $ad_id, $group_id, AdCommander::tax_group() ) ) { delete_transient( GroupTermMeta::group_count_transient( $group_id ) ); wp_send_json_success( array( 'action' => $action, 'groupid' => $group_id, 'adid' => $ad_id, ) ); } wp_die(); } /** * The name of the transient that stores our filtered ads. * * @return string */ public static function group_search_ads_transient() { return Util::ns( 'group_search_ads', '_' ); } /** * Store the available filter ads in a transient so we don't have to query them every time. * * @return array */ private function get_ads_for_search() { $ads = get_transient( self::group_search_ads_transient() ); if ( $ads === false ) { $available_ads = Query::ads( 'post_title', 'asc', Util::any_post_status( 'trash' ), array(), array() ); $ads = array(); if ( ! empty( $available_ads ) ) { foreach ( $available_ads as $ad ) { $ads[] = array( 'id' => $ad->ID, 'title' => $ad->post_title, 'status' => ( $ad->post_status !== 'publish' ) ? wp_strip_all_tags( Util::post_status_label( $ad->post_status ) ) : 'publish', ); } } set_transient( self::group_search_ads_transient(), $ads, 20 ); } return $ads; } /** * Wp_ajax hook. Gets all of the ads for use in the filter. * * @return void */ public function action_get_ads_for_search() { $action = 'get-ads-for-search'; check_ajax_referer( $this->nonce_string( $action ), 'security' ); if ( ! current_user_can( AdCommander::capability() ) ) { wp_die(); } wp_send_json_success( array( 'action' => $action, 'ads' => $this->get_ads_for_search(), ) ); wp_die(); } /** * Wp_ajax hook. Gets all of the ads for use in the filter. * * @return void */ public function action_add_ad_to_term() { $action = 'add-ad-to-term'; check_ajax_referer( $this->nonce_string( $action ), 'security' ); if ( ! current_user_can( AdCommander::capability() ) ) { wp_die(); } if ( ! isset( $_POST['group_id'] ) || ! isset( $_POST['ad_id'] ) ) { wp_die(); } $group_id = ( isset( $_POST['group_id'] ) ) ? absint( $_POST['group_id'] ) : null; $ad_id = ( isset( $_POST['ad_id'] ) ) ? absint( $_POST['ad_id'] ) : null; if ( wp_add_object_terms( $ad_id, $group_id, AdCommander::tax_group() ) ) { delete_transient( GroupTermMeta::group_count_transient( $group_id ) ); wp_send_json_success( array( 'action' => $action, 'groupid' => $group_id, 'adid' => $ad_id, ) ); } wp_die(); } /** * Ad fields before the form table while viewing a Group term. * * @param object $term The current term. * * @return void */ public function before_form_table( $term ) { $this->nonce_field( $this->nonce ); $this->back_to_groups(); ?>
group_ad_list( $term ); $this->group_preview( $term ); } /** * Back to groups link at top of term page. */ private function back_to_groups() { ?> term_id, $this->wo_meta->make_key( 'ad_order' ), true ); $ad_weights = get_term_meta( $term->term_id, $this->wo_meta->make_key( 'ad_weights' ), true ); if ( ! $sorted_ids || ! is_array( $sorted_ids ) ) { $sorted_ids = array(); } $ads = Group::sort_ads_by_sorted_ids( Query::ads_by_group( $term->term_id, Util::any_post_status() ), $sorted_ids ); ?> term_id ) ) : ?>