nonce = $this->nonce( basename( __FILE__ ), AdCommander::posttype_placement() ); add_action( 'edit_form_after_title', array( $this, 'posttype_meta_boxes' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); add_action( 'save_post_' . AdCommander::posttype_placement(), array( $this, 'save_posted_metadata' ), 10, 2 ); /** * Placement post type edit.php */ add_action( 'manage_' . AdCommander::posttype_placement() . '_posts_custom_column', array( $this, 'manage_column_data' ), 10, 3 ); add_filter( 'manage_edit-' . AdCommander::posttype_placement() . '_columns', array( $this, 'manage_columns' ) ); add_filter( 'manage_edit-' . AdCommander::posttype_placement() . '_sortable_columns', array( $this, 'manage_sortable_columns' ) ); add_action( 'pre_get_posts', array( $this, 'placement_sort_pre_get_posts' ) ); add_action( 'restrict_manage_posts', array( $this, 'placement_position_filter' ) ); add_filter( 'parse_query', array( $this, 'filter_placements_by_position' ) ); /** * Targeting */ $this->admin_targeting()->hooks(); } /** * Create a new WOMeta instance if necessary. * * @return WOMeta */ public function meta() { if ( ! $this->wo_meta ) { $this->wo_meta = new WOMeta( AdCommander::ns() ); } return $this->wo_meta; } /** * Sets the current meta if it is not yet set. * * @return mixed */ private function current_meta() { global $post; if ( ! $this->current_meta && isset( $post->ID ) ) { $this->current_meta = $this->meta()->get_post_meta( $post->ID, PlacementPostMeta::post_meta_keys() ); } return $this->current_meta; } /** * Enqueue scripts if we're on a screen that needs them. * * @return void */ public function admin_enqueue_scripts() { if ( $this->is_screen( array( AdCommander::posttype_placement() ) ) ) { wp_enqueue_style( 'wp-color-picker' ); wp_enqueue_script( 'jquery' ); wp_enqueue_script( 'wp-color-picker' ); wp_register_script( 'wp-color-picker-alpha', AdCommander::assets_url() . 'vendor/wp-color-picker-alpha.js', array( 'wp-color-picker' ), AdCommander::version(), array( 'in_footer' => true ) ); wp_enqueue_script( 'wp-color-picker-alpha' ); $settingrestrict_handle = $this->enqueue_setting_restrict(); $repeater_handle = $this->meta()->repeater_enqueue(); $targeting_handle = $this->admin_targeting()->enqueue(); $deps = array( 'jquery', 'wp-color-picker-alpha', $settingrestrict_handle, $repeater_handle, $targeting_handle, ); $handle = Util::ns( 'placement' ); wp_register_script( $handle, AdCommander::assets_url() . 'js/placement-post.js', $deps, AdCommander::version(), array( 'in_footer' => true ) ); wp_enqueue_script( $handle ); } } /** * Hook function for creating/editing meta boxes for our post type. * * @return void */ public function posttype_meta_boxes() { $current_screen = get_current_screen(); if ( isset( $current_screen->id ) && $current_screen->id === AdCommander::posttype_placement() ) { /** * Insert our nonce field */ $this->nonce_field( $this->nonce ); /** * General */ add_meta_box( 'adcmdrsettingsdiv', __( 'Placement Display Settings', 'ad-commander' ), array( $this, 'adcmdrsettings_meta_box' ), AdCommander::posttype_placement(), 'normal', 'high' ); add_meta_box( 'adcmdritemsdiv', __( 'Placement Groups & Ads', 'ad-commander' ), array( $this, 'adcmdritems_meta_box' ), AdCommander::posttype_placement(), 'normal', 'high' ); add_meta_box( 'adcmdrorderdiv', __( 'Placement Settings', 'ad-commander' ), array( $this, 'adcmdrorderdiv_meta_box' ), AdCommander::posttype_placement(), 'side' ); /** * Targeting */ $targeting_title = __( 'Targeting', 'ad-commander' ); if ( ! ProBridge::instance()->is_pro_loaded() ) { $targeting_title .= ProBridge::pro_label(); } add_meta_box( 'adcmdrtargetingdiv', $targeting_title, array( $this, 'adcmdrtargeting_meta_box' ), AdCommander::posttype_placement(), 'normal', 'high' ); } } /** * Add fields to the Settings meta box. * * @return void */ public function adcmdrsettings_meta_box() { $this->metaitem_placement_position(); // $this->metaitem_placement_post_types(); } /** * Add fields to the Items meta box. * * @return void */ public function adcmdritems_meta_box() { $this->metaitem_placement_items(); } /** * Add fields to the Order meta box. * * @return void */ public function adcmdrorderdiv_meta_box() { $this->metaitem_placement_order(); } /** * Add fields to the Targeting meta box. * * @return void */ public function adcmdrtargeting_meta_box() { $this->metaitem_content_targeting(); } /** * Placement position meta fields. * * TODO: Improvements to placement admin; Only show 'Post Types' when applicable, for example. */ private function metaitem_placement_position() { $positions = PlacementPostMeta::placement_positions(); $current_position = $this->meta()->get_value( $this->current_meta(), 'placement_position' ); if ( ! ProBridge::instance()->is_pro_loaded() ) { $nonpro_positions = array(); foreach ( $positions as $key => $position ) { if ( in_array( $key, ProBridge::instance()->pro_placement_positions() ) ) { $key = 'disabled:' . $key; $position .= ProBridge::pro_label(); if ( $current_position === $key ) { $current_position = null; } } $nonpro_positions[ $key ] = $position; } $positions = $nonpro_positions; } ?>
is_pro_loaded() ? '' : ProBridge::instance()->pro_label(); if ( $pro_message ) { $pro_message = '' . $pro_message . ''; } $this->info( $info_message . $pro_message ); $this->admin_targeting()->metaitem_targeting( $this->meta()->get_value( $this->current_meta(), 'content_conditions' ), true, 'content', AdCommander::posttype_placement() ); $this->admin_targeting()->metaitem_targeting( $this->meta()->get_value( $this->current_meta(), 'visitor_conditions' ), true, 'visitor', AdCommander::posttype_placement() ); } /** * Save posted meta data. Interfaces with WOMeta. * * @param int $post_id The Post ID that has saved. * @param object $post The WP_Post that has saved. * * @return void */ public function save_posted_metadata( $post_id, $post ) { $this->meta()->save_posted_metadata( $post, PlacementPostMeta::post_meta_keys(), $this->nonce, AdCommander::capability() ); delete_transient( Placement::popups_should_enqueue_transient_name() ); } /** * Output content within an individual column on the Placement post page. * * @param string $column Current column. * @param int $post_id The post ID. * * @return void */ public function manage_column_data( $column, $post_id ) { switch ( $column ) { case 'position': $position = get_post_meta( $post_id, $this->meta()->make_key( 'placement_position' ), true ); if ( $position ) { echo esc_html( PlacementPostMeta::placement_positions()[ $position ] ); } break; case 'items': $items = maybe_unserialize( get_post_meta( $post_id, $this->meta()->make_key( 'placement_items' ), true ) ); echo is_array( $items ) ? count( $items ) : 0; break; case 'placement_order': $order = get_post_meta( $post_id, $this->meta()->make_key( 'order' ), true ); echo ( $order ) ? absint( $order ) : 1; break; } } /** * Update columns on the placement post page. * * @param array $columns The current columns. * * @return array */ public function manage_columns( $columns ) { /** * Change existing columns */ unset( $columns['author'] ); /** * Sort columns and add new */ $new_columns = array(); foreach ( $columns as $key => $label ) { $new_columns[ $key ] = $label; if ( $key === 'title' ) { $new_columns['position'] = __( 'Position', 'ad-commander' ); $new_columns['items'] = __( 'Items', 'ad-commander' ); $new_columns['placement_order'] = __( 'Placement Order', 'ad-commander' ); } } return $new_columns; } /** * Update sortable columns on the placement page. * * @param array $columns The current columns. * * @return array */ public function manage_sortable_columns( $columns ) { $columns['position'] = 'position'; return $columns; } /** * Create an ad type filter on ad table. * * @param string $post_type The current post type. * * @return void */ public function placement_position_filter( $post_type ) { if ( AdCommander::posttype_placement() !== $post_type ) { return; } if ( ! current_user_can( AdCommander::capability() ) ) { return; } $positions = PlacementPostMeta::placement_positions(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nonce not needed in this case $selected = isset( $_GET['adcmdr_placement_position'] ) ? sanitize_text_field( wp_unslash( $_GET['adcmdr_placement_position'] ) ) : ''; if ( $positions ) { ?> get( 'orderby' ); if ( $filtered_position !== '' && $orderby === 'position' ) { return; } /** * Order our query. */ if ( 'position' == $orderby ) { $position_key = $this->meta()->make_key( 'placement_position' ); $meta_query = array( 'relation' => 'OR', array( 'key' => $position_key, 'compare' => 'NOT EXISTS', ), array( 'key' => $position_key, ), ); $query->set( 'meta_query', $meta_query ); $query->set( 'orderby', 'meta_value' ); } } /** * Filter the admin query to add ad type meta_query. * * @param mixed $query The current query. * * @return void */ public function filter_placements_by_position( $query ) { global $pagenow; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nonce not needed in this case if ( ! is_admin() || $pagenow !== 'edit.php' || ! isset( $_GET['post_type'] ) || AdCommander::posttype_placement() !== sanitize_text_field( wp_unslash( $_GET['post_type'] ) ) ) { return; } if ( ! current_user_can( AdCommander::capability() ) ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nonce not needed in this case $position = isset( $_GET['adcmdr_placement_position'] ) ? sanitize_text_field( wp_unslash( $_GET['adcmdr_placement_position'] ) ) : ''; if ( $position !== '' ) { $query->query_vars['meta_query'][] = array( 'key' => $this->meta()->make_key( 'placement_position' ), 'value' => $position, 'compare' => '=', ); } } }