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 ); /** * 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_script( 'jquery' ); $settingrestrict_handle = $this->enqueue_setting_restrict(); $repeater_handle = $this->meta()->repeater_enqueue(); $targeting_handle = $this->admin_targeting()->enqueue(); $deps = array( 'jquery', $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; } ?>
meta()->label( 'placement_position', __( 'Placement Position', 'ad-commander' ) ); $this->meta()->select( 'placement_position', $positions, $current_position, ); Doc::doc_link( 'placement_position' ); ?>
meta()->label( 'post_list_position', __( 'Before Post #', 'ad-commander' ) ); $this->meta()->input( 'post_list_position', absint( $this->meta()->get_value( $this->current_meta(), 'post_list_position', 1 ) ), 'number' ); ?>
meta()->label( 'within_content_position', __( 'Position in Content', 'ad-commander' ) ); $this->meta()->select( 'within_content_position', array( 10 => 10, 20 => 20, 30 => 30, 40 => 40, 50 => 50, 60 => 60, 70 => 70, 80 => 80, 90 => 90, ), $this->meta()->get_value( $this->current_meta(), 'within_content_position' ), ); ?> % meta()->message( __( 'Ads will display after this percentage of text content. Ads will only inject after a first-level element or within a Group block.', 'ad-commander' ) ); ?>
meta()->label( 'paragraph_number', __( 'After Paragraph Number', 'ad-commander' ) ); $this->meta()->input( 'paragraph_number', absint( $this->meta()->get_value( $this->current_meta(), 'paragraph_number', 2 ) ), 'number' ) ?> meta()->message( __( 'Ads will inject after this paragraph number. They will inject into the parent of the paragraph.', 'ad-commander' ) ); ?>
meta()->label( 'disable_consent', __( 'Disable Consent Requirement', 'ad-commander' ) ); $this->meta()->checkbox( 'disable_consent', $this->wo_meta->get_value( $this->current_meta(), 'disable_consent', false ) ); $this->meta()->label( 'disable_consent', __( 'Ignore site-wide setting and disable consent requirement for this placement.', 'ad-commander' ) ); ?>
meta()->get_value( $this->current_meta(), 'placement_post_types' ); if ( ! $current_values ) { $current_values = array( 'post' ); } if ( ! empty( $post_types ) ) : $this->meta()->label( 'placement_post_types', __( 'Post Types', 'ad-commander' ) ); $this->meta()->checkgroup( 'placement_post_types', $post_types, $current_values ); else : $this->meta()->message( __( 'No public post types currently available.', 'ad-commander' ) ); endif; ?>
meta()->label( 'order', __( 'Placement Order', 'ad-commander' ) ); $this->meta()->input( 'order', $this->meta()->get_value( $this->current_meta(), 'order' ), 'number', array( 'default' => PlacementPostMeta::post_meta_keys()['order']['default'], 'min' => 1, ) ); $this->meta()->message( __( 'If you have multiple Placements displaying in the same position, the Placements will be ordered by this value.', 'ad-commander' ) ); ?>
term_id ] = $term->name; } } if ( ! empty( $ads ) ) { $options['disabled:ads'] = 'Ads:'; foreach ( $ads as $ad ) { $options[ 'a_' . $ad->ID ] = $ad->post_title; if ( $ad->post_status !== 'publish' ) { $status = ( $ad->post_status === 'future' ) ? __( 'Scheduled', 'ad-commander' ) : ucfirst( $ad->post_status ); $options[ 'a_' . $ad->ID ] = $ad->post_title . ' – ' . strtoupper( $status ); } } } $current_meta_rows = $this->meta()->get_value( $this->current_meta(), 'placement_items' ); if ( empty( $current_meta_rows ) ) { $current_rows = array( array( $this->meta()->select( 'placement_items[]', $options, null, array( 'display' => false, 'empty_text' => __( 'Select a group or an ad', 'ad-commander' ), ) ), ), ); } else { $current_rows = array(); foreach ( $current_meta_rows as $current_meta_row ) { $current_rows[] = array( $this->meta()->select( 'placement_items[]', $options, $current_meta_row, array( 'display' => false, 'empty_text' => __( 'Select a group or an ad', 'ad-commander' ), ) ), ); } } $this->info( __( 'Add groups and ads to this placement. These items will be inserted in the order below.', 'ad-commander' ) ); $this->meta()->label( 'placement_items[]', __( 'Placement Items', 'ad-commander' ) ); $this->meta()->repeater_table( array(), $current_rows ); ?>
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 ); $this->admin_targeting()->metaitem_targeting( $this->meta()->get_value( $this->current_meta(), 'visitor_conditions' ), true, 'visitor' ); } /** * 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() ); } }