PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 1.7.12
Advanced Ads – Ad Manager & AdSense v1.7.12
2.0.23 2.0.22 2.0.21 1.38.0 1.39.0 1.39.1 1.39.2 1.39.3 1.39.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.40.0 1.40.1 1.40.2 1.41.0 1.42.0 1.42.1 1.42.2 1.43.0 1.44.0 1.44.1 1.45.0 1.45.1 1.46.0 1.47.0 1.47.1 1.47.2 1.47.3 1.47.4 1.47.5 1.48.0 1.48.1 1.49.0 1.5.0 1.5.0.1 1.5.1 1.5.2 1.5.2.1 1.5.4 1.5.4.1 1.5.5 1.50.0 1.51.0 1.51.1 1.51.2 1.51.3 1.52.0 1.52.1 1.52.2 1.52.3 1.52.4 1.53.0 1.53.1 1.53.2 1.54.0 1.54.1 1.55.0 1.56.0 1.56.1 1.56.2 1.56.3 1.56.4 1.6 1.6.1 1.6.10 1.6.10.1 1.6.10.2 1.6.11 1.6.11.1 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.17.1 1.6.17.2 1.6.2 1.6.2.1 1.6.3 1.6.4 1.6.4.1 1.6.5 1.6.6 1.6.6.1 1.6.7 1.6.7.1 1.6.8 1.6.8.1 1.6.8.2 1.6.8.3 1.6.9 1.6.9.1 1.6.9.2 1.6.9.3 1.6.9.4 1.7 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.1.1 1.7.1.2 1.7.1.3 1.7.1.4 1.7.1.5 1.7.10 trunk 1.7.11 1.0.1 1.7.12 1.0.2 1.7.13 1.0.3 1.7.14 1.1.0 1.7.15 1.1.1 1.7.16 1.1.2 1.7.17 1.1.3 1.7.18 1.10 1.7.19 1.10.1 1.7.2 1.10.10 1.7.2.1 1.10.11 1.7.20 1.10.12 1.7.21 1.10.2 1.7.22 1.10.3 1.7.23 1.10.4 1.7.24 1.10.5 1.7.25 1.10.6 1.7.3 1.10.7 1.7.4 1.10.8 1.7.4.1 1.10.9 1.7.4.2 1.11 1.7.4.3 1.11.1 1.7.4.4 1.11.2 1.7.4.5 1.12 1.7.5 1.13 1.7.5.1 1.13.1 1.7.6 1.13.2 1.7.7 1.13.3 1.7.8 1.13.4 1.7.9 1.13.5 1.7.9.1 1.13.6 1.7.9.2 1.13.7 1.7.9.3 1.13.8 1.8 1.14 1.8.1 1.14.1 1.8.10 1.14.10 1.8.11 1.14.11 1.8.12 1.14.2 1.8.13 1.14.3 1.8.14 1.14.4 1.8.15 1.14.5 1.8.16 1.14.6 1.8.17 1.14.7 1.8.18 1.14.8 1.8.19 1.14.9 1.8.2 1.15 1.8.20 1.16 1.8.21 1.16.1 1.8.22 1.17 1.8.23 1.17.1 1.8.24 1.17.10 1.8.25 1.17.10-rc.1 1.8.26 1.17.11 1.8.27 1.17.12 1.8.28 1.17.12-rc.1 1.8.29 1.17.2 1.8.3 1.17.3 1.8.30 1.17.4 1.8.4 1.17.5 1.8.5 1.17.6 1.8.6 1.17.7 1.8.7 1.17.8 1.8.8 1.17.9 1.8.9 1.17.9-beta.1 1.9 1.18.0 2.0.0 1.19.0 2.0.1 1.19.1 2.0.10 1.2 2.0.11 1.2.1 2.0.12 1.2.2 2.0.13 1.2.3 2.0.14 1.2.4 2.0.15 1.2.5 2.0.16 1.2.6 2.0.17 1.2.7 2.0.18 1.20.0 2.0.19 1.20.0-rc.1 2.0.2 1.20.0-rc.2 2.0.20 1.20.1 2.0.3 1.20.2 2.0.4 1.20.3 2.0.5 1.21.0 2.0.6 1.21.1 2.0.7 1.22.0 2.0.8 1.22.1 2.0.9 1.22.2 1.23.0 1.23.1 1.23.2 1.24.0 1.24.1 1.24.2 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.29.0 1.29.1 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 1.3.18 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.30.0 1.30.1 1.30.2 1.30.2-rc.1 1.30.3 1.30.4 1.30.4-rc.1 1.30.5 1.31.0 1.31.1 1.32.0 1.32.0-rc.1 1.33.0 1.33.1 1.33.2 1.34.0 1.35.0 1.35.1 1.36.0 1.36.1 1.36.2 1.36.3 1.37.0 1.37.1 1.37.2
advanced-ads / public / class-advanced-ads.php
advanced-ads / public Last commit date
assets 9 years ago includes 12 years ago views 9 years ago class-advanced-ads.php 9 years ago
class-advanced-ads.php
729 lines
1 <?php
2
3 /**
4 * Advanced Ads.
5 *
6 * @package Advanced_Ads_Admin
7 * @author Thomas Maier <thomas.maier@webgilde.com>
8 * @license GPL-2.0+
9 * @link http://webgilde.com
10 * @copyright 2013-2015 Thomas Maier, webgilde GmbH
11 */
12
13 /**
14 * Plugin class. This class should ideally be used to work with the
15 * public-facing side of the WordPress site.
16 *
17 * @package Advanced_Ads
18 * @author Thomas Maier <thomas.maier@webgilde.com>
19 */
20 class Advanced_Ads {
21
22 /**
23 * post type slug
24 *
25 * @since 1.0.0
26 * @var string
27 */
28 const POST_TYPE_SLUG = 'advanced_ads';
29
30 /**
31 * ad group slug
32 *
33 * @since 1.0.0
34 * @var string
35 */
36 const AD_GROUP_TAXONOMY = 'advanced_ads_groups';
37
38 /**
39 * Instance of this class.
40 *
41 * @since 1.0.0
42 * @var object
43 */
44 private static $instance = null;
45
46 /**
47 * array with ads currently delivered in the frontend
48 */
49 public $current_ads = array();
50
51 /**
52 * ad types
53 */
54 public $ad_types = array();
55
56 /**
57 * plugin options
58 *
59 * @since 1.0.1
60 * @var array (if loaded)
61 */
62 protected $options = false;
63
64 /**
65 * interal plugin options – set by the plugin
66 *
67 * @since 1.4.5
68 * @var array (if loaded)
69 */
70 protected $internal_options = false;
71
72 /**
73 * list of bots and crawlers to exclude from ad impressions
74 *
75 * @since 1.4.9
76 * @var array list of bots
77 */
78 protected $bots = array('008','ABACHOBot','Accoona-AI-Agent','AddSugarSpiderBot','ADmantX','AhrefsBot','alexa','AnyApexBot','appie','Apple-PubSub','Arachmo','Ask Jeeves','avira.com','B-l-i-t-z-B-O-T','Baiduspider','BecomeBot','BeslistBot','BillyBobBot','Bimbot','Bingbot','BLEXBot','BlitzBOT','boitho.com-dc','boitho.com-robot','bot','btbot','CatchBot','Cerberian Drtrs','Charlotte','ConveraCrawler','cosmos','Covario IDS','crawler','CrystalSemanticsBot','curl','DataparkSearch','DiamondBot','Discobot','Dotbot','EmeraldShield.com WebBot','envolk[ITS]spider','EsperanzaBot','Exabot','expo9','facebookexternalhit','FAST Enterprise Crawler','FAST-WebCrawler','FDSE robot','Feedfetcher-Google','FindLinks','Firefly','froogle','FurlBot','FyberSpider','g2crawler','Gaisbot','GalaxyBot','genieBot','Genieo','Gigabot','Girafabot','Googlebot','Googlebot-Image','GrapeshotCrawler','GurujiBot','HappyFunBot','heritrix','hl_ftien_spider','Holmes','htdig','https://developers.google.com','ia_archiver','iaskspider','iCCrawler','ichiro','igdeSpyder','InfoSeek','inktomi','IRLbot','IssueCrawler','Jaxified Bot','Jyxobot','KoepaBot','Kraken','L.webis','LapozzBot','Larbin','LDSpider','LexxeBot','Linguee','Bot','LinkWalker','lmspider','looksmart','lwp-trivial','mabontland','magpie-crawler','Mail.RU_Bot','MaxPointCrawler','Mediapartners-Google','MJ12bot','Mnogosearch','mogimogi','MojeekBot','Moreoverbot','Morning Paper','msnbot','MSRBot','MVAClient','mxbot','NationalDirectory','NetResearchServer','NetSeer Crawler','NewsGator','NG-Search','nicebot','noxtrumbot','Nusearch','Spider','Nutch crawler','NutchCVS','Nymesis','obot','oegp','omgilibot','OmniExplorer_Bot','OOZBOT','Orbiter','PageBitesHyperBot','Peew','polybot','Pompos','PostPost','proximic','Psbot','PycURL','Qseero','rabaz','Radian6','RAMPyBot','Rankivabot','RufusBot','SandCrawler','savetheworldheritage','SBIder','Scooter','ScoutJet','Scrubby','SearchSight','Seekbot','semanticdiscovery','Sensis','Web Crawler','SEOChat::Bot','SeznamBot','Shim-Crawler','ShopWiki','Shoula robot','silk','Sitebot','Snappy','sogou spider','Sogou web spider','Sosospider','Spade','Speedy Spider','Sqworm','StackRambler','suggybot','SurveyBot','SynooBot','TechnoratiSnoop','TECNOSEEK','Teoma','TerrawizBot','TheSuBot','Thumbnail.CZ','robot','TinEye','truwoGPS','TurnitinBot','TweetedTimes Bot','TwengaBot','updated','URL_Spider_SQL','Urlfilebot','Vagabondo','VoilaBot','voltron','Vortex','voyager','VYU2','WebAlta Crawler','WebBug','webcollage','WebFindBot','WebIndex','Websquash.com','WeSEE:Ads','wf84','Wget','WoFindeIch Robot','WomlpeFactory','WordPress','Xaldon_WebSpider','yacy','Yahoo! Slurp','Yahoo! Slurp China','YahooSeeker','YahooSeeker-Testing','YandexBot','YandexImages','Yasaklibot','Yeti','YodaoBot','yoogliFetchAgent','YoudaoBot','Zao','Zealbot','zspider','ZyBorg');
79
80 /**
81 *
82 * @var Advanced_Ads_Model
83 */
84 protected $model;
85
86 /**
87 *
88 * @var Advanced_Ads_Plugin
89 */
90 protected $plugin;
91
92 /**
93 *
94 * @var Advanced_Ads_Select
95 */
96 protected $ad_selector;
97
98 /**
99 * is the query the main query?, when WP_Query is used
100 *
101 * @var bool
102 */
103 private $is_main_query;
104
105
106 private function __construct() {
107 $this->plugin = Advanced_Ads_Plugin::get_instance();
108 $this->plugin->set_model($this->get_model());
109 $this->ad_selector = Advanced_Ads_Select::get_instance();
110
111 // initialize plugin specific functions
112 add_action( 'init', array( $this, 'wp_init' ) );
113
114 // only when not doing ajax
115 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
116 Advanced_Ads_Ajax::get_instance();
117 }
118 add_action( 'plugins_loaded', array( $this, 'wp_plugins_loaded' ) );
119
120 // allow add-ons to interact
121 add_action( 'init', array( $this, 'advanced_ads_loaded' ), 9 );
122 }
123
124 /**
125 * Return an instance of this class.
126 *
127 * @since 1.0.0
128 * @return Advanced_Ads A single instance of this class.
129 */
130 public static function get_instance() {
131
132 // If the single instance hasn't been set, set it now.
133 if ( null === self::$instance ) {
134 self::$instance = new self;
135 }
136
137 return self::$instance;
138 }
139
140 /**
141 *
142 * @return Advanced_Ads_Model
143 */
144 public function get_model() {
145
146 global $wpdb;
147
148 if ( ! isset($this->model) ) {
149 $this->model = new Advanced_Ads_Model( $wpdb );
150 }
151
152 return $this->model;
153 }
154
155 /**
156 * Initialize the plugin by setting localization and loading public scripts
157 * and styles.
158 *
159 * @since 1.0.0
160 */
161 public function wp_plugins_loaded()
162 {
163 // register hook for global constants
164 add_action( 'wp', array( $this, 'set_disabled_constant' ) );
165
166 // setup default ad types
167 add_filter( 'advanced-ads-ad-types', array( $this, 'setup_default_ad_types' ), 5 );
168
169 // register hooks and filters for auto ad injection
170 $this->init_injection();
171
172 // manipulate sidebar widget
173 add_filter( 'dynamic_sidebar_params', array( $this, 'manipulate_widget_output' ) );
174
175 // add meta robots noindex, nofollow to images, which are part of 'Image ad' ad type
176 add_action( 'wp_head', array( $this, 'noindex_attachment_images' ) );
177
178 // check if ads are disabled in secondary queries
179 add_action( 'the_post', array( $this, 'set_query_type' ), 10, 2 );
180
181
182 }
183
184 /**
185 * allow add-ons to hook
186 */
187 public function advanced_ads_loaded() {
188 do_action( 'advanced-ads-plugin-loaded' );
189 }
190
191 /**
192 * init / load plugin specific functions and settings
193 *
194 * @since 1.0.0
195 */
196 public function wp_init(){
197 // load ad post types
198 $this->create_post_types();
199 // set ad types array
200 $this->set_ad_types();
201 }
202
203 /**
204 * define ad types with their options
205 *
206 * name => publically readable name
207 * description => publically readable description
208 * editor => kind of editor: text (normal text field), content (WP content field), none (no content field)
209 * will display text field, if left empty
210 *
211 * @since 1.0.0
212 */
213 public function set_ad_types() {
214
215 /**
216 * load default ad type files
217 * custom ad types can also be loaded in your own plugin or functions.php
218 */
219 $types = array();
220
221 /**
222 * developers can add new ad types using this filter
223 * see classes/ad-type-content.php for an example for an ad type and usage of this filter
224 */
225 $this->ad_types = apply_filters( 'advanced-ads-ad-types', $types );
226 }
227
228 public function init_injection() {
229 // -TODO abstract
230 add_action( 'wp_head', array( $this, 'inject_header' ), 20 );
231 add_action( 'wp_footer', array( $this, 'inject_footer' ), 20 );
232 add_filter( 'the_content', array( $this, 'inject_content' ), $this->plugin->get_content_injection_priority() );
233 }
234
235 /**
236 * set global constant that prevents ads from being displayed on the current page view
237 *
238 * @since 1.3.10
239 */
240 public function set_disabled_constant(){
241
242 global $post, $wp_the_query;
243
244 // don't set the constant if already defined
245 if ( defined( 'ADVADS_ADS_DISABLED' ) ) { return; }
246
247 $options = $this->plugin->options();
248
249 // check if ads are disabled completely
250 if ( ! empty($options['disabled-ads']['all']) ){
251 define( 'ADVADS_ADS_DISABLED', true );
252 return;
253 }
254
255 // check if ads are disabled from 404 pages
256 if ( $wp_the_query->is_404() && ! empty($options['disabled-ads']['404']) ){
257 define( 'ADVADS_ADS_DISABLED', true );
258 return;
259 }
260
261 // check if ads are disabled from non singular pages (often = archives)
262 if ( ! $wp_the_query->is_singular() && ! empty($options['disabled-ads']['archives']) ){
263 define( 'ADVADS_ADS_DISABLED', true );
264 return;
265 }
266
267 // check if ads are disabled in Feed
268 if ( $wp_the_query->is_feed() && ( ! isset( $options['disabled-ads']['feed'] ) || $options['disabled-ads']['feed'] ) ) {
269 define( 'ADVADS_ADS_DISABLED', true );
270 return;
271 }
272
273 // check if ads are disabled on the current page
274 if ( $wp_the_query->is_singular() && isset($post->ID) ){
275 $post_ad_options = get_post_meta( $post->ID, '_advads_ad_settings', true );
276
277 if ( ! empty($post_ad_options['disable_ads']) ){
278 define( 'ADVADS_ADS_DISABLED', true );
279 }
280 };
281 }
282
283 /**
284 * Return the plugin slug.
285 *
286 * @since 1.0.0
287 * @return Plugin slug variable.
288 */
289 public function get_plugin_slug() {
290 return $this->plugin->get_plugin_slug();
291 }
292
293 /**
294 * add plain and content ad types to the default ads of the plugin using a filter
295 *
296 * @since 1.0.0
297 *
298 */
299 function setup_default_ad_types($types){
300 $types['plain'] = new Advanced_Ads_Ad_Type_Plain(); /* plain text and php code */
301 $types['content'] = new Advanced_Ads_Ad_Type_Content(); /* rich content editor */
302 $types['image'] = new Advanced_Ads_Ad_Type_Image(); /* image ads */
303 $types['group'] = new Advanced_Ads_Ad_Type_Group(); /* group ad */
304 return $types;
305 }
306
307 /**
308 * log error messages when debug is enabled
309 *
310 * @since 1.0.0
311 * @link http://www.smashingmagazine.com/2011/03/08/ten-things-every-wordpress-plugin-developer-should-know/
312 */
313 static function log($message) {
314 if ( true === WP_DEBUG ) {
315 if ( is_array( $message ) || is_object( $message ) ) {
316 error_log( __('Advanced Ads Error following:', 'advanced-ads' ) );
317 error_log( print_r( $message, true ) );
318 } else {
319 $message = sprintf( __( 'Advanced Ads Error: %s', 'advanced-ads' ), $message );
320 error_log( $message );
321 }
322 }
323 }
324
325 // compat method
326 public function options() {
327 return $this->plugin->options();
328 }
329
330 // compat method
331 public function internal_options() {
332 return $this->plugin->internal_options();
333 }
334
335 /**
336 * injected ad into header
337 *
338 * @since 1.1.0
339 */
340 public function inject_header(){
341 $placements = get_option( 'advads-ads-placements', array() );
342 foreach ( $placements as $_placement_id => $_placement ){
343 if ( isset($_placement['type']) && 'header' == $_placement['type'] ){
344 $_options = isset( $_placement['options'] ) ? $_placement['options'] : array();
345 echo Advanced_Ads_Select::get_instance()->get_ad_by_method( $_placement_id, Advanced_Ads_Select::PLACEMENT, $_options );
346 }
347 }
348 }
349
350 /**
351 * injected ads into footer
352 *
353 * @since 1.1.0
354 */
355 public function inject_footer(){
356 $placements = get_option( 'advads-ads-placements', array() );
357 foreach ( $placements as $_placement_id => $_placement ){
358 if ( isset($_placement['type']) && 'footer' == $_placement['type'] ){
359 $_options = isset( $_placement['options'] ) ? $_placement['options'] : array();
360 echo Advanced_Ads_Select::get_instance()->get_ad_by_method( $_placement_id, Advanced_Ads_Select::PLACEMENT, $_options );
361 }
362 }
363 }
364
365 /**
366 * injected ad into content (before and after)
367 * displays ALL ads
368 *
369 * @since 1.1.0
370 * @param str $content post content
371 */
372 public function inject_content($content = ''){
373 $options = $this->plugin->options();
374
375 // check if ads are disabled in secondary queries and this function was called by ajax (in secondary query)
376 if ( ! empty( $options['disabled-ads']['secondary'] ) && ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
377 return $content;
378 }
379
380 // run only within the loop on single pages of public post types
381 $public_post_types = get_post_types( array( 'public' => true, 'publicly_queryable' => true ), 'names', 'or' );
382
383 // make sure that no ad is injected into another ad
384 if ( get_post_type() == self::POST_TYPE_SLUG ){
385 return $content;
386 }
387
388 // check if admin allows injection in all places
389 if( ! isset( $options['content-injection-everywhere'] ) ){
390 // check if this is a singular page within the loop
391 if ( ( ! is_singular( $public_post_types ) && ! is_feed() ) || ! in_the_loop() ) { return $content; }
392 }
393
394 $placements = get_option( 'advads-ads-placements', array() );
395
396 if( ! apply_filters( 'advanced-ads-can-inject-into-content', true, $content, $placements )){
397 return $content;
398 }
399
400 foreach ( $placements as $_placement_id => $_placement ){
401 if ( empty($_placement['item']) || ! isset($_placement['type']) ) { continue; }
402 $_options = isset( $_placement['options'] ) ? $_placement['options'] : array();
403
404 // check if injection is ok for a specific placement id
405 if( ! apply_filters( 'advanced-ads-can-inject-into-content-' . $_placement_id, true, $content, $_placement_id )){
406 continue;
407 }
408
409 switch ( $_placement['type'] ) {
410 case 'post_top':
411 // TODO broken: does not serve placement but serves ad directly
412 $content = Advanced_Ads_Select::get_instance()->get_ad_by_method( $_placement_id, Advanced_Ads_Select::PLACEMENT, $_options ) . $content;
413 break;
414 case 'post_bottom':
415 $content .= Advanced_Ads_Select::get_instance()->get_ad_by_method( $_placement_id, Advanced_Ads_Select::PLACEMENT, $_options );
416 break;
417 case 'post_content':
418 $content = Advanced_Ads_Placements::inject_in_content( $_placement_id, $_options, $content );
419 break;
420 }
421 }
422
423 return $content;
424 }
425
426 /**
427 * load all ads based on WP_Query conditions
428 *
429 * @deprecated 1.4.8 use model class
430 * @since 1.1.0
431 * @param arr $args WP_Query arguments that are more specific that default
432 * @return arr $ads array with post objects
433 */
434 static function get_ads($args = array()){
435 return self::get_instance()->get_model()->get_ads($args);
436 }
437
438 /**
439 * load all ad groups
440 *
441 * @deprecated 1.4.8 use model class
442 * @since 1.1.0
443 * @param arr $args array with options
444 * @return arr $groups array with ad groups
445 * @link http://codex.wordpress.org/Function_Reference/get_terms
446 */
447 static function get_ad_groups($args = array()){
448 return self::get_instance()->get_model()->get_ad_groups($args);
449 }
450
451 /**
452 * get the array with ad placements
453 *
454 * @since 1.1.0
455 * @deprecated 1.4.8 use model
456 * @return arr $ad_placements
457 */
458 static public function get_ad_placements_array(){
459 return self::get_instance()->get_model()->get_ad_placements_array();
460 }
461
462 /**
463 *
464 * @deprecated 1.4.8 use model
465 * @return array
466 */
467 public static function get_ad_conditions() {
468 return self::get_instance()->get_model()->get_ad_conditions();
469 }
470
471 /**
472 * general check if ads can be displayed for the whole page impression
473 *
474 * @since 1.4.9
475 * @return bool true, if ads can be displayed
476 * @todo move this to set_disabled_constant()
477 */
478 public function can_display_ads(){
479
480 // check global constant if ads are enabled or disabled
481 if ( defined( 'ADVADS_ADS_DISABLED' ) ) {
482 return false;
483 }
484
485 $options = $this->options();
486
487 // check if ads are disabled in secondary queries
488 // and this is not main query and this is not ajax (because main query does not exist in ajax but ad needs to be shown)
489 if ( ! empty( $options['disabled-ads']['secondary'] ) && ! $this->is_main_query() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
490 return false;
491 }
492
493 $see_ads_capability = isset($options['hide-for-user-role']) && $options['hide-for-user-role'] != '' ? $options['hide-for-user-role'] : false;
494
495 // check if user is logged in and if so if users with his rights can see ads
496 if ( $see_ads_capability && is_user_logged_in() && current_user_can( $see_ads_capability ) ) {
497 return false;
498 }
499
500 // check bots if option is enabled
501 if( isset($options['block-bots']) && $options['block-bots'] && $this->is_bot() ) {
502 return false;
503 }
504
505 return true;
506 }
507
508 /**
509 * check if the current user agent is given or a bot
510 *
511 * @since 1.4.9
512 * @return bool true if the current user agent is empty or a bot
513 */
514 public function is_bot(){
515 $bots = apply_filters('advanced-ads-bots', $this->bots);
516 $bots = implode('|', $bots);
517 $bots = preg_replace('@[^-_;/|\][ a-z0-9]@i', '', $bots);
518 $regex = "@$bots@i";
519
520 if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT'] !== '') {
521 $agent = $_SERVER['HTTP_USER_AGENT'];
522
523 return preg_match($regex, $agent) === 1;
524 }
525
526 return true;
527 }
528
529 /**
530 * Registers ad post type and group taxonomies
531 *
532 * @since 1.0.0
533 */
534 public function create_post_types() {
535 if ( 1 !== did_action( 'init' ) && 1 !== did_action( 'uninstall_' . ADVADS_BASE ) ) {
536 return;
537 }
538
539 // register ad group taxonomy
540 if ( ! taxonomy_exists( Advanced_Ads::AD_GROUP_TAXONOMY ) ){
541 $post_type_params = $this->get_group_taxonomy_params();
542 register_taxonomy( Advanced_Ads::AD_GROUP_TAXONOMY, array( Advanced_Ads::POST_TYPE_SLUG ), $post_type_params );
543 }
544
545 // register ad post type
546 if ( ! post_type_exists( Advanced_Ads::POST_TYPE_SLUG ) ) {
547 $post_type_params = $this->get_post_type_params();
548 register_post_type( Advanced_Ads::POST_TYPE_SLUG, $post_type_params );
549 }
550 }
551
552 /**
553 * Defines the parameters for the ad post type taxonomy
554 *
555 * @since 1.0.0
556 * @return array
557 */
558 protected function get_group_taxonomy_params(){
559 $labels = array(
560 'name' => _x( 'Ad Groups', 'ad group general name', 'advanced-ads' ),
561 'singular_name' => _x( 'Ad Group', 'ad group singular name', 'advanced-ads' ),
562 'search_items' => __( 'Search Ad Groups', 'advanced-ads' ),
563 'all_items' => __( 'All Ad Groups', 'advanced-ads' ),
564 'parent_item' => __( 'Parent Ad Groups', 'advanced-ads' ),
565 'parent_item_colon' => __( 'Parent Ad Groups:', 'advanced-ads' ),
566 'edit_item' => __( 'Edit Ad Group', 'advanced-ads' ),
567 'update_item' => __( 'Update Ad Group', 'advanced-ads' ),
568 'add_new_item' => __( 'Add New Ad Group', 'advanced-ads' ),
569 'new_item_name' => __( 'New Ad Groups Name', 'advanced-ads' ),
570 'menu_name' => __( 'Groups', 'advanced-ads' ),
571 'not_found' => __( 'No Ad Group found', 'advanced-ads' ),
572 );
573
574 $args = array(
575 'public' => false,
576 'hierarchical' => true,
577 'labels' => $labels,
578 'show_ui' => true,
579 'show_in_nav_menus' => false,
580 'show_tagcloud' => false,
581 'show_admin_column' => true,
582 'query_var' => false,
583 'rewrite' => false,
584 );
585
586 return $args;
587 }
588
589 /**
590 * Defines the parameters for the custom post type
591 *
592 * @since 1.0.0
593 * @return array
594 */
595 protected function get_post_type_params() {
596 $labels = array(
597 'name' => __( 'Ads', 'advanced-ads' ),
598 'singular_name' => __( 'Ad', 'advanced-ads' ),
599 'add_new' => __( 'New Ad', 'advanced-ads' ),
600 'add_new_item' => __( 'Add New Ad', 'advanced-ads' ),
601 'edit' => __( 'Edit', 'advanced-ads' ),
602 'edit_item' => __( 'Edit Ad', 'advanced-ads' ),
603 'new_item' => __( 'New Ad', 'advanced-ads' ),
604 'view' => __( 'View', 'advanced-ads' ),
605 'view_item' => __( 'View the Ad', 'advanced-ads' ),
606 'search_items' => __( 'Search Ads', 'advanced-ads' ),
607 'not_found' => __( 'No Ads found', 'advanced-ads' ),
608 'not_found_in_trash' => __( 'No Ads found in Trash', 'advanced-ads' ),
609 'parent' => __( 'Parent Ad', 'advanced-ads' ),
610 );
611
612 $post_type_params = array(
613 'labels' => $labels,
614 'public' => false,
615 'show_ui' => true,
616 'show_in_menu' => false,
617 'hierarchical' => false,
618 'capabilities' => array(
619 // Meta capabilities
620 'edit_post' => 'advanced_ads_edit_ads',
621 'read_post' => 'advanced_ads_edit_ads',
622 'delete_post' => 'advanced_ads_edit_ads',
623 'edit_page' => 'advanced_ads_edit_ads',
624 'read_page' => 'advanced_ads_edit_ads',
625 'delete_page' => 'advanced_ads_edit_ads',
626 // Primitive capabilities used outside of map_meta_cap()
627 'edit_posts' => 'advanced_ads_edit_ads',
628 'edit_others_posts' => 'advanced_ads_edit_ads',
629 'publish_posts' => 'advanced_ads_edit_ads',
630 'read_private_posts' => 'advanced_ads_edit_ads',
631 // Primitive capabilities used within map_meta_cap():
632 'read' => 'advanced_ads_edit_ads',
633 'delete_posts' => 'advanced_ads_edit_ads',
634 'delete_private_posts' => 'advanced_ads_edit_ads',
635 'delete_published_posts' => 'advanced_ads_edit_ads',
636 'delete_others_posts' => 'advanced_ads_edit_ads',
637 'edit_private_posts' => 'advanced_ads_edit_ads',
638 'edit_published_posts' => 'advanced_ads_edit_ads',
639 'create_posts' => 'advanced_ads_edit_ads',
640 ),
641 'has_archive' => false,
642 //'rewrite' => array( 'slug' => ADVADS_SLUG ),
643 'query_var' => true,
644 'supports' => array( 'title' ),
645 'taxonomies' => array( Advanced_Ads::AD_GROUP_TAXONOMY )
646 );
647
648 return apply_filters( 'advanced-ads-post-type-params', $post_type_params );
649 }
650
651 /**
652 * manipulate output of ad widget
653 *
654 * @since 1.6.8.2
655 * @param arr $params widget and sidebar params
656 */
657 public function manipulate_widget_output( $params = array() ){
658
659 if( $params[0]['widget_name'] === 'Advanced Ads' ){
660
661 $options = $this->plugin->options();
662 // hide id by default (when options are empty) or when option is enabled
663 if( $options === array() || ( isset( $options['remove-widget-id'] ) && $options['remove-widget-id'] ) ){
664 $pattern = '#\s(id)=("|\')[^"^\']+("|\')#';
665 $params[0]['before_widget'] = preg_replace( $pattern, '', $params[0]['before_widget']);
666 }
667 }
668
669 return $params;
670 }
671
672 /**
673 * Add meta robots noindex, nofollow to images, which are part of 'Image ad' ad type
674 */
675 function noindex_attachment_images() {
676 global $post;
677 // if the image was not attached to any post
678 if ( is_attachment() && is_object( $post ) && isset( $post->post_parent ) && $post->post_parent === 0 ) {
679 // if at least one ad contains the image
680 if ( get_post_meta( get_the_ID(), '_advanced-ads_parent_id', true ) > 0 ) {
681 echo '<meta name="robots" content="noindex, nofollow" />';
682 }
683 }
684 }
685
686 /**
687 * Supports the "$this->is_main_query=true" while main query is being executed
688 *
689 * @param WP_Post &$post The Post object (passed by reference).
690 * @param WP_Query &$this The current Query object (passed by reference).
691 */
692 function set_query_type( $post, $query = null ) {
693 if ( $query instanceof WP_Query ) {
694 $this->is_main_query = $query->is_main_query();
695 }
696 }
697
698 /**
699 * Check if main query is being executed
700 *
701 * @return bool true while main query is being executed or not in the loop, false otherwise
702 */
703 public function is_main_query() {
704 if ( ! in_the_loop() ) {
705 // the secondary query check only designed for within post content
706 return true;
707 }
708
709 return $this->is_main_query === true;
710 }
711
712 /**
713 * get an "Advertisement" label to use before single ad or before first ad in a group
714 *
715 * @return string label, bool false if label should not be displayed
716 */
717 public function get_label() {
718 $advads_options = Advanced_Ads::get_instance()->options();
719
720 if ( isset( $advads_options['custom-label']['enabled'] ) ) {
721 $label = ! empty( $advads_options['custom-label']['text'] ) ? esc_html( $advads_options['custom-label']['text'] ) : _x( 'Advertisements', 'label above ads', 'advanced-ads' );
722
723 $template = sprintf( '<div class="%s">%s</div>', Advanced_Ads_Plugin::get_instance()->get_frontend_prefix() . 'adlabel', $label );
724 return apply_filters( 'advanced-ads-custom-label', $template, $label );
725 }
726 return false;
727 }
728 }
729