PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 1.7.3
Advanced Ads – Ad Manager & AdSense v1.7.3
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 10 years ago includes 12 years ago views 10 years ago class-advanced-ads.php 10 years ago
class-advanced-ads.php
719 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
121 /**
122 * Return an instance of this class.
123 *
124 * @since 1.0.0
125 * @return Advanced_Ads A single instance of this class.
126 */
127 public static function get_instance() {
128
129 // If the single instance hasn't been set, set it now.
130 if ( null === self::$instance ) {
131 self::$instance = new self;
132 }
133
134 return self::$instance;
135 }
136
137 /**
138 *
139 * @return Advanced_Ads_Model
140 */
141 public function get_model() {
142
143 global $wpdb;
144
145 if ( ! isset($this->model) ) {
146 $this->model = new Advanced_Ads_Model( $wpdb );
147 }
148
149 return $this->model;
150 }
151
152 /**
153 * Initialize the plugin by setting localization and loading public scripts
154 * and styles.
155 *
156 * @since 1.0.0
157 */
158 public function wp_plugins_loaded()
159 {
160 // register hook for global constants
161 add_action( 'wp', array( $this, 'set_disabled_constant' ) );
162
163 // setup default ad types
164 add_filter( 'advanced-ads-ad-types', array( $this, 'setup_default_ad_types' ), 5 );
165
166 // register hooks and filters for auto ad injection
167 $this->init_injection();
168
169 // allow add-ons to hook
170 do_action( 'advanced-ads-plugin-loaded' );
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 * init / load plugin specific functions and settings
186 *
187 * @since 1.0.0
188 */
189 public function wp_init(){
190 // load ad post types
191 $this->create_post_types();
192 // set ad types array
193 $this->set_ad_types();
194 }
195
196 /**
197 * define ad types with their options
198 *
199 * name => publically readable name
200 * description => publically readable description
201 * editor => kind of editor: text (normal text field), content (WP content field), none (no content field)
202 * will display text field, if left empty
203 *
204 * @since 1.0.0
205 */
206 public function set_ad_types() {
207
208 /**
209 * load default ad type files
210 * custom ad types can also be loaded in your own plugin or functions.php
211 */
212 $types = array();
213
214 /**
215 * developers can add new ad types using this filter
216 * see classes/ad-type-content.php for an example for an ad type and usage of this filter
217 */
218 $this->ad_types = apply_filters( 'advanced-ads-ad-types', $types );
219 }
220
221 public function init_injection() {
222 // -TODO abstract
223 add_action( 'wp_head', array( $this, 'inject_header' ), 20 );
224 add_action( 'wp_footer', array( $this, 'inject_footer' ), 20 );
225 add_filter( 'the_content', array( $this, 'inject_content' ), $this->plugin->get_content_injection_priority() );
226 }
227
228 /**
229 * set global constant that prevents ads from being displayed on the current page view
230 *
231 * @since 1.3.10
232 */
233 public function set_disabled_constant(){
234
235 global $post, $wp_the_query;
236
237 // don't set the constant if already defined
238 if ( defined( 'ADVADS_ADS_DISABLED' ) ) { return; }
239
240 $options = $this->plugin->options();
241
242 // check if ads are disabled completely
243 if ( ! empty($options['disabled-ads']['all']) ){
244 define( 'ADVADS_ADS_DISABLED', true );
245 return;
246 }
247
248 // check if ads are disabled from 404 pages
249 if ( $wp_the_query->is_404() && ! empty($options['disabled-ads']['404']) ){
250 define( 'ADVADS_ADS_DISABLED', true );
251 return;
252 }
253
254 // check if ads are disabled from non singular pages (often = archives)
255 if ( ! $wp_the_query->is_singular() && ! empty($options['disabled-ads']['archives']) ){
256 define( 'ADVADS_ADS_DISABLED', true );
257 return;
258 }
259
260 // check if ads are disabled in Feed
261 if ( $wp_the_query->is_feed() && ( ! isset( $options['disabled-ads']['feed'] ) || $options['disabled-ads']['feed'] ) ) {
262 define( 'ADVADS_ADS_DISABLED', true );
263 return;
264 }
265
266 // check if ads are disabled on the current page
267 if ( $wp_the_query->is_singular() && isset($post->ID) ){
268 $post_ad_options = get_post_meta( $post->ID, '_advads_ad_settings', true );
269
270 if ( ! empty($post_ad_options['disable_ads']) ){
271 define( 'ADVADS_ADS_DISABLED', true );
272 }
273 };
274 }
275
276 /**
277 * Return the plugin slug.
278 *
279 * @since 1.0.0
280 * @return Plugin slug variable.
281 */
282 public function get_plugin_slug() {
283 return $this->plugin->get_plugin_slug();
284 }
285
286 /**
287 * add plain and content ad types to the default ads of the plugin using a filter
288 *
289 * @since 1.0.0
290 *
291 */
292 function setup_default_ad_types($types){
293 $types['plain'] = new Advanced_Ads_Ad_Type_Plain(); /* plain text and php code */
294 $types['content'] = new Advanced_Ads_Ad_Type_Content(); /* rich content editor */
295 $types['image'] = new Advanced_Ads_Ad_Type_Image(); /* image ads */
296 $types['group'] = new Advanced_Ads_Ad_Type_Group(); /* group ad */
297 return $types;
298 }
299
300 /**
301 * log error messages when debug is enabled
302 *
303 * @since 1.0.0
304 * @link http://www.smashingmagazine.com/2011/03/08/ten-things-every-wordpress-plugin-developer-should-know/
305 */
306 static function log($message) {
307 if ( true === WP_DEBUG ) {
308 if ( is_array( $message ) || is_object( $message ) ) {
309 error_log( __('Advanced Ads Error following:', 'advanced-ads' ) );
310 error_log( print_r( $message, true ) );
311 } else {
312 $message = sprintf( __( 'Advanced Ads Error: %s', 'advanced-ads' ), $message );
313 error_log( $message );
314 }
315 }
316 }
317
318 // compat method
319 public function options() {
320 return $this->plugin->options();
321 }
322
323 // compat method
324 public function internal_options() {
325 return $this->plugin->internal_options();
326 }
327
328 /**
329 * injected ad into header
330 *
331 * @since 1.1.0
332 */
333 public function inject_header(){
334 $placements = get_option( 'advads-ads-placements', array() );
335 foreach ( $placements as $_placement_id => $_placement ){
336 if ( isset($_placement['type']) && 'header' == $_placement['type'] ){
337 $_options = isset( $_placement['options'] ) ? $_placement['options'] : array();
338 echo Advanced_Ads_Select::get_instance()->get_ad_by_method( $_placement_id, Advanced_Ads_Select::PLACEMENT, $_options );
339 }
340 }
341 }
342
343 /**
344 * injected ads into footer
345 *
346 * @since 1.1.0
347 */
348 public function inject_footer(){
349 $placements = get_option( 'advads-ads-placements', array() );
350 foreach ( $placements as $_placement_id => $_placement ){
351 if ( isset($_placement['type']) && 'footer' == $_placement['type'] ){
352 $_options = isset( $_placement['options'] ) ? $_placement['options'] : array();
353 echo Advanced_Ads_Select::get_instance()->get_ad_by_method( $_placement_id, Advanced_Ads_Select::PLACEMENT, $_options );
354 }
355 }
356 }
357
358 /**
359 * injected ad into content (before and after)
360 * displays ALL ads
361 *
362 * @since 1.1.0
363 * @param str $content post content
364 */
365 public function inject_content($content = ''){
366 $options = $this->plugin->options();
367
368 // check if ads are disabled in secondary queries and this function was called by ajax (in secondary query)
369 if ( ! empty( $options['disabled-ads']['secondary'] ) && ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
370 return $content;
371 }
372
373 // run only within the loop on single pages of public post types
374 $public_post_types = get_post_types( array( 'public' => true, 'publicly_queryable' => true ), 'names', 'or' );
375
376 // make sure that no ad is injected into another ad
377 if ( get_post_type() == self::POST_TYPE_SLUG ){
378 return $content;
379 }
380
381 // check if admin allows injection in all places
382 if( ! isset( $options['content-injection-everywhere'] ) ){
383 // check if this is a singular page within the loop
384 if ( ( ! is_singular( $public_post_types ) && ! is_feed() ) || ! in_the_loop() ) { return $content; }
385 }
386
387 $placements = get_option( 'advads-ads-placements', array() );
388
389 if( ! apply_filters( 'advanced-ads-can-inject-into-content', true, $content, $placements )){
390 return $content;
391 }
392
393 foreach ( $placements as $_placement_id => $_placement ){
394 if ( empty($_placement['item']) || ! isset($_placement['type']) ) { continue; }
395 $_options = isset( $_placement['options'] ) ? $_placement['options'] : array();
396
397 // check if injection is ok for a specific placement id
398 if( ! apply_filters( 'advanced-ads-can-inject-into-content-' . $_placement_id, true, $content, $_placement_id )){
399 continue;
400 }
401
402 switch ( $_placement['type'] ) {
403 case 'post_top':
404 // TODO broken: does not serve placement but serves ad directly
405 $content = Advanced_Ads_Select::get_instance()->get_ad_by_method( $_placement_id, Advanced_Ads_Select::PLACEMENT, $_options ) . $content;
406 break;
407 case 'post_bottom':
408 $content .= Advanced_Ads_Select::get_instance()->get_ad_by_method( $_placement_id, Advanced_Ads_Select::PLACEMENT, $_options );
409 break;
410 case 'post_content':
411 $content = Advanced_Ads_Placements::inject_in_content( $_placement_id, $_options, $content );
412 break;
413 }
414 }
415
416 return $content;
417 }
418
419 /**
420 * load all ads based on WP_Query conditions
421 *
422 * @deprecated 1.4.8 use model class
423 * @since 1.1.0
424 * @param arr $args WP_Query arguments that are more specific that default
425 * @return arr $ads array with post objects
426 */
427 static function get_ads($args = array()){
428 return self::get_instance()->get_model()->get_ads($args);
429 }
430
431 /**
432 * load all ad groups
433 *
434 * @deprecated 1.4.8 use model class
435 * @since 1.1.0
436 * @param arr $args array with options
437 * @return arr $groups array with ad groups
438 * @link http://codex.wordpress.org/Function_Reference/get_terms
439 */
440 static function get_ad_groups($args = array()){
441 return self::get_instance()->get_model()->get_ad_groups($args);
442 }
443
444 /**
445 * get the array with ad placements
446 *
447 * @since 1.1.0
448 * @deprecated 1.4.8 use model
449 * @return arr $ad_placements
450 */
451 static public function get_ad_placements_array(){
452 return self::get_instance()->get_model()->get_ad_placements_array();
453 }
454
455 /**
456 *
457 * @deprecated 1.4.8 use model
458 * @return array
459 */
460 public static function get_ad_conditions() {
461 return self::get_instance()->get_model()->get_ad_conditions();
462 }
463
464 /**
465 * general check if ads can be displayed for the whole page impression
466 *
467 * @since 1.4.9
468 * @return bool true, if ads can be displayed
469 * @todo move this to set_disabled_constant()
470 */
471 public function can_display_ads(){
472
473 // check global constant if ads are enabled or disabled
474 if ( defined( 'ADVADS_ADS_DISABLED' ) ) {
475 return false;
476 }
477
478 $options = $this->options();
479
480 // check if ads are disabled in secondary queries
481 // 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)
482 if ( ! empty( $options['disabled-ads']['secondary'] ) && ! $this->is_main_query() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
483 return false;
484 }
485
486 $see_ads_capability = isset($options['hide-for-user-role']) && $options['hide-for-user-role'] != '' ? $options['hide-for-user-role'] : false;
487
488 // check if user is logged in and if so if users with his rights can see ads
489 if ( $see_ads_capability && is_user_logged_in() && current_user_can( $see_ads_capability ) ) {
490 return false;
491 }
492
493 // check bots if option is enabled
494 if( isset($options['block-bots']) && $options['block-bots'] && $this->is_bot() ) {
495 return false;
496 }
497
498 return true;
499 }
500
501 /**
502 * check if the current user agent is given or a bot
503 *
504 * @since 1.4.9
505 * @return bool true if the current user agent is empty or a bot
506 */
507 public function is_bot(){
508 $bots = apply_filters('advanced-ads-bots', $this->bots);
509 $bots = implode('|', $bots);
510 $bots = preg_replace('@[^-_;/|\][ a-z0-9]@i', '', $bots);
511 $regex = "@$bots@i";
512
513 if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT'] !== '') {
514 $agent = $_SERVER['HTTP_USER_AGENT'];
515
516 return preg_match($regex, $agent) === 1;
517 }
518
519 return true;
520 }
521
522 /**
523 * Registers ad post type and group taxonomies
524 *
525 * @since 1.0.0
526 */
527 public function create_post_types() {
528 if ( 1 !== did_action( 'init' ) ) {
529 return;
530 }
531
532 // register ad group taxonomy
533 if ( ! taxonomy_exists( Advanced_Ads::AD_GROUP_TAXONOMY ) ){
534 $post_type_params = $this->get_group_taxonomy_params();
535 register_taxonomy( Advanced_Ads::AD_GROUP_TAXONOMY, array( Advanced_Ads::POST_TYPE_SLUG ), $post_type_params );
536 }
537
538 // register ad post type
539 if ( ! post_type_exists( Advanced_Ads::POST_TYPE_SLUG ) ) {
540 $post_type_params = $this->get_post_type_params();
541 register_post_type( Advanced_Ads::POST_TYPE_SLUG, $post_type_params );
542 }
543 }
544
545 /**
546 * Defines the parameters for the ad post type taxonomy
547 *
548 * @since 1.0.0
549 * @return array
550 */
551 protected function get_group_taxonomy_params(){
552 $labels = array(
553 'name' => _x( 'Ad Groups', 'ad group general name', 'advanced-ads' ),
554 'singular_name' => _x( 'Ad Group', 'ad group singular name', 'advanced-ads' ),
555 'search_items' => __( 'Search Ad Groups', 'advanced-ads' ),
556 'all_items' => __( 'All Ad Groups', 'advanced-ads' ),
557 'parent_item' => __( 'Parent Ad Groups', 'advanced-ads' ),
558 'parent_item_colon' => __( 'Parent Ad Groups:', 'advanced-ads' ),
559 'edit_item' => __( 'Edit Ad Group', 'advanced-ads' ),
560 'update_item' => __( 'Update Ad Group', 'advanced-ads' ),
561 'add_new_item' => __( 'Add New Ad Group', 'advanced-ads' ),
562 'new_item_name' => __( 'New Ad Groups Name', 'advanced-ads' ),
563 'menu_name' => __( 'Groups', 'advanced-ads' ),
564 'not_found' => __( 'No Ad Group found', 'advanced-ads' ),
565 );
566
567 $args = array(
568 'public' => false,
569 'hierarchical' => true,
570 'labels' => $labels,
571 'show_ui' => true,
572 'show_in_nav_menus' => false,
573 'show_tagcloud' => false,
574 'show_admin_column' => true,
575 'query_var' => false,
576 'rewrite' => false,
577 );
578
579 return $args;
580 }
581
582 /**
583 * Defines the parameters for the custom post type
584 *
585 * @since 1.0.0
586 * @return array
587 */
588 protected function get_post_type_params() {
589 $labels = array(
590 'name' => __( 'Ads', 'advanced-ads' ),
591 'singular_name' => __( 'Ad', 'advanced-ads' ),
592 'add_new' => __( 'New Ad', 'advanced-ads' ),
593 'add_new_item' => __( 'Add New Ad', 'advanced-ads' ),
594 'edit' => __( 'Edit', 'advanced-ads' ),
595 'edit_item' => __( 'Edit Ad', 'advanced-ads' ),
596 'new_item' => __( 'New Ad', 'advanced-ads' ),
597 'view' => __( 'View', 'advanced-ads' ),
598 'view_item' => __( 'View the Ad', 'advanced-ads' ),
599 'search_items' => __( 'Search Ads', 'advanced-ads' ),
600 'not_found' => __( 'No Ads found', 'advanced-ads' ),
601 'not_found_in_trash' => __( 'No Ads found in Trash', 'advanced-ads' ),
602 'parent' => __( 'Parent Ad', 'advanced-ads' ),
603 );
604
605 $post_type_params = array(
606 'labels' => $labels,
607 'public' => false,
608 'show_ui' => true,
609 'show_in_menu' => false,
610 'hierarchical' => false,
611 'capabilities' => array(
612 // Meta capabilities
613 'edit_post' => 'advanced_ads_edit_ads',
614 'read_post' => 'advanced_ads_edit_ads',
615 'delete_post' => 'advanced_ads_edit_ads',
616 // Primitive capabilities used outside of map_meta_cap()
617 'edit_posts' => 'advanced_ads_edit_ads',
618 'edit_others_posts' => 'advanced_ads_edit_ads',
619 'publish_posts' => 'advanced_ads_edit_ads',
620 'read_private_posts' => 'advanced_ads_edit_ads',
621 // Primitive capabilities used within map_meta_cap():
622 'read' => 'advanced_ads_edit_ads',
623 'delete_posts' => 'advanced_ads_edit_ads',
624 'delete_private_posts' => 'advanced_ads_edit_ads',
625 'delete_published_posts' => 'advanced_ads_edit_ads',
626 'delete_others_posts' => 'advanced_ads_edit_ads',
627 'edit_private_posts' => 'advanced_ads_edit_ads',
628 'edit_published_posts' => 'advanced_ads_edit_ads',
629 'create_posts' => 'advanced_ads_edit_ads',
630 ),
631 'has_archive' => false,
632 'rewrite' => array( 'slug' => ADVADS_SLUG ),
633 'query_var' => true,
634 'supports' => array( 'title' ),
635 'taxonomies' => array( Advanced_Ads::AD_GROUP_TAXONOMY )
636 );
637
638 return apply_filters( 'advanced-ads-post-type-params', $post_type_params );
639 }
640
641 /**
642 * manipulate output of ad widget
643 *
644 * @since 1.6.8.2
645 * @param arr $params widget and sidebar params
646 */
647 public function manipulate_widget_output( $params = array() ){
648
649 if( $params[0]['widget_name'] === 'Advanced Ads' ){
650
651 $options = $this->plugin->options();
652 // hide id by default (when options are empty) or when option is enabled
653 if( $options === array() || ( isset( $options['remove-widget-id'] ) && $options['remove-widget-id'] ) ){
654 $pattern = '#\s(id)=("|\')[^"^\']+("|\')#';
655 $params[0]['before_widget'] = preg_replace( $pattern, '', $params[0]['before_widget']);
656 }
657 }
658
659 return $params;
660 }
661
662 /**
663 * Add meta robots noindex, nofollow to images, which are part of 'Image ad' ad type
664 */
665 function noindex_attachment_images() {
666 global $post;
667 // if the image was not attached to any post
668 if ( is_attachment() && is_object( $post ) && isset( $post->post_parent ) && $post->post_parent === 0 ) {
669 // if at least one ad contains the image
670 if ( get_post_meta( get_the_ID(), '_advanced-ads_parent_id', true ) > 0 ) {
671 echo '<meta name="robots" content="noindex, nofollow" />';
672 }
673 }
674 }
675
676 /**
677 * Supports the "$this->is_main_query=true" while main query is being executed
678 *
679 * @param WP_Post &$post The Post object (passed by reference).
680 * @param WP_Query &$this The current Query object (passed by reference).
681 */
682 function set_query_type( $post, $query = null ) {
683 if ( $query instanceof WP_Query ) {
684 $this->is_main_query = $query->is_main_query();
685 }
686 }
687
688 /**
689 * Check if main query is being executed
690 *
691 * @return bool true while main query is being executed or not in the loop, false otherwise
692 */
693 public function is_main_query() {
694 if ( ! in_the_loop() ) {
695 // the secondary query check only designed for within post content
696 return true;
697 }
698
699 return $this->is_main_query === true;
700 }
701
702 /**
703 * get an "Advertisement" label to use before single ad or before first ad in a group
704 *
705 * @return string label, bool false if label should not be displayed
706 */
707 public function get_label() {
708 $advads_options = Advanced_Ads::get_instance()->options();
709
710 if ( isset( $advads_options['custom-label']['enabled'] ) ) {
711 $label = ! empty( $advads_options['custom-label']['text'] ) ? esc_html( $advads_options['custom-label']['text'] ) : _x( 'Advertisements', 'label before ads' );
712
713 $template = sprintf( '<div class="%s">%s</div>', Advanced_Ads_Plugin::get_instance()->get_frontend_prefix() . 'adlabel', $label );
714 return apply_filters( 'advanced-ads-custom-label', $template, $label );
715 }
716 return false;
717 }
718 }
719