Guest User

Untitled

a guest
Dec 2nd, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 113.81 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Ad Inserter
  4. Version: 2.0.6
  5. Description: Insert any ad or code into Wordpress. Perfect for all kinds of ads. Simply enter any ad or HTML/Javascript/PHP code and select where and how you want to display it.
  6. Author: Igor Funa
  7. Author URI: http://igorfuna.com/
  8. Plugin URI: http://tinymonitor.com/ad-inserter
  9. */
  10.  
  11. /*
  12. Change Log
  13.  
  14. Ad Inserter 2.0.6 - 25 November 2016
  15. - Added support to filter subpages created by the <!--nextpage--> tag
  16. - Added option to import block name (Pro only)
  17. - Cookie deleted only when it exists and debugging is disabled
  18. - Few minor bug fixes
  19.  
  20. Ad Inserter 2.0.5 - 8 october 2016
  21. - Cookie created only when debugging is enabled
  22. - Few minor bug fixes
  23.  
  24. Ad Inserter 2.0.4 - 30 September 2016
  25. - Bug fix: Cursor position always at the end of block name
  26. - State of debugging functions saved to cookie
  27. - Few minor bug fixes
  28.  
  29. Ad Inserter 2.0.3 - 26 September 2016
  30. - Debugging functions in admin toolbar available only for administrators
  31. - Added option to hide debugging functions in admin toolbar
  32. - Added shortcode for debugger
  33. - Few minor bug fixes
  34.  
  35. Ad Inserter 2.0.2 - 25 September 2016
  36. - Changed javascript version check to get plugin version from the HTML page
  37. - Added warning if old cached version of CSS file is loaded on the settings page
  38. - Added warning if version query parameter for js/css files is removed due to inappropriate caching
  39.  
  40. Ad Inserter 2.0.1 - 24 September 2016
  41. - Bug fix: Shortcodes called by name were not displayed
  42.  
  43. Ad Inserter 2.0.0 - 23 September 2016
  44. - Redesigned user interface
  45. - Added many debugging tools for easier troubleshooting
  46. - New feature: Code preview tool with visual CSS editor
  47. - New feature: Label inserted blocks
  48. - New feature: Show available positions for automatic insertion
  49. - New feature: Show HTML tags in posts/static pages
  50. - New feature: Log Ad Inserter processing
  51. - Improved loading speed of the settings page
  52. - Improved block insertion processing speed
  53. - Added support to avoid insertion near images, headers and other elements
  54. - Added option to avoid insertion in feeds
  55. - Added option to display code blocks only to administrators
  56. - Added option for publishig date check for display positions Before/After Content
  57. - Added option for server-side device check for header and footer code
  58. - Added option for maximum page/post words
  59. - Added option for maximum paragraph words
  60. - Added option to black/white-list post IDs
  61. - Added option to black/white-list url query parameters
  62. - Added warning if the settings page is blocked by ad blocker
  63. - Added warning if old cached version of javascript is loaded on the settings page
  64. - Added support for multisite installations to disable settings, widgets and exceptions on network sites (Pro only)
  65. - Block names can be edited by clicking on the name
  66. - Filters now work also on posts and single pages
  67. - CSS code for client-side detection moved to inline CSS
  68. - Bug fix: Minimum user roles for exception editing was not calculated properly
  69. - Bug fix: Server-side detection checkbox was not saved properly
  70. - Many other minor bug fixes, code improvements and cosmetic changes
  71.  
  72. Ad Inserter 1.7.0 - 16 August 2016
  73. - Bug fix: Shortcodes did not ignore post/static page exceptions
  74. - Slightly redesigned user interface
  75. - Excerpt/Post number(s) renamed to Filter as it now works on all display positions
  76. - Widget setting removed from Automatic display to Manual display section
  77. - Added support to disable widgets (standalone checkbox in Manual display)
  78. - Added call counter/filter for widgets
  79. - Added support to edit CSS for predefined styles
  80. - Few other minor bug fixes, code improvements and cosmetic changes
  81.  
  82. Ad Inserter 1.6.7 - 9 August 2016
  83. - Bug fix: Block code textarea was not escaped
  84. - Added checks for page types for shortcodes
  85. - Added support for Before/After Post position call counter/filter
  86. - Few minor cosmetic changes
  87.  
  88. Ad Inserter 1.6.6 - 5 August 2016
  89. - Bug fix: Display on Homepage and other blog pages might get disabled - important if you were using PHP function call or shortcode (import of settings from 1.6.4)
  90. - Few minor cosmetic changes
  91. - Requirements changed to WordPress 4.0 or newer
  92. - Added initial support for Pro version
  93.  
  94. Ad Inserter 1.6.5 - 1 August 2016
  95. - Fixed bug: Wrong counting of max insertions
  96. - Change: display position Before Title was renamed to Before Post
  97. - Added support for display position After Post
  98. - Added support for posts with no <p> tags (paragraphs separated with \r\n\r\n characters)
  99. - Added support for paragraph processing on homepage, category, archive and search pages
  100. - Added support for custom viewports
  101. - Added support for PHP function call counter
  102. - Added support to disable code block on error 404 pages
  103. - Added support to debug paragraph tags
  104.  
  105. Ad Inserter 1.6.4 - 15 May 2016
  106. - Fixed bug: For shortcodes in posts the url was not checked
  107. - Optimizations for device detection
  108.  
  109. Ad Inserter 1.6.3 - 6 April 2016
  110. - Removed deprecated code (fixes PHP 7 deprecated warnings)
  111. - Added support for paragraphs with div and other HTML tags (h1, h2, h3,...)
  112.  
  113. Ad Inserter 1.6.2 - 2 April 2016
  114. - Removed deprecated code (fixes PHP Fatal error Call to a member function get_display_type)
  115. - Added support to change plugin processing priority
  116.  
  117. Ad Inserter 1.6.1 - 28 February 2016
  118. - Fixed bug: For shortcodes in posts the date was not checked
  119. - Fixed error with some templates "Call to undefined method is_main_query()"
  120. - Added support for minumum number of page/post words for Before/After content display option
  121. - Added support for {author} and {author_name} tags
  122.  
  123. Ad Inserter 1.6.0 - 9 January 2016
  124. - Added support for client-side device detection
  125. - Many code improvements
  126. - Improved plugin processing speed
  127. - Removed support for deprecated tags for manual insertion {adinserter n}
  128. - Few minor bug fixes
  129.  
  130. Ad Inserter 1.5.8 - 20 December 2015
  131. - Fixed notice "Undefined index: adinserter_selected_block_" when saving page or post
  132.  
  133. Ad Inserter 1.5.7 - 20 December 2015
  134. - Fixed notice "has_cap was called with an argument that is deprecated since version 2.0!"
  135. - Few minor bug fixes and code improvements
  136. - Added support to blacklist or whitelist url patterns: /url-start*. *url-pattern*, *url-end
  137. - Added support to define minimum number of words in paragraphs
  138. - Added support to define minimum user role for page/post Ad Inserter exceptions editing
  139. - Added support to limit insertions of individual code blocks
  140. - Added support to filter direct visits (no referer)
  141.  
  142. Ad Inserter 1.5.6 - 14 August 2015
  143. - Fixed Security Vulnerability: Plugin was vulnerable to Cross-Site Scripting (XSS)
  144. - Few bug fixes and code improvements
  145.  
  146. Ad Inserter 1.5.5 - 6 June 2015
  147. - Few bug fixes and code improvements
  148. - Added support to export and import all Ad Inserter settings
  149.  
  150. Ad Inserter 1.5.4 - 31 May 2015
  151. - Many code optimizations and cosmetic changes
  152. - Header and Footer code blocks moved to settings tab (#)
  153. - Added support to process shortcodes of other plugins used in Ad Inserter code blocks
  154. - Added support to white-list or black-list individual urls
  155. - Added support to export and import settings for code blocks
  156. - Added support to specify excerpts for block insertion
  157. - Added support to specify text that must be present when counting paragraphs
  158.  
  159. Ad Inserter 1.5.3 - 2 May 2015
  160. - Fixed Security Vulnerability: Plugin was vulnerable to a combination of CSRF/XSS attacks (credits to Kaustubh Padwad)
  161. - Fixed bug: In some cases deprecated widgets warning reported errors
  162. - Added support to white-list or black-list tags
  163. - Added support for category slugs in category list
  164. - Added support for relative paragraph positions
  165. - Added support for individual code block exceptions on post/page editor page
  166. - Added support for minimum number of words
  167. - Added support to disable syntax highlighting editor (to allow using copy/paste on mobile devices)
  168.  
  169. Ad Inserter 1.5.2 - 15 March 2015
  170. - Fixed bug: Widget titles might be displayed at wrong sidebar positions
  171. - Change: Default code block CSS class name was changed from ad-inserter to code-block to prevent Ad Blockers from blocking Ad Inserter divs
  172. - Added warning message if deprecated widgets are used
  173. - Added support to display blocks on desktop + tablet and desktop + phone devices
  174.  
  175. Ad Inserter 1.5.1 - 3 March 2015
  176. - Few fixes to solve plugin incompatibility issues
  177. - Added support to disable all ads on specific page
  178.  
  179. Ad Inserter 1.5.0 - 2 March 2015
  180. - Added support to display blocks on all, desktop or mobile devices
  181. - Added support for new widgets API - one widget for all code blocks with multiple instances
  182. - Added support to change wrapping code CSS class name
  183. - Fixed bug: Display block N days after post is published was not working properly
  184. - Fixed bug: Display block after paragraph in some cases was not working propery
  185.  
  186. Ad Inserter 1.4.1 - 29 December 2014
  187. - Fixed bug: Code blocks configured as widgets were not displayed properly on widgets admin page
  188.  
  189. Ad Inserter 1.4.0 - 21 December 2014
  190. - Added support to skip paragraphs with specified text
  191. - Added position After paragraph
  192. - Added support for header and footer scripts
  193. - Added support for custom CSS styles
  194. - Added support to display blocks to all, logged in or not logged in users
  195. - Added support for syntax highlighting
  196. - Added support for shortcodes
  197. - Added classes to block wrapping divs
  198. - Few bugs fixed
  199.  
  200. Ad Inserter 1.3.5 - 18 March 2014
  201. - Fixed bug: missing echo for PHP function call example
  202.  
  203. Ad Inserter 1.3.4 - 15 March 2014
  204. - Added option for no code wrapping with div
  205. - Added option to insert block codes from PHP code
  206. - Changed HTML codes to disable display on specific pages
  207. - Selected code block position is preserved after settings are saved
  208. - Manual insertion can be enabled or disabled regardless of primary display setting
  209. - Fixed bug: in some cases Before Title display setting inserted code into RSS feed
  210.  
  211. Ad Inserter 1.3.3 - 8 January 2014
  212. - Added option to insert ads also before or after the excerpt
  213. - Fixed bug: in some cases many errors reported after activating the plugin
  214. - Few minor bugs fixed
  215. - Few minor cosmetic changes
  216.  
  217. Ad Inserter 1.3.2 - 4 December 2013
  218. - Fixed blank settings page caused by incompatibility with some themes or plugins
  219.  
  220. Ad Inserter 1.3.1 - 3 December 2013
  221. - Added option to insert ads also on pages
  222. - Added option to process PHP code
  223. - Few bugs fixed
  224.  
  225. Ad Inserter 1.3.0 - 27 November 2013
  226. - Number of ad slots increased to 16
  227. - New tabbed admin interface
  228. - Ads can be manually inserted also with {adinserter AD_NUMBER} tag
  229. - Fixed bug: only the last ad block set to Before Title was displayed
  230. - Few other minor bugs fixed
  231. - Few cosmetic changes
  232.  
  233. Ad Inserter 1.2.1 - 19 November 2013
  234. - Fixed problem: || in ad code (e.g. asynchronous code for AdSense) causes only part of the code to be inserted (|| to rotate ads is replaced with |rotate|)
  235.  
  236. Ad Inserter 1.2.0 - 15/05/2012
  237. - Fixed bug: manual tags in posts lists were not removed
  238. - Added position Before title
  239. - Added support for minimum number of paragraphs
  240. - Added support for page display options for Widget and Before title positions
  241. - Alignment now works for all display positions
  242.  
  243. Ad Inserter 1.1.3 - 07/04/2012
  244. - Fixed bug for {search_query}: When the tag is empty {smart_tag} is used in all cases
  245. - Few changes in the settings page
  246.  
  247. Ad Inserter 1.1.2 - 16/07/2011
  248. - Fixed error with multisite/network installations
  249.  
  250. Ad Inserter 1.1.1 - 16/07/2011
  251. - Fixed bug in Float Right setting display
  252.  
  253. Ad Inserter 1.1.0 - 05/06/2011
  254. - Added option to manually display individual ads
  255. - Added new ad alignments: left, center, right
  256. - Added {search_query} tag
  257. - Added support for category black list and white list
  258.  
  259. Ad Inserter 1.0.4 - 19/12/2010
  260. - HTML entities for {title} and {short_title} are now decoded
  261. - Added {tag} to display the first tag
  262.  
  263. Ad Inserter 1.0.3 - 05/12/2010
  264. - Fixed bug for rotating ads
  265.  
  266. Ad Inserter 1.0.2 - 04/12/2010
  267. - Added support for rotating ads
  268.  
  269. Ad Inserter 1.0.1 - 17/11/2010
  270. - Added support for different sidebar implementations
  271.  
  272. Ad Inserter 1.0.0 - 14/11/2010
  273. - Initial release
  274.  
  275. */
  276.  
  277.  
  278. if (!defined ('AD_INSERTER_PLUGIN_DIR'))
  279. define ('AD_INSERTER_PLUGIN_DIR', plugin_dir_path (__FILE__));
  280.  
  281. /* Version check */
  282. global $wp_version, $version_string;
  283. $exit_msg = 'Ad Inserter requires WordPress 4.0 or newer. <a href="http://codex.wordpress.org/Upgrading_WordPress">Please update!</a>';
  284.  
  285. if (version_compare ($wp_version, "4.0", "<")) {
  286. exit ($exit_msg);
  287. }
  288.  
  289. global $block_object, $ai_wp_data, $ad_interter_globals, $ai_last_check, $ai_last_time, $ai_processing_log, $ai_db_options_extract, $ai_db_options;
  290.  
  291. //include required files
  292. require_once AD_INSERTER_PLUGIN_DIR.'class.php';
  293. require_once AD_INSERTER_PLUGIN_DIR.'constants.php';
  294. require_once AD_INSERTER_PLUGIN_DIR.'settings.php';
  295. require_once AD_INSERTER_PLUGIN_DIR.'preview.php';
  296.  
  297. if (isset ($_GET [AI_URL_DEBUG_PHP]) && $_GET [AI_URL_DEBUG_PHP] != '') {
  298. ini_set ('display_errors', 1);
  299. error_reporting (E_ALL);
  300. }
  301.  
  302. $version_array = explode (".", AD_INSERTER_VERSION);
  303. $version_string = "";
  304. foreach ($version_array as $number) {
  305. $version_string .= sprintf ("%02d", $number);
  306. }
  307.  
  308. $ai_wp_data [AI_WP_DEBUGGING] = 0;
  309. $ai_wp_data [AI_WP_URL] = remove_parameters_from_url ($_SERVER ['REQUEST_URI']);
  310.  
  311. if (!is_admin()) {
  312. if (!isset ($_GET [AI_URL_DEBUG]))
  313. if (isset ($_GET [AI_URL_DEBUG_PROCESSING]) || (isset ($_COOKIE ['AI_WP_DEBUGGING']) && ($_COOKIE ['AI_WP_DEBUGGING'] & AI_DEBUG_PROCESSING) != 0)) {
  314. if (!isset ($_GET [AI_URL_DEBUG_PROCESSING]) || $_GET [AI_URL_DEBUG_PROCESSING] == 1) {
  315. $ai_wp_data [AI_WP_DEBUGGING] |= AI_DEBUG_PROCESSING;
  316.  
  317. $ai_last_time = microtime ();
  318. $ai_processing_log = array ();
  319. ai_log ("INITIALIZATION START");
  320. }
  321. }
  322. }
  323.  
  324. $ad_interter_globals = array ();
  325. $block_object = array ();
  326.  
  327. ai_load_settings ();
  328.  
  329. $ai_wp_data [AI_WP_PAGE_TYPE] = AI_PT_NONE;
  330. $ai_wp_data [AI_WP_USER] = AI_USER_NOT_SET;
  331. $ai_wp_data [AI_CONTEXT] = AI_CONTEXT_NONE;
  332. $ai_wp_data [SERVER_SIDE_DETECTION] = false;
  333. $ai_wp_data [CLIENT_SIDE_DETECTION] = false;
  334.  
  335. for ($counter = 1; $counter <= AD_INSERTER_BLOCKS; $counter ++) {
  336. $obj = $block_object [$counter];
  337.  
  338. if ($obj->get_detection_server_side()) $ai_wp_data [SERVER_SIDE_DETECTION] = true;
  339. if ($obj->get_detection_client_side ()) $ai_wp_data [CLIENT_SIDE_DETECTION] = true;
  340. }
  341.  
  342. $adH = $block_object [AI_HEADER_OPTION_NAME];
  343. $adF = $block_object [AI_FOOTER_OPTION_NAME];
  344. if ($adH->get_detection_server_side()) $ai_wp_data [SERVER_SIDE_DETECTION] = true;
  345. if ($adF->get_detection_server_side()) $ai_wp_data [SERVER_SIDE_DETECTION] = true;
  346.  
  347.  
  348. if ($ai_wp_data [SERVER_SIDE_DETECTION]) {
  349. require_once AD_INSERTER_PLUGIN_DIR.'includes/Mobile_Detect.php';
  350.  
  351. $detect = new ai_Mobile_Detect;
  352.  
  353. define ('AI_MOBILE', $detect->isMobile ());
  354. define ('AI_TABLET', $detect->isTablet ());
  355. define ('AI_PHONE', AI_MOBILE && !AI_TABLET);
  356. define ('AI_DESKTOP', !AI_MOBILE);
  357. } else {
  358. define ('AI_MOBILE', true);
  359. define ('AI_TABLET', true);
  360. define ('AI_PHONE', true);
  361. define ('AI_DESKTOP', true);
  362. }
  363.  
  364. if (isset ($_POST [AI_FORM_SAVE]))
  365. define ('AI_SYNTAX_HIGHLIGHTING', isset ($_POST ["syntax-highlighter-theme"]) && $_POST ["syntax-highlighter-theme"] != AI_OPTION_DISABLED); else
  366. define ('AI_SYNTAX_HIGHLIGHTING', $ai_db_options [AI_GLOBAL_OPTION_NAME]["SYNTAX_HIGHLIGHTER_THEME"] != AI_OPTION_DISABLED);
  367.  
  368. add_action ('admin_menu', 'ai_admin_menu_hook');
  369.  
  370. add_action ('init', 'ai_init_hook');
  371. //add_action ('admin_notices', 'ai_admin_notice_hook');
  372.  
  373. add_action( 'wp', 'ai_wp_hook');
  374.  
  375. if ($adH->get_enable_manual ())
  376. add_action ('wp_head', 'ai_wp_head_hook');
  377.  
  378. if ($adF->get_enable_manual ())
  379. add_action ('wp_footer', 'ai_wp_footer_hook');
  380.  
  381. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0)
  382. add_action ('shutdown', 'ai_shutdown_hook');
  383.  
  384. add_action ('widgets_init', 'ai_widgets_init_hook');
  385. add_action ('add_meta_boxes', 'ai_add_meta_box_hook');
  386. add_action ('save_post', 'ai_save_meta_box_data_hook');
  387.  
  388. if (function_exists ('ai_hooks')) ai_hooks ();
  389.  
  390. add_action ('wp_enqueue_scripts', 'ai_enqueue_scripts_hook');
  391.  
  392. add_filter ('plugin_action_links_'.plugin_basename (__FILE__), 'ai_plugin_action_links');
  393. add_filter ('plugin_row_meta', 'ai_set_plugin_meta', 10, 2);
  394. add_action ('wp_ajax_ai_preview', 'ai_preview');
  395. add_action ('wp_ajax_nopriv_ai_preview', 'ai_preview');
  396.  
  397. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) ai_log ("INITIALIZATION END\n");
  398.  
  399.  
  400.  
  401. function ai_toolbar ($wp_admin_bar) {
  402. global $block_object, $ai_wp_data;
  403.  
  404. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_BLOCKS) == 0) $debug_blocks = 1; else $debug_blocks = 0;
  405. $debug_blocks_class = $debug_blocks == 0 ? ' on' : '';
  406.  
  407. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_POSITIONS) == 0) $debug_positions = 0; else $debug_positions = '';
  408. $debug_positions_class = $debug_positions === '' ? ' on' : '';
  409.  
  410. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_TAGS) == 0) $debug_tags = 1; else $debug_tags = 0;
  411. $debug_tags_class = $debug_tags == 0 ? ' on' : '';
  412.  
  413. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) == 0) $debug_processing = 1; else $debug_processing = 0;
  414. $debug_processing_class = $debug_processing == 0 ? ' on' : '';
  415.  
  416. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_NO_INSERTION) == 0) $debug_no_insertion = 1; else $debug_no_insertion = 0;
  417. $debug_no_insertion_class = $debug_no_insertion == 0 ? ' on' : '';
  418.  
  419. $debug_settings_on = $debug_blocks == 0 || $debug_positions === '' || $debug_tags == 0 || $debug_processing == 0 || $debug_no_insertion == 0;
  420.  
  421. $debug_settings_class = $debug_settings_on ? ' on' : '';
  422. $top_menu_url = $debug_settings_on ? add_query_arg (AI_URL_DEBUG, '0', remove_parameters_from_url ($_SERVER ['REQUEST_URI'])) :
  423. add_query_arg (array (AI_URL_DEBUG_BLOCKS => '1', AI_URL_DEBUG_POSITIONS => '0', AI_URL_DEBUG_TAGS => '1'), remove_parameters_from_url ($_SERVER ['REQUEST_URI']));
  424.  
  425. $wp_admin_bar->add_node (array (
  426. 'id' => 'ai-toolbar',
  427. 'group' => true
  428. ));
  429. $wp_admin_bar->add_node (array (
  430. 'id' => 'ai-toolbar-settings',
  431. 'parent' => 'ai-toolbar',
  432. 'title' => '<span class="ab-icon'.$debug_settings_class.'"></span>'.AD_INSERTER_NAME,
  433. 'href' => $top_menu_url,
  434. ));
  435. $wp_admin_bar->add_node (array (
  436. 'id' => 'ai-toolbar-blocks',
  437. 'parent' => 'ai-toolbar-settings',
  438. 'title' => '<span class="ab-icon'.$debug_blocks_class.'"></span>Label Blocks',
  439. 'href' => set_url_parameter (AI_URL_DEBUG_BLOCKS, $debug_blocks),
  440. ));
  441. $wp_admin_bar->add_node (array (
  442. 'id' => 'ai-toolbar-positions',
  443. 'parent' => 'ai-toolbar-settings',
  444. 'title' => '<span class="ab-icon'.$debug_positions_class.'"></span>Show Positions',
  445. 'href' => set_url_parameter (AI_URL_DEBUG_POSITIONS, $debug_positions),
  446. ));
  447.  
  448. $paragraph_blocks = array ();
  449. for ($block = 0; $block <= AD_INSERTER_BLOCKS; $block ++) {
  450. $obj = $block_object [$block];
  451. $display = $obj->get_display_type();
  452. if ($block == 0 || $display == AD_SELECT_BEFORE_PARAGRAPH || $display == AD_SELECT_AFTER_PARAGRAPH) {
  453.  
  454. $block_tags = trim ($block_object [$block]->get_paragraph_tags ());
  455. $direction = $block_object [$block]->get_direction_type() == AD_DIRECTION_FROM_TOP ? 't' : 'b';
  456. $paragraph_min_words = intval ($obj->get_minimum_paragraph_words());
  457. $paragraph_max_words = intval ($obj->get_maximum_paragraph_words());
  458. $paragraph_text_type = $obj->get_paragraph_text_type ();
  459. $paragraph_text = trim (html_entity_decode ($obj->get_paragraph_text()));
  460. $inside_blockquote = $obj->get_count_inside_blockquote ();
  461.  
  462. if ($block_tags != '') {
  463. $found = false;
  464. foreach ($paragraph_blocks as $index => $paragraph_block) {
  465. if ($paragraph_block ['tags'] == $block_tags &&
  466. $paragraph_block ['direction'] == $direction &&
  467. $paragraph_block ['min'] == $paragraph_min_words &&
  468. $paragraph_block ['max'] == $paragraph_max_words &&
  469. $paragraph_block ['text_type'] == $paragraph_text_type &&
  470. $paragraph_block ['text'] == $paragraph_text &&
  471. $paragraph_block ['blockquote'] == $inside_blockquote
  472. ) {
  473. $found = true;
  474. break;
  475. }
  476. }
  477. if ($found) array_push ($paragraph_blocks [$index]['blocks'], $block); else
  478. $paragraph_blocks []= array ('blocks' => array ($block),
  479. 'tags' => $block_tags,
  480. 'direction' => $direction,
  481. 'min' => $paragraph_min_words,
  482. 'max' => $paragraph_max_words,
  483. 'text_type' => $paragraph_text_type,
  484. 'text' => $paragraph_text,
  485. 'blockquote' => $inside_blockquote,
  486. );
  487. }
  488. }
  489. }
  490.  
  491. foreach ($paragraph_blocks as $index => $paragraph_block) {
  492. $block_class = $debug_positions === '' && in_array ($ai_wp_data [AI_WP_DEBUG_BLOCK], $paragraph_block ['blocks']) ? ' on' : '';
  493. $wp_admin_bar->add_node (array (
  494. 'id' => 'ai-toolbar-positions-'.$index,
  495. 'parent' => 'ai-toolbar-positions',
  496. 'title' => '<span class="ab-icon'.$block_class.'"></span>'.
  497. $paragraph_block ['tags'].
  498. ($paragraph_block ['direction'] == 'b' ? ' <span class="dashicons dashicons-arrow-up-alt up-icon"></span>' : '').
  499. ($paragraph_block ['min'] != 0 ? ' min '.$paragraph_block ['min']. ' ' : '').
  500. ($paragraph_block ['max'] != 0 ? ' max '.$paragraph_block ['max']. ' ' : '').
  501. ($paragraph_block ['blockquote'] ? ' blockquote ' : '').
  502. ($paragraph_block ['text'] != '' ? ($paragraph_block ['text_type'] == AD_DO_NOT_CONTAIN ? ' NC ' : ' C ').' '.$paragraph_block ['text'] : ''),
  503. 'href' => set_url_parameter (AI_URL_DEBUG_POSITIONS, $paragraph_block ['blocks'][0]),
  504. ));
  505. }
  506.  
  507. $wp_admin_bar->add_node (array (
  508. 'id' => 'ai-toolbar-tags',
  509. 'parent' => 'ai-toolbar-settings',
  510. 'title' => '<span class="ab-icon'.$debug_tags_class.'"></span>Show HTML Tags',
  511. 'href' => set_url_parameter (AI_URL_DEBUG_TAGS, $debug_tags),
  512. ));
  513. $wp_admin_bar->add_node (array (
  514. 'id' => 'ai-toolbar-no-insertion',
  515. 'parent' => 'ai-toolbar-settings',
  516. 'title' => '<span class="ab-icon'.$debug_no_insertion_class.'"></span>Disable Insertion',
  517. 'href' => set_url_parameter (AI_URL_DEBUG_NO_INSERTION, $debug_no_insertion),
  518. ));
  519. $wp_admin_bar->add_node (array (
  520. 'id' => 'ai-toolbar-processing',
  521. 'parent' => 'ai-toolbar-settings',
  522. 'title' => '<span class="ab-icon'.$debug_processing_class.'"></span>Log Processing',
  523. 'href' => set_url_parameter (AI_URL_DEBUG_PROCESSING, $debug_processing),
  524. ));
  525. }
  526.  
  527. function set_user () {
  528. global $ai_wp_data;
  529.  
  530. if ($ai_wp_data [AI_WP_USER] != AI_USER_NOT_SET) return;
  531.  
  532. $ai_wp_data [AI_WP_USER] = AI_USER_NOT_LOGGED_IN;
  533.  
  534. if (is_user_logged_in ()) $ai_wp_data [AI_WP_USER] |= AI_USER_LOGGED_IN;
  535. if (current_user_role () >= 5) $ai_wp_data [AI_WP_USER] |= AI_USER_ADMINISTRATOR;
  536.  
  537. if (isset ($_GET [AI_URL_DEBUG_USER]) && $_GET [AI_URL_DEBUG_USER] != 0) $ai_wp_data [AI_WP_USER] = $_GET [AI_URL_DEBUG_USER];
  538. }
  539.  
  540. function set_page_type () {
  541. global $ai_wp_data;
  542.  
  543. if ($ai_wp_data [AI_WP_PAGE_TYPE] != AI_PT_NONE) return;
  544.  
  545. if (is_admin()) $ai_wp_data [AI_WP_PAGE_TYPE] = AI_PT_ADMIN;
  546. elseif (is_feed()) $ai_wp_data [AI_WP_PAGE_TYPE] = AI_PT_FEED;
  547. elseif (is_404()) $ai_wp_data [AI_WP_PAGE_TYPE] = AI_PT_404;
  548. elseif (is_front_page ()) $ai_wp_data [AI_WP_PAGE_TYPE] = AI_PT_HOMEPAGE;
  549. elseif (is_page()) $ai_wp_data [AI_WP_PAGE_TYPE] = AI_PT_STATIC;
  550. elseif (is_single()) $ai_wp_data [AI_WP_PAGE_TYPE] = AI_PT_POST;
  551. elseif (is_category()) $ai_wp_data [AI_WP_PAGE_TYPE] = AI_PT_CATEGORY;
  552. elseif (is_search()) $ai_wp_data [AI_WP_PAGE_TYPE] = AI_PT_SEARCH;
  553. elseif (is_archive()) $ai_wp_data [AI_WP_PAGE_TYPE] = AI_PT_ARCHIVE;
  554. }
  555.  
  556. function ai_log_message ($message) {
  557. global $ai_last_time, $ai_processing_log;
  558. $ai_processing_log []= rtrim (sprintf ("%4d %-50s", (microtime () - $ai_last_time) * 1000, $message));
  559. }
  560.  
  561. function ai_log_filter_content ($content_string) {
  562.  
  563. $content_string = preg_replace ("/\[\[AI_[A|B]P([\d].?)\]\]/", "", $content_string);
  564. return str_replace (array ("<!--", "-->", "\n", "\r"), array ("[!--", "--]", "*n", "*r"), $content_string);
  565. }
  566.  
  567. function ai_log_content (&$content) {
  568. if (strlen ($content) < 100) ai_log (ai_log_filter_content ($content) . ' ['.number_of_words ($content).']'); else
  569. ai_log (ai_log_filter_content (substr ($content, 0, 60)) . ' ... ' . ai_log_filter_content (substr ($content, - 60)) . ' ['.number_of_words ($content).']');
  570. }
  571.  
  572. function ai_log_block_status ($block, $ai_last_check) {
  573. global $block_object;
  574.  
  575. if ($block < 1 || $block > AD_INSERTER_BLOCKS) $block = 0;
  576.  
  577. if ($ai_last_check == AI_CHECK_INSERTED) return "BLOCK $block INSERTED";
  578. $status = "BLOCK $block FAILED CHECK: ";
  579. $obj = $block_object [$block];
  580. switch ($ai_last_check) {
  581. case AI_CHECK_PAGE_TYPE_FRONT_PAGE: $status .= "ENABLED ON HOMEPAGE"; break;
  582. case AI_CHECK_PAGE_TYPE_STATIC_PAGE: $status .= "ENABLED ON STATIC PAGE"; break;
  583. case AI_CHECK_PAGE_TYPE_POST: $status .= "ENABLED ON POST"; break;
  584. case AI_CHECK_PAGE_TYPE_CATEGORY: $status .= "ENABLED ON CATEGORY"; break;
  585. case AI_CHECK_PAGE_TYPE_SEARCH: $status .= "ENABLED ON SEARCH"; break;
  586. case AI_CHECK_PAGE_TYPE_ARCHIVE: $status .= "ENABLED ON ARCHIVE"; break;
  587. case AI_CHECK_PAGE_TYPE_FEED: $status .= "ENABLED ON FEED"; break;
  588. case AI_CHECK_PAGE_TYPE_404: $status .= "ENABLED ON 404"; break;
  589.  
  590.  
  591. case AI_CHECK_DESKTOP_DEVICES: $status .= "DESKTOP DEVICES"; break;
  592. case AI_CHECK_MOBILE_DEVICES: $status .= "MOBILE DEVICES"; break;
  593. case AI_CHECK_TABLET_DEVICES: $status .= "TABLET DEVICES"; break;
  594. case AI_CHECK_PHONE_DEVICES: $status .= "PHONE DEVICES"; break;
  595. case AI_CHECK_DESKTOP_TABLET_DEVICES: $status .= "DESKTOP TABLET DEVICES"; break;
  596. case AI_CHECK_DESKTOP_PHONE_DEVICES: $status .= "DESKTOP PHONE DEVICES"; break;
  597.  
  598. case AI_CHECK_CATEGORY: $status .= "CATEGORY"; break;
  599. case AI_CHECK_TAG: $status .= "TAG"; break;
  600. case AI_CHECK_ID: $status .= "ID"; break;
  601. case AI_CHECK_URL: $status .= "URL"; break;
  602. case AI_CHECK_URL_PARAMETER: $status .= "URL PARAMETER"; break;
  603. case AI_CHECK_REFERER: $status .= "REFERER"; break;
  604. case AI_CHECK_DATE: $status .= "DATE"; break;
  605. case AI_CHECK_CODE: $status .= "CODE NOT EMPTY"; break;
  606. case AI_CHECK_LOGGED_IN_USER: $status .= "LOGGED-IN USER"; break;
  607. case AI_CHECK_NOT_LOGGED_IN_USER: $status .= "NOT LOGGED-IN USER"; break;
  608. case AI_CHECK_ADMINISTRATOR: $status .= "ADMINISTRATOR"; break;
  609.  
  610. case AI_CHECK_ENABLED_ON_ALL_EXCEPT_ON_SELECTED: $status .= "ENABLED ON ALL EXCEPT ON SELECTED"; break;
  611. case AI_CHECK_ENABLED_ONLY_ON_SELECTED: $status .= "ENABLED ONLY ON SELECTED"; break;
  612. case AI_CHECK_DISABLED_MANUALLY: $status .= "404"; break;
  613.  
  614. case AI_CHECK_MAX_INSERTIONS: $status .= "MAX INSERTIONS " . $obj->get_maximum_insertions (); break;
  615. case AI_CHECK_FILTER: $status .= "FILTER " . $obj->get_call_filter(); break;
  616. case AI_CHECK_PARAGRAPH_COUNTING: $status .= "PARAGRAPH COUNTING"; break;
  617. case AI_CHECK_NUMBER_OF_WORDS: $status .= "NUMBER OF WORDS"; break;
  618. case AI_CHECK_DEBUG_NO_INSERTION: $status .= "DEBUG NO INSERTION"; break;
  619. case AI_CHECK_PARAGRAPH_TAGS: $status .= "PARAGRAPH TAGS"; break;
  620. case AI_CHECK_PARAGRAPHS_WITH_TAGS: $status .= "PARAGRAPHS WITH TAGS"; break;
  621. case AI_CHECK_PARAGRAPHS_AFTER_BLOCKQUOTE: $status .= "PARAGRAPHS AFTER BLOCKQUOTE"; break;
  622. case AI_CHECK_PARAGRAPHS_AFTER_MIN_MAX_WORDS: $status .= "PARAGRAPHS AFTER MIN MAX WORDS"; break;
  623. case AI_CHECK_PARAGRAPHS_AFTER_TEXT: $status .= "PARAGRAPHS AFTER TEXT"; break;
  624. case AI_CHECK_PARAGRAPHS_AFTER_CLEARANCE: $status .= "PARAGRAPHS AFTER CLEARANCE"; break;
  625. case AI_CHECK_PARAGRAPHS_MIN_NUMBER: $status .= "PARAGRAPHS MIN NUMBER"; break;
  626.  
  627. case AI_CHECK_DO_NOT_INSERT: $status .= "PARAGRAPH CLEARANCE"; break;
  628. case AI_CHECK_AD_ABOVE: $status .= "PARAGRAPH CLEARANCE ABOVE"; break;
  629. case AI_CHECK_AD_BELOW: $status .= "PARAGRAPH CLEARANCE BELOW"; break;
  630. case AI_CHECK_SHORTCODE_ATTRIBUTES: $status .= "SHORTCODE ATTRIBUTES"; break;
  631.  
  632. case AI_CHECK_ENABLED: $status .= "ENABLED"; break;
  633. case AI_CHECK_NONE: $status = "BLOCK $block"; break;
  634. default: $status .= "?"; break;
  635. }
  636. $ai_last_check = AI_CHECK_NONE;
  637. return $status;
  638. }
  639.  
  640. function ai_log ($message = "") {
  641. global $ai_last_time, $ai_processing_log;
  642.  
  643. if ($message != "") {
  644. if ($message [strlen ($message) - 1] == "\n") {
  645. ai_log_message (str_replace ("\n", "", $message));
  646. $ai_processing_log []= "";
  647. } else ai_log_message ($message);
  648. } else $ai_processing_log []= "";
  649. $ai_last_time = microtime ();
  650. }
  651.  
  652. function remove_parameters_from_url ($url, $parameter = '') {
  653. if ($parameter == '')
  654. $parameters = array (AI_URL_DEBUG, AI_URL_DEBUG_PROCESSING, AI_URL_DEBUG_BLOCKS, AI_URL_DEBUG_USER, AI_URL_DEBUG_TAGS, AI_URL_DEBUG_POSITIONS, AI_URL_DEBUG_NO_INSERTION); else
  655. $parameters = array ($parameter);
  656.  
  657. foreach ($parameters as $parameter) {
  658. if (stripos ($url, '?'.$parameter.'=') !== false) {
  659. $url = preg_replace ("/".$parameter."=[^&]*/", "", $url);
  660. $url = rtrim (str_replace ('?&', '?', $url), "?");
  661. }
  662. elseif (stripos ($url, "&".$parameter."=") !== false)
  663. $url = preg_replace ("/&".$parameter."=[^&]*/", "", $url);
  664. }
  665.  
  666. return $url;
  667. }
  668.  
  669. function current_url () {
  670. if (isset ($_SERVER ["HTTPS"]) && $_SERVER ["HTTPS"] == "on")
  671. $url = "https://"; else
  672. $url = "http://";
  673. $url .= $_SERVER ['SERVER_NAME'];
  674. if($_SERVER['SERVER_PORT'] != 80)
  675. $url .= ":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  676. $url .= $_SERVER["REQUEST_URI"];
  677. return $url;
  678. }
  679.  
  680. function set_url_parameter ($parameter, $value) {
  681. $url = remove_parameters_from_url (current_url ());
  682.  
  683. if (stripos ($url, $parameter) !== false) {
  684. $url = preg_replace ("/($parameter=[^&]*)/", $parameter .'=' . $value, $url);
  685. } else {
  686. if (strpos ($url, '?') !== false)
  687. $url .= '&' . $parameter .'=' . $value; else
  688. $url .= '?' . $parameter .'=' . $value;
  689. }
  690. return $url;
  691. }
  692.  
  693. function number_of_words (&$content) {
  694. $text = str_replace ("\r", "", $content);
  695. $text = str_replace (array ("\n", " "), " ", $text);
  696. $text = str_replace (" ", " ", $text);
  697. $text = strip_tags ($text);
  698. return count (explode (" ", $text));
  699. }
  700.  
  701. function ai_wp_hook () {
  702. global $ai_wp_data, $ai_db_options_extract;
  703.  
  704. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) ai_log ("WP HOOK START");
  705. set_page_type ();
  706. set_user ();
  707.  
  708. if ($ai_wp_data [AI_WP_PAGE_TYPE] != AI_PT_ADMIN && ($ai_wp_data [AI_WP_USER] & AI_USER_ADMINISTRATOR) != 0 && get_admin_toolbar_debugging ())
  709. add_action ('admin_bar_menu', 'ai_toolbar', 20);
  710.  
  711. $url_debugging = get_remote_debugging () || ($ai_wp_data [AI_WP_USER] & AI_USER_ADMINISTRATOR) != 0;
  712.  
  713. if (!is_admin()) {
  714. if (isset ($_GET [AI_URL_DEBUG]) && $_GET [AI_URL_DEBUG] == 0) {
  715. if (isset ($_COOKIE ['AI_WP_DEBUGGING'])) {
  716. unset ($_COOKIE ['AI_WP_DEBUGGING']);
  717. setcookie ('AI_WP_DEBUGGING', '', time() - (15 * 60), COOKIEPATH);
  718. }
  719. if (isset ($_COOKIE ['AI_WP_DEBUG_BLOCK'])) {
  720. unset ($_COOKIE ['AI_WP_DEBUG_BLOCK']);
  721. setcookie ('AI_WP_DEBUG_BLOCK', '', time() - (15 * 60), COOKIEPATH);
  722. }
  723. } else {
  724. $ai_wp_data [AI_WP_DEBUGGING] = isset ($_COOKIE ['AI_WP_DEBUGGING']) ? $ai_wp_data [AI_WP_DEBUGGING] | ($_COOKIE ['AI_WP_DEBUGGING'] & ~AI_DEBUG_PROCESSING) : $ai_wp_data [AI_WP_DEBUGGING];
  725. $ai_wp_data [AI_WP_DEBUG_BLOCK] = isset ($_COOKIE ['AI_WP_DEBUG_BLOCK']) ? $_COOKIE ['AI_WP_DEBUG_BLOCK'] : 0;
  726.  
  727. if (isset ($_GET [AI_URL_DEBUG_BLOCKS]))
  728. if ($_GET [AI_URL_DEBUG_BLOCKS] && $url_debugging) $ai_wp_data [AI_WP_DEBUGGING] |= AI_DEBUG_BLOCKS; else $ai_wp_data [AI_WP_DEBUGGING] &= ~AI_DEBUG_BLOCKS;
  729.  
  730. if (isset ($_GET [AI_URL_DEBUG_TAGS]))
  731. if ($_GET [AI_URL_DEBUG_TAGS] && $url_debugging) $ai_wp_data [AI_WP_DEBUGGING] |= AI_DEBUG_TAGS; else $ai_wp_data [AI_WP_DEBUGGING] &= ~AI_DEBUG_TAGS;
  732.  
  733. if (isset ($_GET [AI_URL_DEBUG_NO_INSERTION]))
  734. if ($_GET [AI_URL_DEBUG_NO_INSERTION] && $url_debugging) $ai_wp_data [AI_WP_DEBUGGING] |= AI_DEBUG_NO_INSERTION; else $ai_wp_data [AI_WP_DEBUGGING] &= ~AI_DEBUG_NO_INSERTION;
  735.  
  736. if (isset ($_GET [AI_URL_DEBUG_POSITIONS])) {
  737. if ($_GET [AI_URL_DEBUG_POSITIONS] !== '' && $url_debugging) $ai_wp_data [AI_WP_DEBUGGING] |= AI_DEBUG_POSITIONS; else $ai_wp_data [AI_WP_DEBUGGING] &= ~AI_DEBUG_POSITIONS;
  738. if (is_numeric ($_GET [AI_URL_DEBUG_POSITIONS])) $ai_wp_data [AI_WP_DEBUG_BLOCK] = intval ($_GET [AI_URL_DEBUG_POSITIONS]);
  739. if ($ai_wp_data [AI_WP_DEBUG_BLOCK] < 0 || $ai_wp_data [AI_WP_DEBUG_BLOCK] > AD_INSERTER_BLOCKS) $ai_wp_data [AI_WP_DEBUG_BLOCK] = 0;
  740. }
  741.  
  742. if ($ai_wp_data [AI_WP_DEBUGGING] != 0)
  743. setcookie ('AI_WP_DEBUGGING', $ai_wp_data [AI_WP_DEBUGGING], time() + AI_COOKIE_TIME, COOKIEPATH); else
  744. if (isset ($_COOKIE ['AI_WP_DEBUGGING'])) setcookie ('AI_WP_DEBUGGING', '', time() - (15 * 60), COOKIEPATH);
  745.  
  746. if ($ai_wp_data [AI_WP_DEBUG_BLOCK] != 0)
  747. setcookie ('AI_WP_DEBUG_BLOCK', $ai_wp_data [AI_WP_DEBUG_BLOCK], time() + AI_COOKIE_TIME, COOKIEPATH); else
  748. if (isset ($_COOKIE ['AI_WP_DEBUG_BLOCK'])) setcookie ('AI_WP_DEBUG_BLOCK', '', time() - (15 * 60), COOKIEPATH);
  749. }
  750. }
  751.  
  752. $debug_positions = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_POSITIONS) != 0;
  753. $debug_tags_positions = ($ai_wp_data [AI_WP_DEBUGGING] & (AI_DEBUG_POSITIONS | AI_DEBUG_TAGS)) != 0;
  754. $debug_tags_positions_blocks = ($ai_wp_data [AI_WP_DEBUGGING] & (AI_DEBUG_POSITIONS | AI_DEBUG_TAGS | AI_DEBUG_BLOCKS)) != 0;
  755.  
  756. $plugin_priority = get_plugin_priority ();
  757. if (isset ($ai_db_options_extract [CONTENT_HOOK_BLOCKS][$ai_wp_data [AI_WP_PAGE_TYPE]]) && count ($ai_db_options_extract [CONTENT_HOOK_BLOCKS][$ai_wp_data [AI_WP_PAGE_TYPE]]) != 0 || $debug_tags_positions)
  758. add_filter ('the_content', 'ai_content_hook', $plugin_priority);
  759.  
  760. if (isset ($ai_db_options_extract [EXCERPT_HOOK_BLOCKS][$ai_wp_data [AI_WP_PAGE_TYPE]]) && count ($ai_db_options_extract [EXCERPT_HOOK_BLOCKS][$ai_wp_data [AI_WP_PAGE_TYPE]]) != 0 || $debug_tags_positions_blocks)
  761. add_filter ('the_excerpt', 'ai_excerpt_hook', $plugin_priority);
  762.  
  763. if (isset ($ai_db_options_extract [LOOP_START_HOOK_BLOCKS][$ai_wp_data [AI_WP_PAGE_TYPE]]) && count ($ai_db_options_extract [LOOP_START_HOOK_BLOCKS][$ai_wp_data [AI_WP_PAGE_TYPE]]) != 0 || $debug_positions)
  764. add_action ('loop_start', 'ai_loop_start_hook');
  765.  
  766. if (isset ($ai_db_options_extract [LOOP_END_HOOK_BLOCKS][$ai_wp_data [AI_WP_PAGE_TYPE]]) && count ($ai_db_options_extract [LOOP_END_HOOK_BLOCKS][$ai_wp_data [AI_WP_PAGE_TYPE]]) != 0 || $debug_positions)
  767. add_action ('loop_end', 'ai_loop_end_hook');
  768.  
  769. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) ai_log ("WP HOOK END\n");
  770. };
  771.  
  772. function ai_init_hook() {
  773. global $block_object, $ai_wp_data;
  774.  
  775. add_shortcode ('adinserter', 'process_shortcodes');
  776. }
  777.  
  778. function ai_admin_menu_hook () {
  779. global $ai_settings_page;
  780.  
  781. if (is_multisite() && !is_main_site () && !multisite_settings_page_enabled ()) return;
  782.  
  783. $ai_settings_page = add_submenu_page ('options-general.php', AD_INSERTER_NAME.' Options', AD_INSERTER_NAME, 'manage_options', basename(__FILE__), 'ai_settings');
  784. add_action ('admin_enqueue_scripts', 'ai_admin_enqueue_scripts');
  785. }
  786.  
  787.  
  788. function ai_admin_enqueue_scripts ($hook_suffix) {
  789. global $ai_settings_page;
  790.  
  791. if ($hook_suffix == $ai_settings_page) {
  792. wp_enqueue_script ('ai-admin-js', plugins_url ('js/ad-inserter.js', __FILE__), array (
  793. 'jquery',
  794. 'jquery-ui-tabs',
  795. 'jquery-ui-button',
  796. 'jquery-ui-tooltip',
  797. ), AD_INSERTER_VERSION);
  798. wp_enqueue_style ('ai-admin-jquery-ui', plugins_url ('css/jquery-ui-1.10.3.custom.min.css', __FILE__), false, null);
  799. wp_enqueue_style ('ai-admin', plugins_url ('css/ad-inserter.css', __FILE__), false, AD_INSERTER_VERSION);
  800.  
  801. // wp_add_inline_style ('ai-admin', AI_SETTINGS_STYLE);
  802.  
  803. if (AI_SYNTAX_HIGHLIGHTING) {
  804. wp_enqueue_script ('ai-ace', plugins_url ('includes/ace/src-min-noconflict/ace.js', __FILE__ ), array (), AD_INSERTER_VERSION, true);
  805. wp_enqueue_script ('ai-ace-ext-modelist', plugins_url ('includes/ace/src-min-noconflict/ext-modelist.js', __FILE__ ), array (), AD_INSERTER_VERSION, true);
  806. }
  807. }
  808. }
  809.  
  810. function ai_enqueue_scripts_hook () {
  811. global $ai_wp_data;
  812.  
  813. $style = "#wp-admin-bar-ai-toolbar-settings .ab-icon:before {
  814. content: '\\f111';
  815. top: 2px;
  816. color: rgba(240,245,250,.6)!important;
  817. }
  818. #wp-admin-bar-ai-toolbar-settings-default .ab-icon:before {
  819. top: 0px;
  820. }
  821. #wp-admin-bar-ai-toolbar-settings .ab-icon.on:before {
  822. color: #00f200!important;
  823. }
  824. #wp-admin-bar-ai-toolbar-settings-default li, #wp-admin-bar-ai-toolbar-settings-default a,
  825. #wp-admin-bar-ai-toolbar-settings-default li:hover, #wp-admin-bar-ai-toolbar-settings-default a:hover {
  826. border: 1px solid transparent;
  827. }
  828. #wp-admin-bar-ai-toolbar-blocks .ab-icon:before {
  829. content: '\\f135';
  830. }
  831. #wp-admin-bar-ai-toolbar-positions .ab-icon:before {
  832. content: '\\f207';
  833. }
  834. #wp-admin-bar-ai-toolbar-positions-default .ab-icon:before {
  835. content: '\\f522';
  836. }
  837. #wp-admin-bar-ai-toolbar-tags .ab-icon:before {
  838. content: '\\f475';
  839. }
  840. #wp-admin-bar-ai-toolbar-no-insertion .ab-icon:before {
  841. content: '\\f214';
  842. }
  843. #wp-admin-bar-ai-toolbar-processing .ab-icon:before {
  844. content: '\\f464';
  845. }
  846. #wp-admin-bar-ai-toolbar-positions span.up-icon {
  847. padding-top: 2px;
  848. }
  849. #wp-admin-bar-ai-toolbar-positions .up-icon:before {
  850. font: 400 20px/1 dashicons;
  851. }";
  852.  
  853. if ($ai_wp_data [CLIENT_SIDE_DETECTION] || get_remote_debugging () || ($ai_wp_data [AI_WP_USER] & AI_USER_LOGGED_IN) != 0)
  854. wp_enqueue_style ('ai-frontend', plugins_url ('css/dummy.css', __FILE__), false, filemtime (plugin_dir_path (__FILE__ ).'css/dummy.css'));
  855.  
  856. if ($ai_wp_data [CLIENT_SIDE_DETECTION])
  857. wp_add_inline_style ('ai-frontend', get_viewport_css ());
  858.  
  859. if (get_remote_debugging () || ($ai_wp_data [AI_WP_USER] & AI_USER_LOGGED_IN) != 0)
  860. wp_add_inline_style ('ai-frontend', $style);
  861. }
  862.  
  863. function ai_admin_notice_hook () {
  864. global $current_screen, $ai_db_options;
  865.  
  866. // $sidebar_widgets = wp_get_sidebars_widgets();
  867. // $sidebars_with_deprecated_widgets = array ();
  868.  
  869. // foreach ($sidebar_widgets as $sidebar_widget_index => $sidebar_widget) {
  870. // if (is_array ($sidebar_widget))
  871. // foreach ($sidebar_widget as $widget) {
  872. // if (preg_match ("/ai_widget([\d]+)/", $widget, $widget_number)) {
  873. // if (isset ($widget_number [1]) && is_numeric ($widget_number [1])) {
  874. // $is_widget = $ai_db_options [$widget_number [1]][AI_OPTION_DISPLAY_TYPE] == AD_SELECT_WIDGET;
  875. // } else $is_widget = false;
  876. // $sidebar_name = $GLOBALS ['wp_registered_sidebars'][$sidebar_widget_index]['name'];
  877. // if ($is_widget && $sidebar_name != "")
  878. // $sidebars_with_deprecated_widgets [$sidebar_widget_index] = $sidebar_name;
  879. // }
  880. // }
  881. // }
  882.  
  883. // if (!empty ($sidebars_with_deprecated_widgets)) {
  884. // echo "<div class='error' style='padding: 11px 15px; font-size: 14px;'><strong>Warning</strong>: You are using deprecated Ad Inserter widgets in the following sidebars: ",
  885. // implode (", ", $sidebars_with_deprecated_widgets),
  886. // ". Please replace them with the new 'Ad Inserter' code block widget. See <a href='https://wordpress.org/plugins/ad-inserter/faq/' target='_blank'>FAQ</a> for details.</div>";
  887. // }
  888. }
  889.  
  890. function ai_plugin_action_links ($links) {
  891. if (is_multisite() && !is_main_site () && !multisite_settings_page_enabled ()) return $links;
  892.  
  893. $settings_link = '<a href="'.admin_url ('options-general.php?page=ad-inserter.php').'">Settings</a>';
  894. array_unshift ($links, $settings_link);
  895. return $links;
  896. }
  897.  
  898. function ai_set_plugin_meta ($links, $file) {
  899. if ($file == plugin_basename (__FILE__)) {
  900. if (is_multisite() && !is_main_site ()) {
  901. foreach ($links as $index => $link) {
  902. if (stripos ($link, "update") !== false) unset ($links [$index]);
  903. }
  904. }
  905. // if (stripos (AD_INSERTER_NAME, "pro") === false) {
  906. // $new_links = array ('donate' => '<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=LHGZEMRTR7WB4" target="_blank">Donate</a>');
  907. // $links = array_merge ($links, $new_links);
  908. // }
  909. }
  910. return $links;
  911. }
  912.  
  913.  
  914. function current_user_role ($user_role_name = "") {
  915. $role_values = array ("super-admin" => 6, "administrator" => 5, "editor" => 4, "author" => 3, "contributor" => 2, "subscriber" => 1);
  916. global $wp_roles;
  917.  
  918. if ($user_role_name != "") {
  919. return isset ($role_values [$user_role_name]) ? $role_values [$user_role_name] : 0;
  920. }
  921.  
  922. $user_role = 0;
  923. $current_user = wp_get_current_user();
  924. $roles = $current_user->roles;
  925.  
  926. // Fix for empty roles
  927. if (isset ($current_user->caps) && count ($current_user->caps) != 0) {
  928. $caps = $current_user->caps;
  929. foreach ($role_values as $role_name => $role_value) {
  930. if (isset ($caps [$role_name]) && $caps [$role_name]) $roles []= $role_name;
  931. }
  932. }
  933.  
  934. foreach ($roles as $role) {
  935. $current_user_role = isset ($role_values [$role]) ? $role_values [$role] : 0;
  936. if ($current_user_role > $user_role) $user_role = $current_user_role;
  937. }
  938.  
  939. return $user_role;
  940. }
  941.  
  942.  
  943. function ai_current_user_role_ok () {
  944. return current_user_role () >= current_user_role (get_minimum_user_role ());
  945. }
  946.  
  947.  
  948. function ai_add_meta_box_hook() {
  949.  
  950. if (!ai_current_user_role_ok ()) return;
  951.  
  952. if (is_multisite() && !is_main_site () && !multisite_exceptions_enabled ()) return;
  953.  
  954. $screens = array ('post', 'page');
  955.  
  956. foreach ($screens as $screen) {
  957. add_meta_box (
  958. 'adinserter_sectionid',
  959. AD_INSERTER_NAME.' Exceptions',
  960. 'ai_meta_box_callback',
  961. $screen
  962. );
  963. }
  964. }
  965.  
  966. function ai_meta_box_callback ($post) {
  967. global $block_object;
  968.  
  969. // Add an nonce field so we can check for it later.
  970. wp_nonce_field ('adinserter_meta_box', 'adinserter_meta_box_nonce');
  971.  
  972. $post_type = get_post_type ($post);
  973.  
  974. /*
  975. * Use get_post_meta() to retrieve an existing value
  976. * from the database and use the value for the form.
  977. */
  978. $post_meta = get_post_meta ($post->ID, '_adinserter_block_exceptions', true);
  979. $selected_blocks = explode (",", $post_meta);
  980.  
  981. echo '<table>';
  982. echo '<thead style="font-weight: bold;">';
  983. echo ' <td>Block</td>';
  984. echo ' <td style="padding: 0 10px 0 10px;">Name</td>';
  985. echo ' <td style="padding: 0 10px 0 10px;">Automatic Display Type</td>';
  986. echo ' <td style="padding: 0 5px 0 5px;">Posts</td>';
  987. echo ' <td style="padding: 0 5px 0 5px;">Pages</td>';
  988. echo ' <td style="padding: 0 5px 0 5px;">Widget</td>';
  989. echo ' <td style="padding: 0 5px 0 5px;">Shortcode</td>';
  990. echo ' <td style="padding: 0 5px 0 5px;">PHP</td>';
  991. echo ' <td style="padding: 0 10px 0 10px;">Default</td>';
  992. echo ' <td style="padding: 0 10px 0 10px;">For this ', $post_type, '</td>';
  993. echo '</thead>';
  994. echo '<tbody>';
  995. $rows = 0;
  996. for ($block = 1; $block <= AD_INSERTER_BLOCKS; $block ++) {
  997. $obj = $block_object [$block];
  998.  
  999. if ($post_type == 'post') {
  1000. $enabled_on_text = $obj->get_ad_enabled_on_which_posts ();
  1001. $general_enabled = $obj->get_display_settings_post();
  1002. } else {
  1003. $enabled_on_text = $obj->get_ad_enabled_on_which_pages ();
  1004. $general_enabled = $obj->get_display_settings_page();
  1005. }
  1006.  
  1007. $individual_option_enabled = $general_enabled && ($enabled_on_text == AD_ENABLED_ON_ALL_EXCEPT_ON_SELECTED || $enabled_on_text == AD_ENABLED_ONLY_ON_SELECTED);
  1008. $individual_text_enabled = $enabled_on_text == AD_ENABLED_ON_ALL_EXCEPT_ON_SELECTED;
  1009.  
  1010. $display_type = $obj->get_display_type();
  1011. if ($rows % 2 != 0) $background = "#F0F0F0"; else $background = "#FFF";
  1012. echo '<tr style="background: ', $background, ';">';
  1013. echo ' <td style="text-align: right;">', $obj->number, '</td>';
  1014. echo ' <td style="padding: 0 10px 0 10px;">', $obj->get_ad_name(), '</td>';
  1015. echo ' <td style="padding: 0 10px 0 10px;">', $display_type, '</td>';
  1016.  
  1017. echo ' <td style="padding: 0 10px 0 10px; text-align: center;">';
  1018. if ($obj->get_display_settings_post ()) echo '&check;';
  1019. echo ' </td>';
  1020. echo ' <td style="padding: 0 10px 0 10px; text-align: center;">';
  1021. if ($obj->get_display_settings_page ()) echo '&check;';
  1022. echo ' </td>';
  1023. echo ' <td style="padding: 0 10px 0 10px; text-align: center;">';
  1024. if ($obj->get_enable_widget ()) echo '&check;';
  1025. echo ' </td>';
  1026. echo ' <td style="padding: 0 10px 0 10px; text-align: center;">';
  1027. if ($obj->get_enable_manual ()) echo '&check;';
  1028. echo ' </td>';
  1029. echo ' <td style="padding: 0 10px 0 10px; text-align: center;">';
  1030. if ($obj->get_enable_php_call ()) echo '&check;';
  1031. echo ' </td>';
  1032.  
  1033. echo ' <td style="padding: 0 10px 0 10px; text-align: left;">';
  1034.  
  1035. if ($individual_option_enabled) {
  1036. if ($individual_text_enabled) echo 'Enabled'; else echo 'Disabled';
  1037. } else {
  1038. if ($general_enabled) echo 'Enabled on all ', $post_type, 's'; else
  1039. echo 'Disabled on all ', $post_type, 's';
  1040. }
  1041. echo ' </td>';
  1042.  
  1043. echo ' <td style="padding: 0 10px 0 10px; text-align: left;">';
  1044. if ($individual_option_enabled)
  1045. echo '<input type="checkbox" style="border-radius: 5px;" name="adinserter_selected_block_', $block, '" id="ai-selected-block-', $block, '" value="1"', in_array ($block, $selected_blocks) ? ' checked': '', ' />';
  1046.  
  1047. echo '<label for="ai-selected-block-', $block, '">';
  1048. if ($individual_option_enabled) {
  1049. if (!$individual_text_enabled) echo 'Enabled'; else echo 'Disabled';
  1050. }
  1051. echo '</label>';
  1052. echo ' </td>';
  1053.  
  1054. echo '</tr>';
  1055. $rows ++;
  1056. }
  1057.  
  1058. echo '</tbody>';
  1059. echo '</table>';
  1060.  
  1061. echo '<p>Default behavior for all code blocks for ', $post_type, 's (enabled or disabled) can be configured on <a href="', admin_url ('options-general.php?page=ad-inserter.php'), '" target="_blank">', AD_INSERTER_NAME, ' Settings page</a>. Here you can configure exceptions for this ', $post_type, '.</p>';
  1062. }
  1063.  
  1064. function ai_save_meta_box_data_hook ($post_id) {
  1065. // Check if our nonce is set.
  1066. if (!isset ($_POST ['adinserter_meta_box_nonce'])) return;
  1067.  
  1068. // Verify that the nonce is valid.
  1069. if (!wp_verify_nonce ($_POST ['adinserter_meta_box_nonce'], 'adinserter_meta_box')) return;
  1070.  
  1071. // If this is an autosave, our form has not been submitted, so we don't want to do anything.
  1072. if (defined ('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
  1073.  
  1074. // Check the user's permissions.
  1075.  
  1076. if (isset ($_POST ['post_type'])) {
  1077. if ($_POST ['post_type'] == 'page') {
  1078. if (!current_user_can ('edit_page', $post_id)) return;
  1079. } else {
  1080. if (!current_user_can ('edit_post', $post_id)) return;
  1081. }
  1082. }
  1083.  
  1084. /* OK, it's safe for us to save the data now. */
  1085.  
  1086. $selected = array ();
  1087. for ($block = 1; $block <= AD_INSERTER_BLOCKS; $block ++) {
  1088. $option_name = 'adinserter_selected_block_' . $block;
  1089. if (isset ($_POST [$option_name]) && $_POST [$option_name]) $selected []= $block;
  1090. }
  1091.  
  1092. // Update the meta field in the database.
  1093. update_post_meta ($post_id, '_adinserter_block_exceptions', implode (",", $selected));
  1094. }
  1095.  
  1096. function ai_widgets_init_hook () {
  1097. if (is_multisite() && !is_main_site () && !multisite_widgets_enabled ()) return;
  1098. register_widget ('ai_widget');
  1099. }
  1100.  
  1101. function ai_wp_head_hook () {
  1102. global $block_object, $ai_wp_data;
  1103.  
  1104. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) ai_log ("HEAD HOOK START");
  1105.  
  1106. $ai_wp_data [AI_CONTEXT] = AI_CONTEXT_NONE;
  1107.  
  1108. $obj = $block_object [AI_HEADER_OPTION_NAME];
  1109. $obj->clear_code_cache ();
  1110.  
  1111. if (!$obj->check_server_side_detection ()) return;
  1112.  
  1113. if ($obj->get_enable_manual ()) {
  1114. if ($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_404 && !$obj->get_enable_404()) return;
  1115. echo $obj->ai_getCode ();
  1116. }
  1117.  
  1118. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) ai_log ("HEAD HOOK END\n");
  1119. }
  1120.  
  1121. function ai_wp_footer_hook () {
  1122. global $block_object, $ai_wp_data;
  1123.  
  1124. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) ai_log ("FOOTER HOOK START");
  1125.  
  1126. $ai_wp_data [AI_CONTEXT] = AI_CONTEXT_FOOTER;
  1127.  
  1128. $obj = $block_object [AI_FOOTER_OPTION_NAME];
  1129. $obj->clear_code_cache ();
  1130.  
  1131. if (!$obj->check_server_side_detection ()) return;
  1132.  
  1133. if ($obj->get_enable_manual ()) {
  1134. if ($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_404 && !$obj->get_enable_404()) return;
  1135. echo $obj->ai_getCode ();
  1136. }
  1137.  
  1138. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) ai_log ("FOOTER HOOK END\n");
  1139. }
  1140.  
  1141. function ai_write_debug_info ($write_processing_log = false) {
  1142. global $block_object, $ai_last_time, $ai_processing_log, $ai_db_options_extract, $ai_wp_data, $ai_db_options;
  1143.  
  1144. echo sprintf ("%-25s%s\n\n", AD_INSERTER_NAME, AD_INSERTER_VERSION);
  1145. echo "GENERATED (WP time): ", date ("Y-m-d H:i:s", time() + get_option ('gmt_offset') * 3600), "\n";
  1146. echo "GENERATED (Server time): ", date ("Y-m-d H:i:s", time()), "\n";
  1147.  
  1148. echo "SETTINGS: ";
  1149. if (isset ($ai_db_options ['global']['VERSION']))
  1150. echo (int) ($ai_db_options ['global']['VERSION'][0].$ai_db_options ['global']['VERSION'][1]), '.',
  1151. (int) ($ai_db_options ['global']['VERSION'][2].$ai_db_options ['global']['VERSION'][3]), '.',
  1152. (int) ($ai_db_options ['global']['VERSION'][4].$ai_db_options ['global']['VERSION'][5]);
  1153.  
  1154. echo "\n";
  1155. echo "SETTINGS TIMESTAMP: ";
  1156. echo isset ($ai_db_options ['global']['TIMESTAMP']) ? date ("Y-m-d H:i:s", $ai_db_options ['global']['TIMESTAMP'] + get_option ('gmt_offset') * 3600) : "", "\n";
  1157. echo "MULTISITE: ", is_multisite() ? "YES" : "NO", "\n";
  1158. if (is_multisite()) {
  1159. echo "MAIN SITE: ", is_main_site () ? "YES" : "NO", "\n";
  1160. }
  1161.  
  1162. echo "USER: ";
  1163. if (($ai_wp_data [AI_WP_USER] & AI_USER_LOGGED_IN) == AI_USER_LOGGED_IN) echo "LOGGED-IN "; else echo "NOT LOGGED-IN ";
  1164. if (($ai_wp_data [AI_WP_USER] & AI_USER_ADMINISTRATOR) == AI_USER_ADMINISTRATOR) echo "ADMINISTRATOR";
  1165. echo "\n";
  1166. echo "PAGE TYPE: ";
  1167. switch ($ai_wp_data [AI_WP_PAGE_TYPE]) {
  1168. case AI_PT_STATIC: echo "STATIC PAGE"; break;
  1169. case AI_PT_POST: echo "POST"; break;
  1170. case AI_PT_HOMEPAGE: echo "HOMEPAGE"; break;
  1171. case AI_PT_CATEGORY: echo "CATEGORY PAGE"; break;
  1172. case AI_PT_ARCHIVE: echo "ARCHIVE PAGE"; break;
  1173. case AI_PT_SEARCH: echo "SEARCH PAGE"; break;
  1174. case AI_PT_404: echo "404 PAGE"; break;
  1175. case AI_PT_FEED: echo "FEED"; break;
  1176. default: echo "?"; break;
  1177. }
  1178. echo "\n";
  1179. echo 'ID: ', get_the_ID(), "\n";
  1180. echo 'URL: ', $ai_wp_data [AI_WP_URL], "\n";
  1181. echo 'REFERER: ', isset ($_SERVER['HTTP_REFERER']) ? remove_parameters_from_url ($_SERVER['HTTP_REFERER']) : "", "\n";
  1182. echo 'CLIENT-SIDE DETECTION: ', $ai_wp_data [CLIENT_SIDE_DETECTION] ? 'USED' : "NOT USED", "\n";
  1183. echo 'SERVER-SIDE DETECTION: ', $ai_wp_data [SERVER_SIDE_DETECTION] ? 'USED' : "NOT USED", "\n";
  1184. if ($ai_wp_data [SERVER_SIDE_DETECTION]) {
  1185. echo 'SERVER-SIDE DEVICE: ';
  1186. if (AI_DESKTOP) echo "DESKTOP\n";
  1187. elseif (AI_TABLET) echo "TABLET\n";
  1188. elseif (AI_PHONE) echo "PHONE\n";
  1189. else echo "?\n";
  1190. }
  1191.  
  1192. echo "\n";
  1193.  
  1194. $default = new ai_Block (1);
  1195.  
  1196. echo "BLOCK SETTINGS Po Pa Hp Cp Ap Sp Fe 404 Wi Sh PHP\n";
  1197. for ($block = 1; $block <= AD_INSERTER_BLOCKS; $block ++) {
  1198. $obj = $block_object [$block];
  1199.  
  1200. $settings = "";
  1201. $display_settings = '';
  1202. $alignment_settings = '';
  1203. $default_settings = true;
  1204. $display_type = '';
  1205. foreach (array_keys ($default->wp_options) as $key){
  1206. switch ($key) {
  1207. case AI_OPTION_CODE:
  1208. case AI_OPTION_NAME:
  1209. continue 2;
  1210. case AI_OPTION_DISPLAY_ON_PAGES:
  1211. case AI_OPTION_DISPLAY_ON_POSTS:
  1212. case AI_OPTION_DISPLAY_ON_HOMEPAGE:
  1213. case AI_OPTION_DISPLAY_ON_CATEGORY_PAGES:
  1214. case AI_OPTION_DISPLAY_ON_SEARCH_PAGES:
  1215. case AI_OPTION_DISPLAY_ON_ARCHIVE_PAGES:
  1216. case AI_OPTION_ENABLE_FEED:
  1217. case AI_OPTION_ENABLE_404:
  1218. case AI_OPTION_ENABLE_MANUAL:
  1219. case AI_OPTION_ENABLE_WIDGET:
  1220. case AI_OPTION_ENABLE_PHP_CALL:
  1221. if ($obj->wp_options [$key] != $default->wp_options [$key]) $default_settings = false;
  1222. continue 2;
  1223. }
  1224. if ($obj->wp_options [$key] != $default->wp_options [$key]) {
  1225. $default_settings = false;
  1226. switch ($key) {
  1227. case AI_OPTION_DISPLAY_TYPE:
  1228. $display_settings = $obj->wp_options [$key];
  1229. break;
  1230. case AI_OPTION_ALIGNMENT_TYPE:
  1231. $alignment_settings = " " . $obj->wp_options [$key];
  1232. break;
  1233. case AI_OPTION_PARAGRAPH_TEXT:
  1234. case AI_OPTION_AVOID_TEXT_ABOVE:
  1235. case AI_OPTION_AVOID_TEXT_BELOW:
  1236. if ($write_processing_log)
  1237. $settings .= " " . $key . ": " . ai_log_filter_content (html_entity_decode ($obj->wp_options [$key])); else
  1238. $settings .= " " . $key . ": " . $obj->wp_options [$key];
  1239. break;
  1240. default:
  1241. $settings .= " " . $key . ": " . $obj->wp_options [$key];
  1242. break;
  1243. }
  1244. } else
  1245. switch ($key) {
  1246. case AI_OPTION_DISPLAY_TYPE:
  1247. $display_settings = $obj->wp_options [$key];
  1248. break;
  1249. case AI_OPTION_ALIGNMENT_TYPE:
  1250. $alignment_settings = " " . $obj->wp_options [$key];
  1251. break;
  1252. }
  1253. }
  1254. if ($default_settings && $settings == '') continue;
  1255. $settings = $display_settings . $alignment_settings . $settings;
  1256.  
  1257. echo sprintf ("%2d %-21s ", $block, substr ($obj->get_ad_name(), 0, 21));
  1258.  
  1259. echo $obj->get_display_settings_post() ? "o" : ".", " ";
  1260. echo $obj->get_display_settings_page() ? "o" : ".", " ";
  1261. echo $obj->get_display_settings_home() ? "o" : ".", " ";
  1262. echo $obj->get_display_settings_category() ? "o" : ".", " ";
  1263. echo $obj->get_display_settings_archive() ? "o" : ".", " ";
  1264. echo $obj->get_display_settings_search() ? "o" : ".", " ";
  1265. echo $obj->get_enable_feed() ? "o" : ".", " ";
  1266. echo $obj->get_enable_404() ? "o" : ".", " ";
  1267. echo $obj->get_enable_widget() ? "x" : ".", " ";
  1268. echo $obj->get_enable_manual() ? "x" : ".", " ";
  1269. echo $obj->get_enable_php_call() ? "x" : ".", " ";
  1270.  
  1271. echo $settings, "\n";
  1272. }
  1273. echo "\n";
  1274.  
  1275. echo "TOTAL BLOCKS\n";
  1276. if (count ($ai_db_options_extract [CONTENT_HOOK_BLOCKS][AI_PT_ANY]))
  1277. echo "CONTENT HOOK BLOCKS: ", implode (", ", $ai_db_options_extract [CONTENT_HOOK_BLOCKS][AI_PT_ANY]), "\n";
  1278. if (count ($ai_db_options_extract [EXCERPT_HOOK_BLOCKS][AI_PT_ANY]))
  1279. echo "EXCERPT HOOK BLOCKS: ", implode (", ", $ai_db_options_extract [EXCERPT_HOOK_BLOCKS][AI_PT_ANY]), "\n";
  1280. if (count ($ai_db_options_extract [LOOP_START_HOOK_BLOCKS][AI_PT_ANY]))
  1281. echo "LOOP START HOOK BLOCKS: ", implode (", ", $ai_db_options_extract [LOOP_START_HOOK_BLOCKS][AI_PT_ANY]), "\n";
  1282. if (count ($ai_db_options_extract [LOOP_END_HOOK_BLOCKS][AI_PT_ANY]))
  1283. echo "LOOP END HOOK BLOCKS: ", implode (", ", $ai_db_options_extract [LOOP_END_HOOK_BLOCKS][AI_PT_ANY]), "\n";
  1284.  
  1285.  
  1286. echo "\nPAGE TYPE BLOCKS\n";
  1287. if (count ($ai_db_options_extract [CONTENT_HOOK_BLOCKS][$ai_wp_data [AI_WP_PAGE_TYPE]]))
  1288. echo "CONTENT HOOK BLOCKS: ", implode (", ", $ai_db_options_extract [CONTENT_HOOK_BLOCKS][$ai_wp_data [AI_WP_PAGE_TYPE]]), "\n";
  1289. if (count ($ai_db_options_extract [EXCERPT_HOOK_BLOCKS][$ai_wp_data [AI_WP_PAGE_TYPE]]))
  1290. echo "EXCERPT HOOK BLOCKS: ", implode (", ", $ai_db_options_extract [EXCERPT_HOOK_BLOCKS][$ai_wp_data [AI_WP_PAGE_TYPE]]), "\n";
  1291. if (count ($ai_db_options_extract [LOOP_START_HOOK_BLOCKS][$ai_wp_data [AI_WP_PAGE_TYPE]]))
  1292. echo "LOOP START HOOK BLOCKS: ", implode (", ", $ai_db_options_extract [LOOP_START_HOOK_BLOCKS][$ai_wp_data [AI_WP_PAGE_TYPE]]), "\n";
  1293. if (count ($ai_db_options_extract [LOOP_END_HOOK_BLOCKS][$ai_wp_data [AI_WP_PAGE_TYPE]]))
  1294. echo "LOOP END HOOK BLOCKS: ", implode (", ", $ai_db_options_extract [LOOP_END_HOOK_BLOCKS][$ai_wp_data [AI_WP_PAGE_TYPE]]), "\n";
  1295.  
  1296. if ($write_processing_log) {
  1297. echo "\nTIME EVENT\n";
  1298. echo "======================================\n";
  1299.  
  1300. foreach ($ai_processing_log as $log_line) {
  1301. echo $log_line, "\n";
  1302. }
  1303.  
  1304. echo "PHP: ", phpversion(), "\n";
  1305. global $wp_version;
  1306. echo "Wordpress: ", $wp_version, "\n";
  1307. $current_theme = wp_get_theme();
  1308.  
  1309. echo "Current Theme: ", $current_theme->get ('Name') . " " . $current_theme->get ('Version'), "\n";
  1310. echo "\n";
  1311. echo "A INSTALLED PLUGINS\n";
  1312. echo "======================================\n";
  1313.  
  1314. if ( ! function_exists( 'get_plugins' ) ) {
  1315. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  1316. }
  1317. $all_plugins = get_plugins();
  1318. $active_plugins = get_option ('active_plugins');
  1319. foreach ($all_plugins as $plugin_path => $plugin) {
  1320. echo in_array ($plugin_path, $active_plugins) ? '* ' : ' ', html_entity_decode ($plugin ["Name"]), ' ', $plugin ["Version"], "\n";
  1321. }
  1322. }
  1323. }
  1324.  
  1325. function ai_shutdown_hook () {
  1326. global $ai_wp_data;
  1327.  
  1328. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) == 0 || !(get_remote_debugging () || ($ai_wp_data [AI_WP_USER] & AI_USER_LOGGED_IN) != 0)) return;
  1329. echo "\n<!--\n\n";
  1330. ai_write_debug_info (true);
  1331. echo "\n-->\n";
  1332. }
  1333.  
  1334. function ai_check_multisite_options (&$multisite_options) {
  1335. if (!isset ($multisite_options ['MULTISITE_SETTINGS_PAGE'])) $multisite_options ['MULTISITE_SETTINGS_PAGE'] = DEFAULT_MULTISITE_SETTINGS_PAGE;
  1336. if (!isset ($multisite_options ['MULTISITE_WIDGETS'])) $multisite_options ['MULTISITE_WIDGETS'] = DEFAULT_MULTISITE_WIDGETS;
  1337. if (!isset ($multisite_options ['MULTISITE_EXCEPTIONS'])) $multisite_options ['MULTISITE_EXCEPTIONS'] = DEFAULT_MULTISITE_EXCEPTIONS;
  1338. }
  1339.  
  1340. function ai_check_plugin_options ($plugin_options = array ()) {
  1341. global $version_string;
  1342.  
  1343. $plugin_options ['VERSION'] = $version_string;
  1344.  
  1345. if (!isset ($plugin_options ['SYNTAX_HIGHLIGHTER_THEME'])) $plugin_options ['SYNTAX_HIGHLIGHTER_THEME'] = DEFAULT_SYNTAX_HIGHLIGHTER_THEME;
  1346.  
  1347. if (!isset ($plugin_options ['BLOCK_CLASS_NAME']) ||
  1348. $plugin_options ['BLOCK_CLASS_NAME'] == '') $plugin_options ['BLOCK_CLASS_NAME'] = DEFAULT_BLOCK_CLASS_NAME;
  1349.  
  1350. if (!isset ($plugin_options ['MINIMUM_USER_ROLE'])) $plugin_options ['MINIMUM_USER_ROLE'] = DEFAULT_MINIMUM_USER_ROLE;
  1351.  
  1352. if (!isset ($plugin_options ['PLUGIN_PRIORITY'])) $plugin_options ['PLUGIN_PRIORITY'] = DEFAULT_PLUGIN_PRIORITY;
  1353. $plugin_priority = $plugin_options ['PLUGIN_PRIORITY'];
  1354. if (!is_numeric ($plugin_priority)) {
  1355. $plugin_priority = DEFAULT_PLUGIN_PRIORITY;
  1356. }
  1357. $plugin_priority = intval ($plugin_priority);
  1358. if ($plugin_priority < 0) {
  1359. $plugin_priority = 0;
  1360. }
  1361. if ($plugin_priority > 999999) {
  1362. $plugin_priority = 999999;
  1363. }
  1364. $plugin_options ['PLUGIN_PRIORITY'] = $plugin_priority;
  1365.  
  1366. if (!isset ($plugin_options ['ADMIN_TOOLBAR_DEBUGGING'])) $plugin_options ['ADMIN_TOOLBAR_DEBUGGING'] = DEFAULT_ADMIN_TOOLBAR_DEBUGGING;
  1367. if (!isset ($plugin_options ['REMOTE_DEBUGGING'])) $plugin_options ['REMOTE_DEBUGGING'] = DEFAULT_REMOTE_DEBUGGING;
  1368. if (!isset ($plugin_options ['JAVASCRIPT_DEBUGGING'])) $plugin_options ['JAVASCRIPT_DEBUGGING'] = DEFAULT_JAVASCRIPT_DEBUGGING;
  1369.  
  1370. for ($viewport = 1; $viewport <= AD_INSERTER_VIEWPORTS; $viewport ++) {
  1371. $viewport_name_option_name = 'VIEWPORT_NAME_' . $viewport;
  1372. $viewport_width_option_name = 'VIEWPORT_WIDTH_' . $viewport;
  1373.  
  1374. if (!isset ($plugin_options [$viewport_name_option_name])) $plugin_options [$viewport_name_option_name] =
  1375. defined ("DEFAULT_VIEWPORT_NAME_" . $viewport) ? constant ("DEFAULT_VIEWPORT_NAME_" . $viewport) : "";
  1376.  
  1377. if ($viewport == 1 && $plugin_options [$viewport_name_option_name] == '')
  1378. $plugin_options [$viewport_name_option_name] = constant ("DEFAULT_VIEWPORT_NAME_1");
  1379.  
  1380. if ($plugin_options [$viewport_name_option_name] != '') {
  1381. if (!isset ($plugin_options [$viewport_width_option_name])) $plugin_options [$viewport_width_option_name] =
  1382. defined ("DEFAULT_VIEWPORT_WIDTH_" . $viewport) ? constant ("DEFAULT_VIEWPORT_WIDTH_" . $viewport) : 0;
  1383.  
  1384. $viewport_width = $plugin_options [$viewport_width_option_name];
  1385.  
  1386. if ($viewport > 1) {
  1387. $previous_viewport_option_width = $plugin_options ['VIEWPORT_WIDTH_' . ($viewport - 1)];
  1388. }
  1389.  
  1390. if (!is_numeric ($viewport_width)) {
  1391. if ($viewport == 1)
  1392. $viewport_width = constant ("DEFAULT_VIEWPORT_WIDTH_1"); else
  1393. $viewport_width = $previous_viewport_option_width - 1;
  1394.  
  1395. }
  1396. if ($viewport_width > 9999) {
  1397. $viewport_width = 9999;
  1398. }
  1399.  
  1400. if ($viewport > 1) {
  1401. if ($viewport_width >= $previous_viewport_option_width)
  1402. $viewport_width = $previous_viewport_option_width - 1;
  1403. }
  1404.  
  1405. $viewport_width = intval ($viewport_width);
  1406. if ($viewport_width < 0) {
  1407. $viewport_width = 0;
  1408. }
  1409.  
  1410. $plugin_options [$viewport_width_option_name] = $viewport_width;
  1411. } else $plugin_options [$viewport_width_option_name] = '';
  1412. }
  1413.  
  1414. return ($plugin_options);
  1415. }
  1416.  
  1417. function option_stripslashes (&$options) {
  1418. if (is_array ($options)) {
  1419. foreach ($options as $key => $option) {
  1420. option_stripslashes ($options [$key]);
  1421. }
  1422. } else if (is_string ($options)) $options = stripslashes ($options);
  1423. }
  1424.  
  1425. // Deprecated
  1426. function ai_get_option ($option_name) {
  1427. $options = get_option ($option_name);
  1428. option_stripslashes ($options);
  1429. return ($options);
  1430. }
  1431.  
  1432. function ai_load_options () {
  1433. global $ai_db_options, $ai_db_options_multisite, $ai_wp_data;
  1434.  
  1435. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) ai_log ("LOAD OPTIONS START");
  1436.  
  1437. $ai_db_options = get_option (WP_OPTION_NAME);
  1438. option_stripslashes ($ai_db_options);
  1439.  
  1440. if (is_multisite()) {
  1441. $ai_db_options_multisite = get_site_option (WP_OPTION_NAME);
  1442. option_stripslashes ($ai_db_options_multisite);
  1443. }
  1444.  
  1445. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) ai_log ("LOAD OPTIONS END");
  1446. }
  1447.  
  1448. function get_viewport_css () {
  1449. global $ai_db_options;
  1450.  
  1451. if (isset ($ai_db_options [AI_GLOBAL_OPTION_NAME])) $plugin_db_options = $ai_db_options [AI_GLOBAL_OPTION_NAME]; else $plugin_db_options = '';
  1452. if (!$plugin_db_options) $plugin_db_options = get_option (AD_OPTIONS); // Old options
  1453.  
  1454. if (!isset ($plugin_db_options ['VIEWPORT_CSS'])) $plugin_db_options ['VIEWPORT_CSS'] = generate_viewport_css ();
  1455.  
  1456. return ($plugin_db_options ['VIEWPORT_CSS']);
  1457. }
  1458.  
  1459. function get_syntax_highlighter_theme () {
  1460. global $ai_db_options;
  1461.  
  1462. if (isset ($ai_db_options [AI_GLOBAL_OPTION_NAME])) $plugin_db_options = $ai_db_options [AI_GLOBAL_OPTION_NAME]; else $plugin_db_options = '';
  1463. if (!$plugin_db_options) $plugin_db_options = get_option (AD_OPTIONS);
  1464.  
  1465. if (!isset ($plugin_db_options ['SYNTAX_HIGHLIGHTER_THEME']) || $plugin_db_options ['SYNTAX_HIGHLIGHTER_THEME'] == '') {
  1466. $plugin_db_options ['SYNTAX_HIGHLIGHTER_THEME'] = DEFAULT_SYNTAX_HIGHLIGHTER_THEME;
  1467. }
  1468.  
  1469. return ($plugin_db_options ['SYNTAX_HIGHLIGHTER_THEME']);
  1470. }
  1471.  
  1472. function get_block_class_name () {
  1473. global $ai_db_options;
  1474.  
  1475. if (isset ($ai_db_options [AI_GLOBAL_OPTION_NAME])) $plugin_db_options = $ai_db_options [AI_GLOBAL_OPTION_NAME]; else $plugin_db_options = '';
  1476. if (!$plugin_db_options) $plugin_db_options = get_option (AD_OPTIONS);
  1477.  
  1478. if (!isset ($plugin_db_options ['BLOCK_CLASS_NAME']) || $plugin_db_options ['BLOCK_CLASS_NAME'] == '') {
  1479. $plugin_db_options ['BLOCK_CLASS_NAME'] = DEFAULT_BLOCK_CLASS_NAME;
  1480. }
  1481.  
  1482. return ($plugin_db_options ['BLOCK_CLASS_NAME']);
  1483. }
  1484.  
  1485. function get_minimum_user_role () {
  1486. global $ai_db_options;
  1487.  
  1488. if (isset ($ai_db_options [AI_GLOBAL_OPTION_NAME])) $plugin_db_options = $ai_db_options [AI_GLOBAL_OPTION_NAME]; else $plugin_db_options = '';
  1489. if (!$plugin_db_options) $plugin_db_options = get_option (AD_OPTIONS);
  1490.  
  1491. if (!isset ($plugin_db_options ['MINIMUM_USER_ROLE']) || $plugin_db_options ['MINIMUM_USER_ROLE'] == '') {
  1492. $plugin_db_options ['MINIMUM_USER_ROLE'] = DEFAULT_MINIMUM_USER_ROLE;
  1493. }
  1494.  
  1495. return ($plugin_db_options ['MINIMUM_USER_ROLE']);
  1496. }
  1497.  
  1498. function get_plugin_priority () {
  1499. global $ai_db_options;
  1500.  
  1501. if (isset ($ai_db_options [AI_GLOBAL_OPTION_NAME])) $plugin_db_options = $ai_db_options [AI_GLOBAL_OPTION_NAME]; else $plugin_db_options = '';
  1502. if (!$plugin_db_options) $plugin_db_options = get_option (AD_OPTIONS);
  1503.  
  1504. if (!isset ($plugin_db_options ['PLUGIN_PRIORITY']) || $plugin_db_options ['PLUGIN_PRIORITY'] == '') {
  1505. $plugin_db_options ['PLUGIN_PRIORITY'] = DEFAULT_PLUGIN_PRIORITY;
  1506. }
  1507.  
  1508. return ($plugin_db_options ['PLUGIN_PRIORITY']);
  1509. }
  1510.  
  1511. function get_admin_toolbar_debugging () {
  1512. global $ai_db_options;
  1513.  
  1514. if (isset ($ai_db_options [AI_GLOBAL_OPTION_NAME])) $plugin_db_options = $ai_db_options [AI_GLOBAL_OPTION_NAME]; else $plugin_db_options = '';
  1515.  
  1516. if (!isset ($plugin_db_options ['ADMIN_TOOLBAR_DEBUGGING']) || $plugin_db_options ['ADMIN_TOOLBAR_DEBUGGING'] == '') {
  1517. $plugin_db_options ['ADMIN_TOOLBAR_DEBUGGING'] = DEFAULT_ADMIN_TOOLBAR_DEBUGGING;
  1518. }
  1519.  
  1520. return ($plugin_db_options ['ADMIN_TOOLBAR_DEBUGGING']);
  1521. }
  1522.  
  1523. function get_remote_debugging () {
  1524. global $ai_db_options;
  1525.  
  1526. if (isset ($ai_db_options [AI_GLOBAL_OPTION_NAME])) $plugin_db_options = $ai_db_options [AI_GLOBAL_OPTION_NAME]; else $plugin_db_options = '';
  1527.  
  1528. if (!isset ($plugin_db_options ['REMOTE_DEBUGGING']) || $plugin_db_options ['REMOTE_DEBUGGING'] == '') {
  1529. $plugin_db_options ['REMOTE_DEBUGGING'] = DEFAULT_REMOTE_DEBUGGING;
  1530. }
  1531.  
  1532. return ($plugin_db_options ['REMOTE_DEBUGGING']);
  1533. }
  1534.  
  1535. function get_javascript_debugging () {
  1536. global $ai_db_options;
  1537.  
  1538. if (isset ($ai_db_options [AI_GLOBAL_OPTION_NAME])) $plugin_db_options = $ai_db_options [AI_GLOBAL_OPTION_NAME]; else $plugin_db_options = '';
  1539.  
  1540. if (!isset ($plugin_db_options ['JAVASCRIPT_DEBUGGING']) || $plugin_db_options ['JAVASCRIPT_DEBUGGING'] == '') {
  1541. $plugin_db_options ['JAVASCRIPT_DEBUGGING'] = DEFAULT_JAVASCRIPT_DEBUGGING;
  1542. }
  1543.  
  1544. return ($plugin_db_options ['JAVASCRIPT_DEBUGGING']);
  1545. }
  1546.  
  1547. function multisite_settings_page_enabled () {
  1548. global $ai_db_options_multisite;
  1549.  
  1550. if (is_multisite()) {
  1551. if (!isset ($ai_db_options_multisite ['MULTISITE_SETTINGS_PAGE']) || $ai_db_options_multisite ['MULTISITE_SETTINGS_PAGE'] == '') {
  1552. $ai_db_options_multisite ['MULTISITE_SETTINGS_PAGE'] = DEFAULT_MULTISITE_SETTINGS_PAGE;
  1553. }
  1554.  
  1555. return ($ai_db_options_multisite ['MULTISITE_SETTINGS_PAGE']);
  1556. }
  1557.  
  1558. return DEFAULT_MULTISITE_SETTINGS_PAGE;
  1559. }
  1560.  
  1561. function multisite_widgets_enabled () {
  1562. global $ai_db_options_multisite;
  1563.  
  1564. if (is_multisite()) {
  1565. if (!isset ($ai_db_options_multisite ['MULTISITE_WIDGETS']) || $ai_db_options_multisite ['MULTISITE_WIDGETS'] == '') {
  1566. $ai_db_options_multisite ['MULTISITE_WIDGETS'] = DEFAULT_MULTISITE_WIDGETS;
  1567. }
  1568.  
  1569. return ($ai_db_options_multisite ['MULTISITE_WIDGETS']);
  1570. }
  1571.  
  1572. return DEFAULT_MULTISITE_WIDGETS;
  1573. }
  1574.  
  1575. function multisite_exceptions_enabled () {
  1576. global $ai_db_options_multisite;
  1577.  
  1578. if (is_multisite()) {
  1579. if (!isset ($ai_db_options_multisite ['MULTISITE_EXCEPTIONS']) || $ai_db_options_multisite ['MULTISITE_EXCEPTIONS'] == '') {
  1580. $ai_db_options_multisite ['MULTISITE_EXCEPTIONS'] = DEFAULT_MULTISITE_EXCEPTIONS;
  1581. }
  1582.  
  1583. return ($ai_db_options_multisite ['MULTISITE_EXCEPTIONS']);
  1584. }
  1585.  
  1586. return DEFAULT_MULTISITE_EXCEPTIONS;
  1587. }
  1588.  
  1589. function get_viewport_name ($viewport_number) {
  1590. global $ai_db_options;
  1591.  
  1592. if (isset ($ai_db_options [AI_GLOBAL_OPTION_NAME])) $plugin_db_options = $ai_db_options [AI_GLOBAL_OPTION_NAME]; else $plugin_db_options = '';
  1593. if (!$plugin_db_options) $plugin_db_options = get_option (AD_OPTIONS);
  1594.  
  1595. $viewport_settins_name = 'VIEWPORT_NAME_' . $viewport_number;
  1596.  
  1597. if (!isset ($plugin_db_options [$viewport_settins_name])) {
  1598. $plugin_db_options [$viewport_settins_name] = defined ("DEFAULT_VIEWPORT_NAME_" . $viewport_number) ? constant ("DEFAULT_VIEWPORT_NAME_" . $viewport_number) : "";
  1599. }
  1600.  
  1601. return ($plugin_db_options [$viewport_settins_name]);
  1602. }
  1603.  
  1604. function get_viewport_width ($viewport_number) {
  1605. global $ai_db_options;
  1606.  
  1607. if (isset ($ai_db_options [AI_GLOBAL_OPTION_NAME])) $plugin_db_options = $ai_db_options [AI_GLOBAL_OPTION_NAME]; else $plugin_db_options = '';
  1608. if (!$plugin_db_options) $plugin_db_options = get_option (AD_OPTIONS);
  1609.  
  1610. $viewport_settins_name = 'VIEWPORT_WIDTH_' . $viewport_number;
  1611.  
  1612. if (!isset ($plugin_db_options [$viewport_settins_name])) {
  1613. $plugin_db_options [$viewport_settins_name] = defined ("DEFAULT_VIEWPORT_WIDTH_" . $viewport_number) ? constant ("DEFAULT_VIEWPORT_WIDTH_" . $viewport_number) : 0;
  1614. }
  1615.  
  1616. return ($plugin_db_options [$viewport_settins_name]);
  1617. }
  1618.  
  1619. function filter_html_class ($str){
  1620.  
  1621. $str = str_replace (array ("\\\""), array ("\""), $str);
  1622. $str = sanitize_html_class ($str);
  1623.  
  1624. return $str;
  1625. }
  1626.  
  1627. function filter_string ($str){
  1628.  
  1629. $str = str_replace (array ("\\\""), array ("\""), $str);
  1630. $str = str_replace (array ("\"", "<", ">"), "", $str);
  1631. $str = trim (esc_html ($str));
  1632.  
  1633. return $str;
  1634. }
  1635.  
  1636. function filter_option ($option, $value, $delete_escaped_backslashes = true){
  1637. if ($delete_escaped_backslashes)
  1638. $value = str_replace (array ("\\\""), array ("\""), $value);
  1639.  
  1640. if ($option == AI_OPTION_DOMAIN_LIST) {
  1641. $value = str_replace (array ("\\", "/", "?", "\"", "<", ">", "[", "]"), "", $value);
  1642. $value = esc_html ($value);
  1643. }
  1644. elseif (
  1645. $option == AI_OPTION_PARAGRAPH_TEXT ||
  1646. $option == AI_OPTION_AVOID_TEXT_ABOVE ||
  1647. $option == AI_OPTION_AVOID_TEXT_BELOW
  1648. ) {
  1649. $value = esc_html ($value);
  1650. }
  1651. elseif ($option == AI_OPTION_NAME ||
  1652. $option == AI_OPTION_GENERAL_TAG ||
  1653. $option == AI_OPTION_DOMAIN_LIST ||
  1654. $option == AI_OPTION_CATEGORY_LIST ||
  1655. $option == AI_OPTION_TAG_LIST ||
  1656. $option == AI_OPTION_ID_LIST ||
  1657. $option == AI_OPTION_URL_LIST ||
  1658. $option == AI_OPTION_URL_PARAMETER_LIST ||
  1659. $option == AI_OPTION_PARAGRAPH_TEXT_TYPE ||
  1660. $option == AI_OPTION_PARAGRAPH_NUMBER ||
  1661. $option == AI_OPTION_MIN_PARAGRAPHS ||
  1662. $option == AI_OPTION_AVOID_PARAGRAPHS_ABOVE ||
  1663. $option == AI_OPTION_AVOID_PARAGRAPHS_BELOW ||
  1664. $option == AI_OPTION_AVOID_TRY_LIMIT ||
  1665. $option == AI_OPTION_MIN_WORDS ||
  1666. $option == AI_OPTION_MAX_WORDS ||
  1667. $option == AI_OPTION_MIN_PARAGRAPH_WORDS ||
  1668. $option == AI_OPTION_MAX_PARAGRAPH_WORDS ||
  1669. $option == AI_OPTION_MAXIMUM_INSERTIONS ||
  1670. $option == AI_OPTION_AFTER_DAYS ||
  1671. $option == AI_OPTION_EXCERPT_NUMBER ||
  1672. $option == AI_OPTION_CUSTOM_CSS) {
  1673. $value = str_replace (array ("\"", "<", ">", "[", "]"), "", $value);
  1674. $value = esc_html ($value);
  1675. }
  1676.  
  1677. return $value;
  1678. }
  1679.  
  1680. function filter_option_hf ($option, $value){
  1681. $value = str_replace (array ("\\\""), array ("\""), $value);
  1682.  
  1683. if ($option == AI_OPTION_CODE ) {
  1684. } elseif ($option == AI_OPTION_ENABLE_MANUAL) {
  1685. } elseif ($option == AI_OPTION_PROCESS_PHP) {
  1686. } elseif ($option == AI_OPTION_ENABLE_404) {
  1687. } elseif ($option == AI_OPTION_DETECT_SERVER_SIDE) {
  1688. } elseif ($option == AI_OPTION_DISPLAY_FOR_DEVICES) {
  1689. }
  1690.  
  1691. return $value;
  1692. }
  1693.  
  1694. function ai_preview () {
  1695. global $ai_db_options, $block_object;
  1696.  
  1697. check_admin_referer ("adinserter_data", "ai_check");
  1698.  
  1699. if (isset ($_GET ["preview"])) {
  1700. $block = $_GET ["preview"];
  1701. if (is_numeric ($block) && $block >= 1 && $block <= AD_INSERTER_BLOCKS) {
  1702. generate_code_preview ($block);
  1703. }
  1704. }
  1705.  
  1706. if (isset ($_GET ["export"])) {
  1707. $block = $_GET ["export"];
  1708. if (is_numeric ($block)) {
  1709. if ($block == 0) echo base64_encode (serialize ($ai_db_options));
  1710. elseif ($block >= 1 && $block <= AD_INSERTER_BLOCKS) {
  1711. $obj = $block_object [$block];
  1712. echo base64_encode (serialize ($obj->wp_options));
  1713. }
  1714. }
  1715. }
  1716.  
  1717. if (isset ($_GET ["image"])) {
  1718. header ("Content-Type: image/png");
  1719. header ("Content-Length: " . filesize (AD_INSERTER_PLUGIN_DIR.'images/'.$_GET ["image"]));
  1720. readfile (AD_INSERTER_PLUGIN_DIR.'images/'.$_GET ["image"]);
  1721. }
  1722.  
  1723. die ();
  1724. }
  1725.  
  1726. function ai_generate_extract (&$settings) {
  1727.  
  1728. $obj = new ai_Block (1);
  1729.  
  1730. $extract = array ();
  1731. $content_hook_blocks = array ();
  1732. $excerpt_hook_blocks = array ();
  1733. $loop_start_hook_blocks = array ();
  1734. $loop_end_hook_blocks = array ();
  1735.  
  1736. $content_hook_blocks = array (AI_PT_ANY => array (), AI_PT_HOMEPAGE => array(), AI_PT_CATEGORY => array(), AI_PT_SEARCH => array(), AI_PT_ARCHIVE => array(), AI_PT_STATIC => array(), AI_PT_POST => array(), AI_PT_404 => array(), AI_PT_FEED => array());
  1737. $excerpt_hook_blocks = array (AI_PT_ANY => array (), AI_PT_HOMEPAGE => array(), AI_PT_CATEGORY => array(), AI_PT_SEARCH => array(), AI_PT_ARCHIVE => array(), AI_PT_STATIC => array(), AI_PT_POST => array(), AI_PT_404 => array(), AI_PT_FEED => array());
  1738. $loop_start_hook_blocks = array (AI_PT_ANY => array (), AI_PT_HOMEPAGE => array(), AI_PT_CATEGORY => array(), AI_PT_SEARCH => array(), AI_PT_ARCHIVE => array(), AI_PT_STATIC => array(), AI_PT_POST => array(), AI_PT_404 => array(), AI_PT_FEED => array());
  1739. $loop_end_hook_blocks = array (AI_PT_ANY => array (), AI_PT_HOMEPAGE => array(), AI_PT_CATEGORY => array(), AI_PT_SEARCH => array(), AI_PT_ARCHIVE => array(), AI_PT_STATIC => array(), AI_PT_POST => array(), AI_PT_404 => array(), AI_PT_FEED => array());
  1740.  
  1741. // Generate extracted data
  1742. for ($block = 1; $block <= AD_INSERTER_BLOCKS; $block ++) {
  1743.  
  1744. if (!isset ($settings [$block])) continue;
  1745.  
  1746. $obj->number = $block;
  1747. $obj->wp_options = $settings [$block];
  1748.  
  1749. $page_types = array ();
  1750. if ($obj->get_display_settings_home()) $page_types []= AI_PT_HOMEPAGE;
  1751. if ($obj->get_display_settings_page()) $page_types []= AI_PT_STATIC;
  1752. if ($obj->get_display_settings_post()) $page_types []= AI_PT_POST;
  1753. if ($obj->get_display_settings_category()) $page_types []= AI_PT_CATEGORY;
  1754. if ($obj->get_display_settings_search()) $page_types []= AI_PT_SEARCH;
  1755. if ($obj->get_display_settings_archive()) $page_types []= AI_PT_ARCHIVE;
  1756. if ($obj->get_enable_feed()) $page_types []= AI_PT_FEED;
  1757. if ($obj->get_enable_404()) $page_types []= AI_PT_404;
  1758.  
  1759. if ($page_types) {
  1760. switch ($obj->get_display_type()) {
  1761. case AD_SELECT_BEFORE_PARAGRAPH:
  1762. case AD_SELECT_AFTER_PARAGRAPH:
  1763. case AD_SELECT_BEFORE_CONTENT:
  1764. case AD_SELECT_AFTER_CONTENT:
  1765. foreach ($page_types as $block_page_type) $content_hook_blocks [$block_page_type][]= $block;
  1766. $content_hook_blocks [AI_PT_ANY][]= $block;
  1767. break;
  1768. case AD_SELECT_BEFORE_EXCERPT:
  1769. case AD_SELECT_AFTER_EXCERPT:
  1770. foreach ($page_types as $block_page_type) $excerpt_hook_blocks [$block_page_type][]= $block;
  1771. $excerpt_hook_blocks [AI_PT_ANY][]= $block;
  1772. break;
  1773. case AD_SELECT_BEFORE_POST:
  1774. foreach ($page_types as $block_page_type) $loop_start_hook_blocks [$block_page_type][]= $block;
  1775. $loop_start_hook_blocks [AI_PT_ANY][]= $block;
  1776. break;
  1777. case AD_SELECT_AFTER_POST:
  1778. foreach ($page_types as $block_page_type) $loop_end_hook_blocks [$block_page_type][]= $block;
  1779. $loop_end_hook_blocks [AI_PT_ANY][]= $block;
  1780. break;
  1781. }
  1782. }
  1783. }
  1784.  
  1785. $extract [CONTENT_HOOK_BLOCKS] = $content_hook_blocks;
  1786. $extract [EXCERPT_HOOK_BLOCKS] = $excerpt_hook_blocks;
  1787. $extract [LOOP_START_HOOK_BLOCKS] = $loop_start_hook_blocks;
  1788. $extract [LOOP_END_HOOK_BLOCKS] = $loop_end_hook_blocks;
  1789.  
  1790. return ($extract);
  1791. }
  1792.  
  1793. function ai_load_settings () {
  1794. global $ai_db_options, $block_object, $ai_db_options_extract, $ai_wp_data, $version_string;
  1795.  
  1796. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) ai_log ("LOAD SETTINGS START");
  1797.  
  1798. ai_load_options ();
  1799.  
  1800. if (isset ($ai_db_options [AI_EXTRACT_OPTION_NAME]) && $ai_db_options ['global']['VERSION'] == $version_string)
  1801. $ai_db_options_extract = $ai_db_options [AI_EXTRACT_OPTION_NAME]; else
  1802. $ai_db_options_extract = ai_generate_extract ($ai_db_options);
  1803.  
  1804. $obj = new ai_Block (0);
  1805. $obj->wp_options [AI_OPTION_NAME] = 'Default';
  1806. $block_object [0] = $obj;
  1807. for ($block = 1; $block <= AD_INSERTER_BLOCKS; $block ++) {
  1808. $obj = new ai_Block ($block);
  1809. $obj->load_options ($block);
  1810. $block_object [$block] = $obj;
  1811. }
  1812.  
  1813. $adH = new ai_AdH();
  1814. $adF = new ai_AdF();
  1815. $adH->load_options (AI_HEADER_OPTION_NAME);
  1816. $adF->load_options (AI_FOOTER_OPTION_NAME);
  1817. $block_object [AI_HEADER_OPTION_NAME] = $adH;
  1818. $block_object [AI_FOOTER_OPTION_NAME] = $adF;
  1819.  
  1820. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) ai_log ("LOAD SETTINGS END");
  1821. }
  1822.  
  1823.  
  1824. function generate_viewport_css () {
  1825.  
  1826. $viewports = array ();
  1827. for ($viewport = 1; $viewport <= AD_INSERTER_VIEWPORTS; $viewport ++) {
  1828. $viewport_name = get_viewport_name ($viewport);
  1829. $viewport_width = get_viewport_width ($viewport);
  1830. if ($viewport_name != '') {
  1831. $viewports []= array ('index' => $viewport, 'name' => $viewport_name, 'width' => $viewport_width);
  1832. }
  1833. }
  1834.  
  1835. $viewport_styles = '';
  1836. if (count ($viewports) != 0) {
  1837. // $viewport_styles .= "/* " . AD_INSERTER_NAME . " version " . AD_INSERTER_VERSION ." - viewport classes */\n\n";
  1838. // $viewport_styles .= "/* DO NOT MODIFY - This file is automatically generated when you save ".AD_INSERTER_NAME." settings */\n";
  1839. foreach ($viewports as $index => $viewport) {
  1840. // $viewport_styles .= "\n/* " . $viewport ['name'] . " */\n\n";
  1841. if ($viewport ['index'] == 1) {
  1842. foreach (array_reverse ($viewports) as $index2 => $viewport2) {
  1843. if ($viewport2 ['index'] != 1) {
  1844. $viewport_styles .= ".ai-viewport-" . $viewport2 ['index'] . " { display: none !important;}\n";
  1845. }
  1846. }
  1847. $viewport_styles .= ".ai-viewport-1 { display: inherit !important;}\n";
  1848. $viewport_styles .= ".ai-viewport-0 { display: none !important;}\n";
  1849. } else {
  1850. $viewport_styles .= "@media ";
  1851. if ($index != count ($viewports) - 1)
  1852. $viewport_styles .= "(min-width: " . $viewport ['width'] . "px) and ";
  1853. $viewport_styles .= "(max-width: " . ($viewports [$index - 1]['width'] - 1) . "px) {\n";
  1854. foreach ($viewports as $index2 => $viewport2) {
  1855. if ($viewport2 ['index'] == 1)
  1856. $viewport_styles .= ".ai-viewport-" . $viewport2 ['index'] . " { display: none !important;}\n";
  1857. elseif ($viewport ['index'] == $viewport2 ['index'])
  1858. $viewport_styles .= ".ai-viewport-" . $viewport2 ['index'] . " { display: inherit !important;}\n";
  1859.  
  1860. }
  1861. $viewport_styles .= "}\n";
  1862. }
  1863. }
  1864. }
  1865. return ($viewport_styles);
  1866. }
  1867.  
  1868.  
  1869. function ai_settings () {
  1870. global $ai_db_options, $block_object;
  1871.  
  1872. if (isset ($_POST [AI_FORM_SAVE])) {
  1873.  
  1874. // echo count ($_POST);
  1875. // print_r ($_POST);
  1876.  
  1877. check_admin_referer ('save_adinserter_settings');
  1878.  
  1879. $subpage = 'main';
  1880. $start = 1;
  1881. $end = 16;
  1882. if (function_exists ('ai_settings_parameters')) ai_settings_parameters ($subpage, $start, $end);
  1883.  
  1884. $invalid_blocks = array ();
  1885.  
  1886. $import_switch_name = AI_OPTION_IMPORT . WP_FORM_FIELD_POSTFIX . '0';
  1887. if (isset ($_POST [$import_switch_name]) && $_POST [$import_switch_name] == "1") {
  1888. // Import Ad Inserter settings
  1889. $saved_settings = $ai_db_options;
  1890. $ai_options = @unserialize (base64_decode (str_replace (array ("\\\""), array ("\""), $_POST ["export_settings_0"])));
  1891. if ($ai_options === false) {
  1892. $ai_options = $saved_settings;
  1893. $invalid_blocks []= 0;
  1894. }
  1895. } else {
  1896. // Try to import individual settings
  1897. $ai_options = array ();
  1898.  
  1899. for ($block = 1; $block <= AD_INSERTER_BLOCKS; $block ++) {
  1900. $ad = new ai_Block ($block);
  1901.  
  1902. if (isset ($ai_db_options [$block])) $saved_settings = $ai_db_options [$block]; else
  1903. $saved_settings = $ad->wp_options;
  1904.  
  1905. if ($block < $start || $block > $end) {
  1906. $ai_options [$block] = $saved_settings;
  1907. continue;
  1908. }
  1909.  
  1910. // $block_settings = 0;
  1911. $import_switch_name = AI_OPTION_IMPORT . WP_FORM_FIELD_POSTFIX . $block;
  1912. $import_name_switch_name = AI_OPTION_IMPORT_NAME . WP_FORM_FIELD_POSTFIX . $block;
  1913. if (isset ($_POST [$import_switch_name]) && $_POST [$import_switch_name] == "1") {
  1914.  
  1915. $exported_settings = @unserialize (base64_decode (str_replace (array ("\\\""), array ("\""), $_POST ["export_settings_" . $block])));
  1916.  
  1917. if ($exported_settings !== false) {
  1918. foreach (array_keys ($ad->wp_options) as $key){
  1919. if ($key == AI_OPTION_NAME && isset ($_POST [$import_name_switch_name]) && $_POST [$import_name_switch_name] != "1") {
  1920. $form_field_name = $key . WP_FORM_FIELD_POSTFIX . $block;
  1921. if (isset ($_POST [$form_field_name])){
  1922. $ad->wp_options [$key] = filter_option ($key, $_POST [$form_field_name]);
  1923. }
  1924. } else {
  1925. if (isset ($exported_settings [$key])) {
  1926. $ad->wp_options [$key] = filter_option ($key, $exported_settings [$key], false);
  1927. // $block_settings ++;
  1928. }
  1929. }
  1930. }
  1931. } else {
  1932. $ad->wp_options = $saved_settings;
  1933. $invalid_blocks []= $block;
  1934. }
  1935. } else {
  1936. foreach (array_keys ($ad->wp_options) as $key){
  1937. $form_field_name = $key . WP_FORM_FIELD_POSTFIX . $block;
  1938. if (isset ($_POST [$form_field_name])){
  1939. $ad->wp_options [$key] = filter_option ($key, $_POST [$form_field_name]);
  1940. // $block_settings ++;
  1941. }
  1942. }
  1943. }
  1944.  
  1945. $ai_options [$block] = $ad->wp_options;
  1946. delete_option (str_replace ("#", $block, AD_ADx_OPTIONS));
  1947. }
  1948.  
  1949. $adH = new ai_AdH();
  1950. $adF = new ai_AdF();
  1951.  
  1952. foreach(array_keys ($adH->wp_options) as $key){
  1953. $form_field_name = $key . WP_FORM_FIELD_POSTFIX . AI_HEADER_OPTION_NAME;
  1954. if(isset ($_POST [$form_field_name])){
  1955. $adH->wp_options [$key] = filter_option_hf ($key, $_POST [$form_field_name]);
  1956. }
  1957. }
  1958.  
  1959. foreach(array_keys($adF->wp_options) as $key){
  1960. $form_field_name = $key . WP_FORM_FIELD_POSTFIX . AI_FOOTER_OPTION_NAME;
  1961. if(isset ($_POST [$form_field_name])){
  1962. $adF->wp_options [$key] = filter_option_hf ($key, $_POST [$form_field_name]);
  1963. }
  1964. }
  1965.  
  1966. $ai_options [AI_HEADER_OPTION_NAME] = $adH->wp_options;
  1967. $ai_options [AI_FOOTER_OPTION_NAME] = $adF->wp_options;
  1968.  
  1969. $options = array ();
  1970.  
  1971. if (function_exists ('ai_filter_global_settings')) ai_filter_global_settings ($options);
  1972.  
  1973. $options ['SYNTAX_HIGHLIGHTER_THEME'] = filter_string ($_POST ['syntax-highlighter-theme']);
  1974. $options ['BLOCK_CLASS_NAME'] = filter_html_class ($_POST ['block-class-name']);
  1975. $options ['MINIMUM_USER_ROLE'] = filter_string ($_POST ['minimum-user-role']);
  1976. $options ['PLUGIN_PRIORITY'] = filter_option ('plugin_priority', $_POST ['plugin_priority']);
  1977. $options ['ADMIN_TOOLBAR_DEBUGGING'] = filter_option ('admin_toolbar_debugging', $_POST ['admin_toolbar_debugging']);
  1978. $options ['REMOTE_DEBUGGING'] = filter_option ('remote_debugging', $_POST ['remote_debugging']);
  1979. $options ['JAVASCRIPT_DEBUGGING'] = filter_option ('javascript_debugging', $_POST ['javascript_debugging']);
  1980.  
  1981. for ($viewport = 1; $viewport <= AD_INSERTER_VIEWPORTS; $viewport ++) {
  1982. if (isset ($_POST ['viewport-name-'.$viewport]))
  1983. $options ['VIEWPORT_NAME_'.$viewport] = filter_string ($_POST ['viewport-name-'.$viewport]);
  1984. if (isset ($_POST ['viewport-width-'.$viewport]))
  1985. $options ['VIEWPORT_WIDTH_'.$viewport] = filter_option ('viewport_width', $_POST ['viewport-width-'.$viewport]);
  1986. }
  1987.  
  1988. $options ['VIEWPORT_CSS'] = generate_viewport_css ();
  1989.  
  1990. $ai_options [AI_GLOBAL_OPTION_NAME] = ai_check_plugin_options ($options);
  1991. }
  1992.  
  1993. if (!empty ($invalid_blocks)) {
  1994. if ($invalid_blocks [0] == 0) {
  1995. echo "<div class='error' style='margin: 5px 15px 2px 0px; padding: 10px;'>Error importing ", AD_INSERTER_NAME, " settings.</div>";
  1996. } else echo "<div class='error' style='margin: 5px 15px 2px 0px; padding: 10px;'>Error importing settings for block", count ($invalid_blocks) == 1 ? "" : "s:", " ", implode (", ", $invalid_blocks), ".</div>";
  1997. }
  1998.  
  1999. // Generate and save extract
  2000. $ai_options [AI_EXTRACT_OPTION_NAME] = ai_generate_extract ($ai_options);
  2001.  
  2002. $ai_options [AI_GLOBAL_OPTION_NAME]['TIMESTAMP'] = time ();
  2003.  
  2004. update_option (WP_OPTION_NAME, $ai_options);
  2005.  
  2006. // Multisite
  2007. if (is_multisite () && is_main_site ()) {
  2008. $options = array ();
  2009. $options ['MULTISITE_SETTINGS_PAGE'] = filter_option ('multisite_settings_page', $_POST ['multisite_settings_page']);
  2010. $options ['MULTISITE_WIDGETS'] = filter_option ('multisite_widgets', $_POST ['multisite_widgets']);
  2011. $options ['MULTISITE_EXCEPTIONS'] = filter_option ('multisite_exceptions', $_POST ['multisite_exceptions']);
  2012. ai_check_multisite_options ($options);
  2013. update_site_option (WP_OPTION_NAME, $options);
  2014. }
  2015.  
  2016. ai_load_settings ();
  2017.  
  2018. delete_option (str_replace ("#", "Header", AD_ADx_OPTIONS));
  2019. delete_option (str_replace ("#", "Footer", AD_ADx_OPTIONS));
  2020. delete_option (AD_OPTIONS);
  2021.  
  2022. echo "<div class='updated' style='margin: 5px 15px 2px 0px; padding: 10px;'><strong>Settings saved.</strong></div>";
  2023.  
  2024. } elseif (isset ($_POST [AI_FORM_CLEAR])) {
  2025.  
  2026. check_admin_referer ('save_adinserter_settings');
  2027.  
  2028. for ($block = 1; $block <= AD_INSERTER_BLOCKS; $block ++) {
  2029. delete_option (str_replace ("#", $block, AD_ADx_OPTIONS));
  2030. }
  2031.  
  2032. delete_option (str_replace ("#", "Header", AD_ADx_OPTIONS));
  2033. delete_option (str_replace ("#", "Footer", AD_ADx_OPTIONS));
  2034. delete_option (AD_OPTIONS);
  2035.  
  2036. delete_option (WP_OPTION_NAME);
  2037. delete_option (WP_AD_INSERTER_PRO_LICENSE);
  2038. if (is_multisite () && is_main_site ()) {
  2039. delete_site_option (WP_OPTION_NAME, $options);
  2040. }
  2041.  
  2042. ai_load_settings ();
  2043.  
  2044. echo "<div class='error' style='margin: 5px 15px 2px 0px; padding: 10px;'>Settings cleared.</div>";
  2045. }
  2046.  
  2047. generate_settings_form ();
  2048. }
  2049.  
  2050.  
  2051. function ai_adinserter ($ad_number = '', $ignore = ''){
  2052. global $block_object, $ad_interter_globals, $ai_wp_data, $ai_last_check;
  2053.  
  2054. $debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
  2055.  
  2056. if ($ad_number == "") return "";
  2057. if (!is_numeric ($ad_number)) return "";
  2058. $ad_number = (int) $ad_number;
  2059. if ($ad_number < 1 || $ad_number > AD_INSERTER_BLOCKS) return "";
  2060.  
  2061. $globals_name = AI_PHP_FUNCTION_CALL_COUNTER_NAME . $ad_number;
  2062.  
  2063. if (!isset ($ad_interter_globals [$globals_name])) {
  2064. $ad_interter_globals [$globals_name] = 1;
  2065. } else $ad_interter_globals [$globals_name] ++;
  2066.  
  2067. if ($debug_processing) ai_log ("PHP FUNCTION CALL adinserter ($ad_number".($ignore == '' ? '' : ', \''.$ignore)."') [" . $ad_interter_globals [$globals_name] . ']');
  2068.  
  2069. $ai_wp_data [AI_CONTEXT] = AI_CONTEXT_PHP_FUNCTION;
  2070.  
  2071. $ignore_array = array ();
  2072. if (trim ($ignore) != '') {
  2073. $ignore_array = explode (",", str_replace (" ", "", $ignore));
  2074. }
  2075.  
  2076. $obj = $block_object [$ad_number];
  2077. $obj->clear_code_cache ();
  2078.  
  2079. $ai_last_check = AI_CHECK_ENABLED;
  2080. if (!$obj->get_enable_php_call ()) return "";
  2081. if (!$obj->check_server_side_detection ()) return "";
  2082. if (!$obj->check_page_types_lists_users (in_array ("page_type", $ignore_array))) return "";
  2083. if (!$obj->check_filter ($ad_interter_globals [$globals_name])) return "";
  2084.  
  2085. if ($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_POST || $ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_STATIC) {
  2086. $meta_value = get_post_meta (get_the_ID (), '_adinserter_block_exceptions', true);
  2087. $selected_blocks = explode (",", $meta_value);
  2088.  
  2089. if (!$obj->check_post_page_exceptions ($selected_blocks)) return "";
  2090. }
  2091.  
  2092. // Last check before insertion
  2093. if (!$obj->check_and_increment_block_counter ()) return "";
  2094.  
  2095. $ai_last_check = AI_CHECK_DEBUG_NO_INSERTION;
  2096. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_NO_INSERTION) != 0) return "";
  2097.  
  2098. $ai_last_check = AI_CHECK_INSERTED;
  2099. return $obj->get_code_for_insertion ();
  2100. }
  2101.  
  2102. function adinserter ($block = '', $ignore = '') {
  2103. global $ai_last_check, $ai_wp_data;
  2104.  
  2105. $debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
  2106. $ai_last_check = AI_CHECK_NONE;
  2107. $code = ai_adinserter ($block, $ignore);
  2108. if ($debug_processing && $ai_last_check != AI_CHECK_NONE) ai_log (ai_log_block_status ($block, $ai_last_check));
  2109. if ($debug_processing) ai_log ("PHP FUNCTION CALL END\n");
  2110.  
  2111. return $code;
  2112. }
  2113.  
  2114.  
  2115.  
  2116. function ai_content_hook ($content = ''){
  2117. global $block_object, $ad_interter_globals, $ai_db_options_extract, $ai_wp_data, $ai_last_check;
  2118.  
  2119. if ($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_ADMIN) return;
  2120.  
  2121. $debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
  2122. $globals_name = AI_CONTENT_COUNTER_NAME;
  2123.  
  2124. if (!isset ($ad_interter_globals [$globals_name])) {
  2125. $ad_interter_globals [$globals_name] = 1;
  2126. } else $ad_interter_globals [$globals_name] ++;
  2127.  
  2128. if ($debug_processing) ai_log ("CONTENT HOOK START [" . $ad_interter_globals [$globals_name] . ']');
  2129.  
  2130. $ai_wp_data [AI_CONTEXT] = AI_CONTEXT_CONTENT;
  2131.  
  2132. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_POSITIONS) != 0) {
  2133. $content_words = number_of_words ($content);
  2134.  
  2135. $positions_inserted = false;
  2136. if ($ai_wp_data [AI_WP_DEBUG_BLOCK] == 0) {
  2137. $preview = $block_object [0];
  2138. $content = $preview->before_paragraph ($content, true);
  2139. $content = $preview->after_paragraph ($content, true);
  2140. $positions_inserted = true;
  2141. }
  2142. }
  2143.  
  2144. if ($ai_db_options_extract [CONTENT_HOOK_BLOCKS][$ai_wp_data [AI_WP_PAGE_TYPE]]) {
  2145. if ($debug_processing) ai_log_content ($content);
  2146.  
  2147. $meta_value = get_post_meta (get_the_ID (), '_adinserter_block_exceptions', true);
  2148. $selected_blocks = explode (",", $meta_value);
  2149.  
  2150. $ai_last_check = AI_CHECK_NONE;
  2151. $current_block = 0;
  2152.  
  2153. foreach ($ai_db_options_extract [CONTENT_HOOK_BLOCKS][$ai_wp_data [AI_WP_PAGE_TYPE]] as $block) {
  2154. if ($debug_processing && $ai_last_check != AI_CHECK_NONE) ai_log (ai_log_block_status ($current_block, $ai_last_check));
  2155.  
  2156. if (!isset ($block_object [$block])) continue;
  2157.  
  2158. $current_block = $block;
  2159.  
  2160. $obj = $block_object [$block];
  2161. $obj->clear_code_cache ();
  2162.  
  2163. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_POSITIONS) != 0 && !$positions_inserted && $ai_wp_data [AI_WP_DEBUG_BLOCK] <= $block) {
  2164. $preview = $block_object [$ai_wp_data [AI_WP_DEBUG_BLOCK]];
  2165. $content = $preview->before_paragraph ($content, true);
  2166. $content = $preview->after_paragraph ($content, true);
  2167. $positions_inserted = true;
  2168. }
  2169.  
  2170. if (!$obj->check_server_side_detection ()) continue;
  2171. if (!$obj->check_page_types_lists_users ()) continue;
  2172. if (!$obj->check_post_page_exceptions ($selected_blocks)) continue;
  2173. if (!$obj->check_filter ($ad_interter_globals [$globals_name])) continue;
  2174. if (!$obj->check_number_of_words ($content)) continue;
  2175.  
  2176. // Deprecated
  2177. $ai_last_check = AI_CHECK_DISABLED_MANUALLY;
  2178. if ($obj->display_disabled ($content)) continue;
  2179.  
  2180. // Last check before insertion
  2181. if (!$obj->check_block_counter ()) continue;
  2182.  
  2183. $display_type = $obj->get_display_type();
  2184.  
  2185. if ($display_type == AD_SELECT_BEFORE_PARAGRAPH) {
  2186. $ai_last_check = AI_CHECK_PARAGRAPH_COUNTING;
  2187. $content = $obj->before_paragraph ($content);
  2188. }
  2189. elseif ($display_type == AD_SELECT_AFTER_PARAGRAPH) {
  2190. $ai_last_check = AI_CHECK_PARAGRAPH_COUNTING;
  2191. $content = $obj->after_paragraph ($content);
  2192. }
  2193. elseif ($display_type == AD_SELECT_BEFORE_CONTENT) {
  2194. $obj->increment_block_counter ();
  2195.  
  2196. $ai_last_check = AI_CHECK_DEBUG_NO_INSERTION;
  2197. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_NO_INSERTION) == 0) {
  2198. $content = $obj->get_code_for_insertion () . $content;
  2199. $ai_last_check = AI_CHECK_INSERTED;
  2200. }
  2201. }
  2202. elseif ($display_type == AD_SELECT_AFTER_CONTENT) {
  2203. $obj->increment_block_counter ();
  2204.  
  2205. $ai_last_check = AI_CHECK_DEBUG_NO_INSERTION;
  2206. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_NO_INSERTION) == 0) {
  2207. $content = $content . $obj->get_code_for_insertion ();
  2208. $ai_last_check = AI_CHECK_INSERTED;
  2209. }
  2210. }
  2211. }
  2212. if ($debug_processing && $ai_last_check != AI_CHECK_NONE) ai_log (ai_log_block_status ($current_block, $ai_last_check));
  2213. }
  2214.  
  2215. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_TAGS) != 0) {
  2216. $style = AI_DEBUG_TAGS_STYLE;
  2217.  
  2218. $content = preg_replace ("/\r\n\r\n/", "\r\n\r\n<kbd style='$style background: #0ff; color: #000;'>\\r\\n\\r\\n</kbd>", $content);
  2219.  
  2220. $content = preg_replace ("/<p>/i", "<p><kbd style='$style background: #0a0;'>&lt;p&gt;</kbd>", $content);
  2221. // $content = preg_replace ("/<p ([^>]*?)>/i", "<p$1><kbd style='$style background: #0a0;'>&lt;p$1&gt;</kbd>", $content); // Full p tags
  2222. $content = preg_replace ("/<p ([^>]*?)>/i", "<p$1><kbd style='$style background: #0a0;'>&lt;p&gt;</kbd>", $content);
  2223. // $content = preg_replace ("/<div([^>]*?)>/i", "<div$1><kbd style='$style background: #46f;'>&lt;div$1&gt;</kbd>", $content); // Full div tags
  2224. $content = preg_replace ("/<div([^>]*?)>/i", "<div$1><kbd style='$style background: #46f;'>&lt;div&gt;</kbd>", $content);
  2225. $content = preg_replace ("/<h([1-6])([^>]*?)>/i", "<h$1$2><kbd style='$style background: #d4e;'>&lt;h$1&gt;</kbd>", $content);
  2226. $content = preg_replace ("/<img([^>]*?)>/i", "<img$1><kbd style='$style background: #ee0; color: #000'>&lt;img$1&gt;</kbd>", $content);
  2227. $content = preg_replace ("/<pre([^>]*?)>/i", "<pre$1><kbd style='$style background: #222;'>&lt;pre&gt;</kbd>", $content);
  2228. $content = preg_replace ("/<(?!section|ins|script|kbd|a|strong|pre|p|div|h[1-6]|img)([a-z0-9]+)([^>]*?)>/i", "<$1$2><kbd style='$style background: #fb0; color: #000;'>&lt;$1$2&gt;</kbd>", $content);
  2229.  
  2230. $content = preg_replace ("/<\/p>/i", "<kbd style='$style background: #0a0;'>&lt;/p&gt;</kbd></p>", $content);
  2231. $content = preg_replace ("/<\/div>/i", "<kbd style='$style background: #46f;'>&lt;/div&gt;</kbd></div>", $content);
  2232. $content = preg_replace ("/<\/h([1-6])>/i", "<kbd style='$style background: #d4e;'>&lt;/h$1&gt;</kbd></h$1>", $content);
  2233. $content = preg_replace ("/<\/pre>/i", "<kbd style='$style background: #222;'>&lt;/pre&gt;</kbd></pre>", $content);
  2234. $content = preg_replace ("/<\/(?!section|ins|script|kbd|a|strong|pre|p|div|h[1-6])([a-z0-9]+)>/i", "<kbd style='$style background: #fb0; color: #000;'>&lt;/$1&gt;</kbd></$1>", $content);
  2235. }
  2236.  
  2237. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_POSITIONS) != 0) {
  2238. $style = AI_DEBUG_POSITIONS_STYLE;
  2239.  
  2240. if (!$positions_inserted) {
  2241. $preview = $block_object [$ai_wp_data [AI_WP_DEBUG_BLOCK]];
  2242. $content = $preview->before_paragraph ($content, true);
  2243. $content = $preview->after_paragraph ($content, true);
  2244. }
  2245.  
  2246. $content = preg_replace ("/\[\[AI_BP([\d]+?)\]\]/", "<section style='$style'>BEFORE PARAGRAPH $1</section>", $content);
  2247. $content = preg_replace ("/\[\[AI_AP([\d]+?)\]\]/", "<section style='$style'>AFTER PARAGRAPH $1</section>", $content);
  2248.  
  2249. $counter = $ad_interter_globals [$globals_name];
  2250. if ($counter == 1) $counter = '';
  2251.  
  2252. $content = "<section style='$style'><a style='float: left; font-size: 10px; text-decoration: none; color: transparent; padding: 0px 10px 0 0;'>".$content_words." words</a>BEFORE CONTENT ".$counter."<a style='float: right; font-size: 10px; text-decoration: none; color: #88f; padding: 0px 10px 0 0;'>".$content_words." words</a></section>". $content;
  2253.  
  2254. $content = $content . "<section style='".AI_DEBUG_POSITIONS_STYLE."'>AFTER CONTENT ".$counter."</section>";
  2255. }
  2256.  
  2257. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_TAGS) != 0) {
  2258. $content = '<kbd style="display: none">[HTML TAGS REMOVED]</kbd>' . $content;
  2259. }
  2260.  
  2261. if ($debug_processing) ai_log ("CONTENT HOOK END\n");
  2262.  
  2263. return $content;
  2264. }
  2265.  
  2266. // Process Before/After Excerpt postion
  2267. function ai_excerpt_hook ($content = ''){
  2268. global $ad_interter_globals, $block_object, $ai_db_options_extract, $ai_wp_data, $ai_last_check;
  2269.  
  2270. if ($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_ADMIN) return;
  2271.  
  2272. $debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
  2273. $globals_name = AI_EXCERPT_COUNTER_NAME;
  2274.  
  2275. if (!isset ($ad_interter_globals [$globals_name])) {
  2276. $ad_interter_globals [$globals_name] = 1;
  2277. } else $ad_interter_globals [$globals_name] ++;
  2278.  
  2279. if ($debug_processing) ai_log ("EXCERPT HOOK START [" . $ad_interter_globals [$globals_name] . ']');
  2280.  
  2281. $ai_wp_data [AI_CONTEXT] = AI_CONTEXT_EXCERPT;
  2282.  
  2283. $ai_last_check = AI_CHECK_NONE;
  2284. $current_block = 0;
  2285.  
  2286. foreach ($ai_db_options_extract [EXCERPT_HOOK_BLOCKS][$ai_wp_data [AI_WP_PAGE_TYPE]] as $block) {
  2287. if ($debug_processing && $ai_last_check != AI_CHECK_NONE) ai_log (ai_log_block_status ($current_block, $ai_last_check));
  2288.  
  2289. if (!isset ($block_object [$block])) continue;
  2290.  
  2291. $current_block = $block;
  2292. $obj = $block_object [$block];
  2293. $obj->clear_code_cache ();
  2294.  
  2295. if (!$obj->check_server_side_detection ()) continue;
  2296. if (!$obj->check_page_types_lists_users ()) continue;
  2297. if (!$obj->check_filter ($ad_interter_globals [$globals_name])) continue;
  2298.  
  2299. // Deprecated
  2300. $ai_last_check = AI_CHECK_DISABLED_MANUALLY;
  2301. if ($obj->display_disabled ($content)) continue;
  2302.  
  2303. // Last check before insertion
  2304. if (!$obj->check_and_increment_block_counter ()) continue;
  2305.  
  2306. $ai_last_check = AI_CHECK_DEBUG_NO_INSERTION;
  2307. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_NO_INSERTION) == 0) {
  2308.  
  2309. $display_type = $obj->get_display_type ();
  2310. if ($display_type == AD_SELECT_BEFORE_EXCERPT)
  2311. $content = $obj->get_code_for_insertion () . $content; else
  2312. $content = $content . $obj->get_code_for_insertion ();
  2313.  
  2314. $ai_last_check = AI_CHECK_INSERTED;
  2315. }
  2316. }
  2317.  
  2318. if ($debug_processing && $ai_last_check != AI_CHECK_NONE) ai_log (ai_log_block_status ($current_block, $ai_last_check));
  2319.  
  2320. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_POSITIONS) != 0) {
  2321. $style = AI_DEBUG_POSITIONS_STYLE;
  2322.  
  2323. $content = "<section style='$style'>BEFORE EXCERPT ".$ad_interter_globals [$globals_name]."</section>". $content . "<section style='$style'>AFTER EXCERPT ".$ad_interter_globals [$globals_name]."</section>";
  2324.  
  2325. // Color positions from the content hook
  2326. $content = preg_replace ("/((BEFORE|AFTER) (CONTENT|PARAGRAPH) ?[\d]*)/", "<span style='color: blue;'> [$1] </span>", $content);
  2327. }
  2328.  
  2329. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_TAGS) != 0) {
  2330. // Remove marked tags from the content hook
  2331. $content = preg_replace ("/&lt;(.+?)&gt;/", "", $content);
  2332.  
  2333. // Color text to mark removed HTML tags
  2334. $content = str_replace ('[HTML TAGS REMOVED]', "<span style='color: red;'>[HTML TAGS REMOVED]</span>", $content);
  2335. }
  2336.  
  2337. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_BLOCKS) != 0) {
  2338. // Remove block labels from the content hook
  2339. if (strpos ($content, '>[AI]<') === false)
  2340. $content = preg_replace ("/\[AI\](.+?)\[\/AI\]/", "", $content);
  2341. }
  2342.  
  2343. if ($debug_processing) ai_log ("EXCERPT HOOK END\n");
  2344.  
  2345. return $content;
  2346. }
  2347.  
  2348. // Process Before / After Post postion
  2349.  
  2350. function ai_before_after_post ($query, $display_type){
  2351. global $block_object, $ad_interter_globals, $ai_db_options_extract, $ai_wp_data, $ai_last_check;
  2352.  
  2353. $debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
  2354.  
  2355. if ($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_ADMIN) return;
  2356.  
  2357. if (!method_exists ($query, 'is_main_query')) return;
  2358. if (!$query->is_main_query()) return;
  2359.  
  2360. $globals_name = $display_type == AD_SELECT_BEFORE_POST ? AI_LOOP_BEFORE_COUNTER_NAME : AI_LOOP_AFTER_COUNTER_NAME;
  2361.  
  2362. if (!isset ($ad_interter_globals [$globals_name])) {
  2363. $ad_interter_globals [$globals_name] = 1;
  2364. } else $ad_interter_globals [$globals_name] ++;
  2365.  
  2366. $ai_wp_data [AI_CONTEXT] = $display_type == AD_SELECT_BEFORE_POST ? AI_CONTEXT_BEFORE_POST : AI_CONTEXT_AFTER_POST;
  2367.  
  2368. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_POSITIONS) != 0) {
  2369.  
  2370. $style = AI_DEBUG_POSITIONS_STYLE;
  2371.  
  2372. $counter = $ad_interter_globals [$globals_name];
  2373. if ($counter == 1) $counter = '';
  2374.  
  2375. echo "<section style='$style'>".($display_type == AD_SELECT_BEFORE_POST ? "BEFORE" : "AFTER")." POST ".$counter."</section>";
  2376. }
  2377.  
  2378. $meta_value = get_post_meta (get_the_ID (), '_adinserter_block_exceptions', true);
  2379. $selected_blocks = explode (",", $meta_value);
  2380.  
  2381. $ad_code = "";
  2382.  
  2383. $ai_last_check = AI_CHECK_NONE;
  2384. $current_block = 0;
  2385.  
  2386. foreach ($ai_db_options_extract [$display_type == AD_SELECT_BEFORE_POST ? LOOP_START_HOOK_BLOCKS : LOOP_END_HOOK_BLOCKS][$ai_wp_data [AI_WP_PAGE_TYPE]] as $block) {
  2387. if ($debug_processing && $ai_last_check != AI_CHECK_NONE) ai_log (ai_log_block_status ($current_block, $ai_last_check));
  2388.  
  2389. if (!isset ($block_object [$block])) continue;
  2390.  
  2391. $current_block = $block;
  2392.  
  2393. $obj = $block_object [$block];
  2394. $obj->clear_code_cache ();
  2395.  
  2396. if (!$obj->check_server_side_detection ()) continue;
  2397. if (!$obj->check_page_types_lists_users ()) continue;
  2398. if (!$obj->check_post_page_exceptions ($selected_blocks)) continue;
  2399. if (!$obj->check_filter ($ad_interter_globals [$globals_name])) continue;
  2400.  
  2401. // Last check before insertion
  2402. if (!$obj->check_and_increment_block_counter ()) continue;
  2403.  
  2404. $ai_last_check = AI_CHECK_DEBUG_NO_INSERTION;
  2405. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_NO_INSERTION) == 0) {
  2406. $ad_code .= $obj->get_code_for_insertion ();
  2407. $ai_last_check = AI_CHECK_INSERTED;
  2408. }
  2409. }
  2410. if ($debug_processing && $ai_last_check != AI_CHECK_NONE) ai_log (ai_log_block_status ($current_block, $ai_last_check));
  2411.  
  2412. echo $ad_code;
  2413. }
  2414.  
  2415. // Process Before Post postion
  2416. function ai_loop_start_hook ($query){
  2417. global $ai_wp_data;
  2418. $debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
  2419. if ($debug_processing) ai_log ("LOOP START HOOK START");
  2420. ai_before_after_post ($query, AD_SELECT_BEFORE_POST);
  2421. if ($debug_processing) ai_log ("LOOP START HOOK END\n");
  2422. }
  2423.  
  2424.  
  2425. // Process After Post postion
  2426. function ai_loop_end_hook ($query){
  2427. global $ai_wp_data;
  2428. $debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
  2429. if ($debug_processing) ai_log ("LOOP END HOOK START");
  2430. ai_before_after_post ($query, AD_SELECT_AFTER_POST);
  2431. if ($debug_processing) ai_log ("LOOP END HOOK END\n");
  2432. }
  2433.  
  2434. function process_shortcode (&$block, $atts) {
  2435. global $block_object, $ai_last_check, $ai_wp_data;
  2436.  
  2437. $parameters = shortcode_atts (array (
  2438. "block" => "",
  2439. "name" => "",
  2440. "ignore" => "",
  2441. ), $atts);
  2442.  
  2443. $block = - 1;
  2444. if (is_numeric ($parameters ['block'])) {
  2445. $block = intval ($parameters ['block']);
  2446. } elseif ($parameters ['name'] != '') {
  2447. $shortcode_name = strtolower ($parameters ['name']);
  2448. if ($shortcode_name == 'debugger') $block = 0; else {
  2449. for ($counter = 1; $counter <= AD_INSERTER_BLOCKS; $counter ++) {
  2450. $obj = $block_object [$counter];
  2451. $ad_name = strtolower (trim ($obj->get_ad_name()));
  2452. if ($shortcode_name == $ad_name) {
  2453. $block = $counter;
  2454. break;
  2455. }
  2456. }
  2457. }
  2458. }
  2459.  
  2460. if ($block == 0) {
  2461. if (get_remote_debugging () || ($ai_wp_data [AI_WP_USER] & AI_USER_ADMINISTRATOR) != 0) {
  2462. ob_start ();
  2463. echo "<pre style='", AI_DEBUG_WIDGET_STYLE, "'>\n";
  2464. ai_write_debug_info ();
  2465. echo "</pre>";
  2466. return ob_get_clean ();
  2467. }
  2468. return "";
  2469. }
  2470.  
  2471. $ai_last_check = AI_CHECK_SHORTCODE_ATTRIBUTES;
  2472. if ($block < 1 || $block > AD_INSERTER_BLOCKS) return "";
  2473.  
  2474. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) ai_log ("SHORTCODE $block (".($parameters ['block'] != '' ? 'block="'.$parameters ['block'].'"' : '').($parameters ['name'] != '' ? 'name="'.$parameters ['name'].'"' : '').")");
  2475.  
  2476. // IGNORE SETTINGS
  2477. // page_type
  2478. // *block_counter
  2479.  
  2480. $ignore_array = array ();
  2481. if (trim ($parameters ['ignore']) != '') {
  2482. $ignore_array = explode (",", str_replace (" ", "", $parameters ['ignore']));
  2483. }
  2484.  
  2485. $ai_wp_data [AI_CONTEXT] = AI_CONTEXT_SHORTCODE;
  2486.  
  2487. $obj = $block_object [$block];
  2488. $obj->clear_code_cache ();
  2489.  
  2490. $ai_last_check = AI_CHECK_ENABLED;
  2491. if (!$obj->get_enable_manual ()) return "";
  2492.  
  2493. if (!$obj->check_server_side_detection ()) return "";
  2494. if (!$obj->check_page_types_lists_users (in_array ("page_type", $ignore_array))) return "";
  2495.  
  2496. // Last check before insertion
  2497. if (!$obj->check_and_increment_block_counter ()) return "";
  2498.  
  2499. $ai_last_check = AI_CHECK_DEBUG_NO_INSERTION;
  2500. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_NO_INSERTION) == 0) {
  2501. $ai_last_check = AI_CHECK_INSERTED;
  2502. return $obj->get_code_for_insertion ();
  2503. }
  2504. }
  2505.  
  2506.  
  2507. function process_shortcodes ($atts) {
  2508. global $ai_last_check, $ai_wp_data;
  2509.  
  2510. $debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
  2511. if ($debug_processing) {
  2512. $atts_string = '';
  2513. foreach ($atts as $index => $att) {
  2514. $atts_string .= $index . '="'.$att.'" ';
  2515. }
  2516. ai_log ("PROCESS SHORTCODES [adinserter ".trim ($atts_string).']');
  2517. }
  2518. $ai_last_check = AI_CHECK_NONE;
  2519. $block = - 1;
  2520. $shortcode = process_shortcode ($block, $atts);
  2521. if ($debug_processing && $ai_last_check != AI_CHECK_NONE) ai_log (ai_log_block_status ($block, $ai_last_check));
  2522. if ($debug_processing) ai_log ("SHORTCODE END\n");
  2523. return $shortcode;
  2524. }
  2525.  
  2526.  
  2527. class ai_widget extends WP_Widget {
  2528.  
  2529. function __construct () {
  2530. parent::__construct (
  2531. false, // Base ID
  2532. AD_INSERTER_NAME, // Name
  2533. array ( // Args
  2534. 'classname' => 'ai_widget',
  2535. 'description' => AD_INSERTER_NAME.' code block widget.')
  2536. );
  2537. }
  2538.  
  2539. function form ($instance) {
  2540. global $block_object;
  2541.  
  2542. // Output admin widget options form
  2543.  
  2544. $widget_title = !empty ($instance ['widget-title']) ? $instance ['widget-title'] : '';
  2545. $block = isset ($instance ['block']) ? $instance ['block'] : 1;
  2546.  
  2547. if ($block == 0) $title = 'Debugger'; else {
  2548. $obj = $block_object [$block];
  2549.  
  2550. $title = '[' . $block . '] ' . $obj->get_ad_name();
  2551. if (!empty ($widget_title)) $title .= ' - ' . $widget_title;
  2552. if (!$obj->get_enable_widget ()) $title .= ' - DISABLED';
  2553. }
  2554.  
  2555. ?>
  2556. <input id="<?php echo $this->get_field_id ('title'); ?>" name="<?php echo $this->get_field_name ('title'); ?>" type="hidden" value="<?php echo esc_attr ($title); ?>">
  2557.  
  2558. <p>
  2559. <label for="<?php echo $this->get_field_id ('widget-title'); ?>">Title:</label>
  2560. <input id="<?php echo $this->get_field_id ('widget-title'); ?>" name="<?php echo $this->get_field_name ('widget-title'); ?>" type="text" value="<?php echo esc_attr ($widget_title); ?>" style="width: 90%;">
  2561. </p>
  2562.  
  2563. <p>
  2564. <label for="<?php echo $this->get_field_id ('block'); ?>">Block:</label>
  2565. <select id="<?php echo $this->get_field_id ('block'); ?>" name="<?php echo $this->get_field_name('block'); ?>" style="width: 88%;">
  2566. <?php
  2567. for ($block_index = 1; $block_index <= AD_INSERTER_BLOCKS; $block_index ++) {
  2568. $obj = $block_object [$block_index];
  2569. ?>
  2570. <option value='<?php echo $block_index; ?>' <?php if ($block_index == $block) echo 'selected="selected"'; ?>><?php echo $obj->get_ad_name(), !$obj->get_enable_widget ()? ' - DISABLED' : ''; ?></option>
  2571. <?php } ?>
  2572. <option value='0' <?php if ($block == 0) echo 'selected="selected"'; ?>>Debugger</option>
  2573. </select>
  2574. </p>
  2575.  
  2576. <?php
  2577. $url_parameters = "";
  2578. if (function_exists ('ai_settings_url_parameters')) $url_parameters = ai_settings_url_parameters ($block);
  2579.  
  2580. echo "<p><a href='", admin_url ('options-general.php?page=ad-inserter.php'), $url_parameters, "&tab=", $block, "'>Settings</a></p>";
  2581. }
  2582.  
  2583. function update ($new_instance, $old_instance) {
  2584. // Save widget options
  2585. $instance = $old_instance;
  2586.  
  2587. $instance ['widget-title'] = (!empty ($new_instance ['widget-title'])) ? strip_tags ($new_instance ['widget-title']) : '';
  2588. $instance ['title'] = (!empty ($new_instance ['title'])) ? strip_tags ($new_instance ['title']) : '';
  2589. $instance ['block'] = (isset ($new_instance ['block'])) ? $new_instance ['block'] : 1;
  2590.  
  2591. return $instance;
  2592. }
  2593.  
  2594. function widget ($args, $instance) {
  2595. global $ai_last_check, $ai_wp_data;
  2596.  
  2597. $ai_last_check = AI_CHECK_NONE;
  2598.  
  2599. $block = 0;
  2600. ai_widget_draw ($args, $instance, $block);
  2601.  
  2602. $debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
  2603.  
  2604. if ($debug_processing && $ai_last_check != AI_CHECK_NONE) ai_log (ai_log_block_status ($block, $ai_last_check));
  2605. if ($debug_processing) ai_log ("WIDGET END\n");
  2606. }
  2607. }
  2608.  
  2609.  
  2610. function ai_widget_draw ($args, $instance, &$block) {
  2611. global $block_object, $ad_interter_globals, $ai_wp_data, $ai_last_check;
  2612.  
  2613. $block = isset ($instance ['block']) ? $instance ['block'] : 1;
  2614.  
  2615. if ($block == 0) {
  2616. if (get_remote_debugging () || ($ai_wp_data [AI_WP_USER] & AI_USER_ADMINISTRATOR) != 0)
  2617. ai_widget_draw_debugger ($args, $instance, $block);
  2618. return;
  2619. }
  2620.  
  2621. if ($block < 1 || $block > AD_INSERTER_BLOCKS) return;
  2622.  
  2623. $title = !empty ($instance ['widget-title']) ? $instance ['widget-title'] : '';
  2624.  
  2625. $obj = $block_object [$block];
  2626. $obj->clear_code_cache ();
  2627.  
  2628. $globals_name = AI_WIDGET_COUNTER_NAME . $block;
  2629.  
  2630. if (!isset ($ad_interter_globals [$globals_name])) {
  2631. $ad_interter_globals [$globals_name] = 1;
  2632. } else $ad_interter_globals [$globals_name] ++;
  2633.  
  2634. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) ai_log ("WIDGET (". $obj->number . ') ['.$ad_interter_globals [$globals_name] . ']');
  2635.  
  2636. $ai_wp_data [AI_CONTEXT] = AI_CONTEXT_WIDGET;
  2637.  
  2638. $ai_last_check = AI_CHECK_ENABLED;
  2639. if (!$obj->get_enable_widget ()) return;
  2640. if (!$obj->check_server_side_detection ()) return;
  2641. if (!$obj->check_page_types_lists_users ()) return;
  2642. if (!$obj->check_filter ($ad_interter_globals [$globals_name])) return;
  2643.  
  2644. if ($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_POST || $ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_STATIC) {
  2645. $meta_value = get_post_meta (get_the_ID (), '_adinserter_block_exceptions', true);
  2646. $selected_blocks = explode (",", $meta_value);
  2647.  
  2648. if (!$obj->check_post_page_exceptions ($selected_blocks)) return;
  2649. }
  2650.  
  2651. // Last check before insertion
  2652. if (!$obj->check_and_increment_block_counter ()) return;
  2653.  
  2654. $ai_last_check = AI_CHECK_DEBUG_NO_INSERTION;
  2655. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_NO_INSERTION) == 0) {
  2656. $viewport_classes = trim ($obj->get_viewport_classes ());
  2657. if ($viewport_classes != "") echo "<div class='" . $viewport_classes . "'>";
  2658. echo $args ['before_widget'];
  2659.  
  2660. if (!empty ($title)) {
  2661. echo $args ['before_title'], apply_filters ('widget_title', $title), $args ['after_title'];
  2662. }
  2663.  
  2664. echo $obj->get_code_for_insertion (false);
  2665.  
  2666. echo $args ['after_widget'];
  2667. if ($viewport_classes != "") echo "</div>";
  2668.  
  2669. $ai_last_check = AI_CHECK_INSERTED;
  2670.  
  2671. if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_BLOCKS) != 0 && $obj->get_detection_client_side())
  2672. echo $obj->get_code_for_insertion (false, true);
  2673. }
  2674. }
  2675.  
  2676. function ai_widget_draw_debugger ($args, $instance, &$block) {
  2677. global $ai_wp_data, $ai_db_options, $block_object;
  2678.  
  2679. echo $args ['before_widget'];
  2680.  
  2681. $title = !empty ($instance ['widget-title']) ? $instance ['widget-title'] : '';
  2682.  
  2683. if (!empty ($title)) {
  2684. echo $args ['before_title'], apply_filters ('widget_title', $title), $args ['after_title'];
  2685. }
  2686.  
  2687. echo "<pre style='", AI_DEBUG_WIDGET_STYLE, "'>\n";
  2688. ai_write_debug_info ();
  2689. echo "</pre>";
  2690.  
  2691. if ($ai_wp_data [CLIENT_SIDE_DETECTION]) {
  2692. for ($viewport = 1; $viewport <= AD_INSERTER_VIEWPORTS; $viewport ++) {
  2693. $viewport_name = get_viewport_name ($viewport);
  2694. if ($viewport_name != '') {
  2695. echo "<pre class='ai-viewport-" . $viewport ."' style='", AI_DEBUG_WIDGET_STYLE, "'>\n";
  2696. echo "CLIENT-SIDE DEVICE: ", $viewport_name;
  2697. echo "</pre>";
  2698. }
  2699. }
  2700. }
  2701.  
  2702. echo $args ['after_widget'];
  2703. }
Add Comment
Please, Sign In to add comment