/** * Gets an instance of the WP_DFP_Ad_Slot class * * @since 1.0 * * @param string|WP_Post $slot The name of the ad slot or a WP_Post object. * * @return WP_DFP_Ad_Slot */ function wp_dfp_ad_slot($slot) { if (!class_exists('WP_DFP_Ad_Slot')) { WP_DFP::inc('class-wp-dfp-ad-slot.php'); } try { return new WP_DFP_Ad_Slot($slot); } catch (InvalidArgumentException $e) { return new WP_Error(); } }
/** * Gets the full path to the ad slot * * @since 1.0.1 * * @return string */ public function path() { if (!$this->post instanceof WP_Post) { return ''; } WP_DFP::inc('wp-dfp-settings.php'); return ltrim(WP_DFP_Settings::get('network_code', '') . '/' . $this->slot(), '/'); }
/** * 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)); }
/** * Set the wp_forms_api base URL * * @since 1.0 * @filter wp_form_base_url */ public static function wp_form_base_url($url) { return WP_DFP::url('externals/wp-forms-api'); }
/** * Gets all of the available slots * * @since 1.0 * * @return array */ public static function get_slots() { if (is_array(self::$slots)) { return self::$slots; } self::$slots = get_posts(array('post_type' => self::POST_TYPE, 'posts_per_page' => -1, 'post_status' => 'publish')); return self::$slots; }