/** * Generates the targeting for the current content being displayed * * @since 1.0 * * @return array */ public static function get_targeting() { if (is_array(self::$targeting)) { return; } // Init targeting array self::$targeting = array(); // Include WP_DFP_Settings class WP_DFP::inc('wp-dfp-settings.php'); // Helper function for converting all array values to strings $to_string = create_function('$v', 'return is_array( $v ) ? $v : (string) $v;'); // Define targeting rules for home/front page if (is_front_page() || is_home()) { self::$targeting['post_name'] = 'home'; self::$targeting['post_title'] = 'Home'; self::$targeting['post_id'] = 0; self::$targeting['post_type'] = ''; } elseif (is_singular()) { global $post; $tag_terms = get_the_terms($post->ID, 'post_tag'); if (is_array($tag_terms)) { self::$targeting['tag'] = wp_list_pluck($tag_terms, 'slug'); } $category_terms = get_the_terms($post->ID, 'category'); if (is_array($category_terms)) { self::$targeting['category'] = array_map($to_string, wp_list_pluck($category_terms, 'slug')); } self::$targeting['post_name'] = $post->post_name; self::$targeting['post_title'] = $post->post_title; self::$targeting['post_id'] = $post->ID; self::$targeting['post_type'] = $post->post_type; } elseif (is_category()) { self::$targeting['taxonomy'] = 'category'; self::$targeting['term'] = get_queried_object()->slug; } elseif (is_tag()) { self::$targeting['taxonomy'] = 'tag'; self::$targeting['term'] = get_queried_object()->slug; } elseif (is_tax()) { self::$taxonomy = get_queried_object()->slug; self::$targeting['taxonomy'] = $taxonomy; self::$targeting['term'] = get_query_var($taxonomy); } elseif (is_search()) { self::$targeting['search_term'] = get_query_var('s'); } /** * Filter the targeting arguments for the current content being displayed * * @since 1.0 * * @param array $targeting An array of targeting arguments. */ self::$targeting = apply_filters('wp_dfp_targeting', self::$targeting); // Define slots $all_slots = WP_DFP::get_slots(); $slots = array(); foreach ($all_slots as $slot) { $slot = wp_dfp_ad_slot($slot); $slots[$slot->slot()] = $slot->sizes(); unset($slot); // prevent possible memory spike } // Get network code $network_code = WP_DFP_Settings::get('network_code'); // Set message defaults $messages = array('noNetworkCode' => null); /* If current user can manage options and a network code is not defined then display an message in each in-page ad slot */ if (empty($network_code) && current_user_can('manage_options')) { $messages['noNetworkCode'] = '<div class="wp-dfp-ad-unit-error">' . sprintf(__('WP_DFP: You must supply your DFP network code in the <a target="_blank" href="%s">WP DFP settings</a> screen.', 'wp-dfp'), wp_dfp_settings_url()) . '</div>'; } wp_enqueue_script('wp-dfp'); wp_localize_script('wp-dfp', 'wpdfp', array('network' => $network_code, 'targeting' => array_map($to_string, self::$targeting), 'slots' => $slots, 'messages' => $messages)); }
/** * Renders a meta box * * @since 1.0 * @uses $wpdb * * @param WP_Post $post The current post object. * @param array $args Any additional arguments as passed by add_meta_box() */ public static function render_meta_box($post, $args = array()) { global $wpdb; $meta_box = array_shift($args); $values = array(); $form = array(); switch ($meta_box) { case 'wp-dfp-slot-config': $ad_slot = wp_dfp_ad_slot($post); $values['wp_dfp_slot_name'] = $post->post_name; $values[WP_DFP::META_OOP] = (int) $ad_slot->meta(WP_DFP::META_OOP); $form = array('wp_dfp_slot_name' => array('#label' => __('Slot Name', 'wp-dfp'), '#type' => 'text', '#description' => __('Enter the name of the slot as found in DFP. Do not include your network ID!', 'wp-dfp')), WP_DFP::META_OOP => array('#label' => __('This is an out-of-page slot.', 'wp-dfp'), '#type' => 'checkbox', '#checked' => 1, '#conditional' => array('element' => '#wp-dfp-sizing-rules', 'action' => 'hide', 'value' => 1))); break; case 'wp-dfp-sizing-rules': $ad_slot = wp_dfp_ad_slot($post); $rules = $ad_slot->meta(WP_DFP::META_SIZING_RULES, array()); $index = 0; foreach ($rules as $container_width => $sizes) { $values[WP_DFP::META_SIZING_RULES][$index]['container_width'] = $container_width; $size_parts = array(); foreach ($sizes as $size) { $size_parts[] = join('x', $size); } $values[WP_DFP::META_SIZING_RULES][$index]['sizes'] = join("\n", $size_parts); $index++; } $form = array('sizing_rules' => array('#label' => '', '#description' => __('Click "Add Rule" below to specify a new sizing rule.', 'wp-dfp'), WP_DFP::META_SIZING_RULES => array('#type' => 'multiple', '#add_link' => __('Add Rule', 'wp-dfp'), '#multiple' => array('container_width' => array('#type' => 'text', '#label' => __('Container Width', 'wp-dfp'), '#description' => __('If the calculated ad container width is >= than this value and is less than the next largest specified ad container width, then the ad sizes defined below will be used for this ad slot.', 'wp-dfp')), 'sizes' => array('#type' => 'textarea', '#label' => __('Ad Sizes', 'wp-dfp'), '#attrs' => array('rows' => 5), '#description' => __('One ad size per line. Example: 950x100 where 950 is the width of the ad and 100 is the height of the ad.', 'wp-dfp')))))); break; case 'submitdiv': $delete_markup = ''; if (current_user_can("delete_post", $post->ID)) { $delete_text = !EMPTY_TRASH_DAYS ? __('Delete Permanently') : __('Move to Trash'); $delete_markup = '<div id="delete-action"><a class="submitdelete deletion" href="' . get_delete_post_link($post->ID) . '">' . $delete_text . '</a></div>'; } $markup = ' <div id="minor-publishing"> <div id="misc-publishing-actions"> <div class="misc-pub-section"> <span class="dashicons dashicons-welcome-add-page"></span><a href="' . self::clone_url($post) . '">' . __('Clone this slot', 'wp-dfp') . '</a> </div> </div> </div> <div id="major-publishing-actions">' . $delete_markup . ' <div id="publishing-action"> <span class="spinner"></span>' . get_submit_button(__('Save Ad Slot', 'wp-dfp'), 'primary button-large', 'publish', false) . ' </div> <div class="clear"></div> </div>'; echo $markup; break; } echo WP_Forms_API::render_form($form, $values); }