Ad Inserter – WordPress Ads Management with AdSense Header Integration - Version 2.1.4

Version Description

  • Paragraph counting restored to standard functions
  • Added option to select paragraph counting functions with multibyte support (unicode characters)
  • Fixed bug for wrong paragraph counting in posts with blockquote sections in some cases
  • Fixed bug for wrong measuring of plugin processing time in some cases
  • Fixed bug for "Empty delimiter" warning
Download this release

Release Info

Developer spacetime
Plugin Icon 128x128 Ad Inserter – WordPress Ads Management with AdSense Header Integration
Version 2.1.4
Comparing to
See all releases

Code changes from version 2.1.3 to 2.1.4

Files changed (7) hide show
  1. ad-inserter.php +60 -43
  2. class.php +616 -16
  3. constants.php +9 -1
  4. css/ad-inserter.css +1 -1
  5. js/ad-inserter.js +1 -1
  6. readme.txt +13 -1
  7. settings.php +11 -0
ad-inserter.php CHANGED
@@ -1,7 +1,7 @@
1
  <?php
2
  /*
3
  Plugin Name: Ad Inserter
4
- Version: 2.1.3
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/
@@ -11,6 +11,13 @@ Plugin URI: http://adinserter.pro/documentation
11
  /*
12
  Change Log
13
 
 
 
 
 
 
 
 
14
  Ad Inserter 2.1.3 - 11 March 2017
15
  - Added support for counting paragraphs with multibyte (unicode) characters
16
  - Fixed bug for class name not saved
@@ -390,9 +397,9 @@ if (!is_admin()) {
390
  $ai_wp_data [AI_WP_DEBUGGING] |= AI_DEBUG_PROCESSING;
391
 
392
  $ai_total_plugin_time = 0;
393
- $start_time = microtime ();
394
  $ai_total_php_time = 0;
395
- $ai_last_time = microtime ();
396
  $ai_processing_log = array ();
397
  ai_log ("INITIALIZATION START");
398
  }
@@ -499,7 +506,7 @@ add_action ('wp_ajax_ai_data', 'ai_data');
499
  add_action ('wp_ajax_nopriv_ai_data', 'ai_data');
500
 
501
  if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) {
502
- $ai_total_plugin_time += microtime () - $start_time;
503
  ai_log ("INITIALIZATION END\n");
504
  }
505
 
@@ -662,7 +669,7 @@ function set_page_type () {
662
 
663
  function ai_log_message ($message) {
664
  global $ai_last_time, $ai_processing_log;
665
- $ai_processing_log []= rtrim (sprintf ("%4d %-50s", (microtime () - $ai_last_time) * 1000, $message));
666
  }
667
 
668
  function ai_log_filter_content ($content_string) {
@@ -756,7 +763,7 @@ function ai_log ($message = "") {
756
  $ai_processing_log []= "";
757
  } else ai_log_message ($message);
758
  } else $ai_processing_log []= "";
759
- $ai_last_time = microtime ();
760
  }
761
 
762
  function remove_parameters_from_url ($url, $parameter = '') {
@@ -813,7 +820,7 @@ function ai_wp_hook () {
813
 
814
  if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) {
815
  ai_log ("WP HOOK START");
816
- $start_time = microtime ();
817
  }
818
 
819
  set_page_type ();
@@ -887,7 +894,7 @@ function ai_wp_hook () {
887
  add_action ('the_post', 'ai_post_hook');
888
 
889
  if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) {
890
- $ai_total_plugin_time += microtime () - $start_time;
891
  ai_log ("WP HOOK END\n");
892
  }
893
  };
@@ -1406,7 +1413,7 @@ function ai_wp_head_hook () {
1406
 
1407
  if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) {
1408
  ai_log ("HEAD HOOK START");
1409
- $start_time = microtime ();
1410
  }
1411
 
1412
  add_head_inline_styles_and_scripts ();
@@ -1424,7 +1431,7 @@ function ai_wp_head_hook () {
1424
  }
1425
 
1426
  if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) {
1427
- $ai_total_plugin_time += microtime () - $start_time;
1428
  ai_log ("HEAD HOOK END\n");
1429
  }
1430
  }
@@ -1434,7 +1441,7 @@ function ai_wp_footer_hook () {
1434
 
1435
  if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) {
1436
  ai_log ("FOOTER HOOK START");
1437
- $start_time = microtime ();
1438
  }
1439
 
1440
  add_footer_inline_scripts ();
@@ -1452,7 +1459,7 @@ function ai_wp_footer_hook () {
1452
  }
1453
 
1454
  if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) {
1455
- $ai_total_plugin_time += microtime () - $start_time;
1456
  ai_log ("FOOTER HOOK END\n");
1457
  }
1458
  }
@@ -1465,9 +1472,9 @@ function ai_write_debug_info ($write_processing_log = false) {
1465
  echo "\n\n";
1466
  echo "GENERATED (WP time): ", date ("Y-m-d H:i:s", time() + get_option ('gmt_offset') * 3600), "\n";
1467
  echo "GENERATED (Server time): ", date ("Y-m-d H:i:s", time()), "\n";
1468
- echo "PLUGIN CODE PROCESSING: ", number_format (($ai_total_plugin_time - $ai_total_php_time) * 1000, 2) , " ms\n";
1469
- echo "USER CODE PROCESSING: ", number_format ($ai_total_php_time * 1000, 2), " ms\n";
1470
- echo "TOTAL PROCESSING TIME: ", number_format ($ai_total_plugin_time * 1000, 2), " ms\n";
1471
 
1472
  echo "SETTINGS: ";
1473
  if (isset ($ai_db_options [AI_GLOBAL_OPTION_NAME]['VERSION']))
@@ -1745,10 +1752,11 @@ function ai_check_plugin_options ($plugin_options = array ()) {
1745
  $plugin_options ['PLUGIN_PRIORITY'] = $plugin_priority;
1746
 
1747
 
1748
- if (!isset ($plugin_options ['DYNAMIC_BLOCKS'])) $plugin_options ['DYNAMIC_BLOCKS'] = DEFAULT_DYNAMIC_BLOCKS;
1749
- if (!isset ($plugin_options ['ADMIN_TOOLBAR_DEBUGGING'])) $plugin_options ['ADMIN_TOOLBAR_DEBUGGING'] = DEFAULT_ADMIN_TOOLBAR_DEBUGGING;
1750
- if (!isset ($plugin_options ['REMOTE_DEBUGGING'])) $plugin_options ['REMOTE_DEBUGGING'] = DEFAULT_REMOTE_DEBUGGING;
1751
- if (!isset ($plugin_options ['JAVASCRIPT_DEBUGGING'])) $plugin_options ['JAVASCRIPT_DEBUGGING'] = DEFAULT_JAVASCRIPT_DEBUGGING;
 
1752
 
1753
  for ($viewport = 1; $viewport <= AD_INSERTER_VIEWPORTS; $viewport ++) {
1754
  $viewport_name_option_name = 'VIEWPORT_NAME_' . $viewport;
@@ -1883,6 +1891,14 @@ function get_dynamic_blocks(){
1883
  return ($ai_db_options [AI_GLOBAL_OPTION_NAME]['DYNAMIC_BLOCKS']);
1884
  }
1885
 
 
 
 
 
 
 
 
 
1886
  function get_admin_toolbar_debugging () {
1887
  global $ai_db_options;
1888
 
@@ -2395,14 +2411,15 @@ function ai_settings () {
2395
 
2396
  if (function_exists ('ai_filter_global_settings')) ai_filter_global_settings ($options);
2397
 
2398
- $options ['SYNTAX_HIGHLIGHTER_THEME'] = filter_string ($_POST ['syntax-highlighter-theme']);
2399
- $options ['BLOCK_CLASS_NAME'] = filter_html_class ($_POST ['block-class-name']);
2400
- $options ['MINIMUM_USER_ROLE'] = filter_string ($_POST ['minimum-user-role']);
2401
- $options ['PLUGIN_PRIORITY'] = filter_option ('plugin_priority', $_POST ['plugin_priority']);
2402
- $options ['DYNAMIC_BLOCKS'] = filter_option ('dynamic_blocks', $_POST ['dynamic_blocks']);
2403
- $options ['ADMIN_TOOLBAR_DEBUGGING'] = filter_option ('admin_toolbar_debugging', $_POST ['admin_toolbar_debugging']);
2404
- $options ['REMOTE_DEBUGGING'] = filter_option ('remote_debugging', $_POST ['remote_debugging']);
2405
- $options ['JAVASCRIPT_DEBUGGING'] = filter_option ('javascript_debugging', $_POST ['javascript_debugging']);
 
2406
 
2407
  for ($viewport = 1; $viewport <= AD_INSERTER_VIEWPORTS; $viewport ++) {
2408
  if (isset ($_POST ['viewport-name-'.$viewport]))
@@ -2609,13 +2626,13 @@ function adinserter ($block = '', $ignore = '') {
2609
  global $ai_last_check, $ai_wp_data, $ai_total_plugin_time;
2610
 
2611
  $debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
2612
- if ($debug_processing) $start_time = microtime ();
2613
 
2614
  $ai_last_check = AI_CHECK_NONE;
2615
  $code = ai_adinserter ($block, $ignore);
2616
 
2617
  if ($debug_processing) {
2618
- $ai_total_plugin_time += microtime () - $start_time;
2619
  if ($ai_last_check != AI_CHECK_NONE) ai_log (ai_log_block_status ($block, $ai_last_check));
2620
  ai_log ("PHP FUNCTION CALL END\n");
2621
  }
@@ -2639,7 +2656,7 @@ function ai_content_hook ($content = '') {
2639
 
2640
  if ($debug_processing) {
2641
  ai_log ("CONTENT HOOK START [" . $ad_inserter_globals [$globals_name] . ']');
2642
- $start_time = microtime ();
2643
  }
2644
 
2645
  $ai_wp_data [AI_CONTEXT] = AI_CONTEXT_CONTENT;
@@ -2780,7 +2797,7 @@ function ai_content_hook ($content = '') {
2780
  }
2781
 
2782
  if ($debug_processing) {
2783
- $ai_total_plugin_time += microtime () - $start_time;
2784
  ai_log ("CONTENT HOOK END\n");
2785
  }
2786
 
@@ -2802,7 +2819,7 @@ function ai_excerpt_hook ($content = '') {
2802
 
2803
  if ($debug_processing) {
2804
  ai_log ("EXCERPT HOOK START [" . $ad_inserter_globals [$globals_name] . ']');
2805
- $start_time = microtime ();
2806
  }
2807
 
2808
  $ai_wp_data [AI_CONTEXT] = AI_CONTEXT_EXCERPT;
@@ -2872,7 +2889,7 @@ function ai_excerpt_hook ($content = '') {
2872
  }
2873
 
2874
  if ($debug_processing) {
2875
- $ai_total_plugin_time += microtime () - $start_time;
2876
  ai_log ("EXCERPT HOOK END\n");
2877
  }
2878
 
@@ -2958,11 +2975,11 @@ function ai_loop_start_hook ($query) {
2958
  $debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
2959
  if ($debug_processing) {
2960
  ai_log ("LOOP START HOOK START");
2961
- $start_time = microtime ();
2962
  }
2963
  ai_before_after_post ($query, AI_AUTOMATIC_INSERTION_BEFORE_POST);
2964
  if ($debug_processing) {
2965
- $ai_total_plugin_time += microtime () - $start_time;
2966
  ai_log ("LOOP START HOOK END\n");
2967
  }
2968
  }
@@ -2974,11 +2991,11 @@ function ai_loop_end_hook ($query){
2974
  $debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
2975
  if ($debug_processing) {
2976
  ai_log ("LOOP END HOOK START");
2977
- $start_time = microtime ();
2978
  }
2979
  ai_before_after_post ($query, AI_AUTOMATIC_INSERTION_AFTER_POST);
2980
  if ($debug_processing) {
2981
- $ai_total_plugin_time += microtime () - $start_time;
2982
  ai_log ("LOOP END HOOK END\n");
2983
  }
2984
  }
@@ -3005,7 +3022,7 @@ function ai_post_hook ($post) {
3005
  $debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
3006
  if ($debug_processing) {
3007
  ai_log ('POST HOOK START [' . $ad_inserter_globals [AI_POST_COUNTER_NAME] . ']');
3008
- $start_time = microtime ();
3009
  }
3010
 
3011
  $ai_wp_data [AI_CONTEXT] = AI_CONTEXT_BETWEEN_POSTS;
@@ -3054,7 +3071,7 @@ function ai_post_hook ($post) {
3054
  echo $ad_code;
3055
 
3056
  if ($debug_processing) {
3057
- $ai_total_plugin_time += microtime () - $start_time;
3058
  ai_log ("POST HOOK END\n");
3059
  }
3060
 
@@ -3149,14 +3166,14 @@ function process_shortcodes ($atts) {
3149
  $atts_string .= $index . '="'.$att.'" ';
3150
  }
3151
  ai_log ("PROCESS SHORTCODES [adinserter ".trim ($atts_string).']');
3152
- $start_time = microtime ();
3153
  }
3154
  $ai_last_check = AI_CHECK_NONE;
3155
  $block = - 1;
3156
  $shortcode = process_shortcode ($block, $atts);
3157
 
3158
  if ($debug_processing) {
3159
- $ai_total_plugin_time += microtime () - $start_time;
3160
  if ($ai_last_check != AI_CHECK_NONE) ai_log (ai_log_block_status ($block, $ai_last_check));
3161
  ai_log ("SHORTCODE END\n");
3162
  }
@@ -3236,7 +3253,7 @@ class ai_widget extends WP_Widget {
3236
  global $ai_last_check, $ai_wp_data, $ai_total_plugin_time;
3237
 
3238
  $debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
3239
- if ($debug_processing) $start_time = microtime ();
3240
 
3241
  $ai_last_check = AI_CHECK_NONE;
3242
 
@@ -3244,7 +3261,7 @@ class ai_widget extends WP_Widget {
3244
  ai_widget_draw ($args, $instance, $block);
3245
 
3246
  if ($debug_processing) {
3247
- $ai_total_plugin_time += microtime () - $start_time;
3248
  if ($ai_last_check != AI_CHECK_NONE) ai_log (ai_log_block_status ($block, $ai_last_check));
3249
  ai_log ("WIDGET END\n");
3250
  }
1
  <?php
2
  /*
3
  Plugin Name: Ad Inserter
4
+ Version: 2.1.4
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/
11
  /*
12
  Change Log
13
 
14
+ Ad Inserter 2.1.4 - 12 March 2017
15
+ - Paragraph counting restored to standard functions
16
+ - Added option to select paragraph counting functions with multibyte support (unicode characters)
17
+ - Fixed bug for wrong paragraph counting in posts with blockquote sections in some cases
18
+ - Fixed bug for wrong measuring of plugin processing time in some cases
19
+ - Fixed bug for "Empty delimiter" warning
20
+
21
  Ad Inserter 2.1.3 - 11 March 2017
22
  - Added support for counting paragraphs with multibyte (unicode) characters
23
  - Fixed bug for class name not saved
397
  $ai_wp_data [AI_WP_DEBUGGING] |= AI_DEBUG_PROCESSING;
398
 
399
  $ai_total_plugin_time = 0;
400
+ $start_time = microtime (true);
401
  $ai_total_php_time = 0;
402
+ $ai_last_time = microtime (true);
403
  $ai_processing_log = array ();
404
  ai_log ("INITIALIZATION START");
405
  }
506
  add_action ('wp_ajax_nopriv_ai_data', 'ai_data');
507
 
508
  if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) {
509
+ $ai_total_plugin_time += microtime (true) - $start_time;
510
  ai_log ("INITIALIZATION END\n");
511
  }
512
 
669
 
670
  function ai_log_message ($message) {
671
  global $ai_last_time, $ai_processing_log;
672
+ $ai_processing_log []= rtrim (sprintf ("%4d %-50s", (microtime (true) - $ai_last_time) * 1000, $message));
673
  }
674
 
675
  function ai_log_filter_content ($content_string) {
763
  $ai_processing_log []= "";
764
  } else ai_log_message ($message);
765
  } else $ai_processing_log []= "";
766
+ $ai_last_time = microtime (true);
767
  }
768
 
769
  function remove_parameters_from_url ($url, $parameter = '') {
820
 
821
  if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) {
822
  ai_log ("WP HOOK START");
823
+ $start_time = microtime (true);
824
  }
825
 
826
  set_page_type ();
894
  add_action ('the_post', 'ai_post_hook');
895
 
896
  if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) {
897
+ $ai_total_plugin_time += microtime (true) - $start_time;
898
  ai_log ("WP HOOK END\n");
899
  }
900
  };
1413
 
1414
  if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) {
1415
  ai_log ("HEAD HOOK START");
1416
+ $start_time = microtime (true);
1417
  }
1418
 
1419
  add_head_inline_styles_and_scripts ();
1431
  }
1432
 
1433
  if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) {
1434
+ $ai_total_plugin_time += microtime (true) - $start_time;
1435
  ai_log ("HEAD HOOK END\n");
1436
  }
1437
  }
1441
 
1442
  if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) {
1443
  ai_log ("FOOTER HOOK START");
1444
+ $start_time = microtime (true);
1445
  }
1446
 
1447
  add_footer_inline_scripts ();
1459
  }
1460
 
1461
  if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) {
1462
+ $ai_total_plugin_time += microtime (true) - $start_time;
1463
  ai_log ("FOOTER HOOK END\n");
1464
  }
1465
  }
1472
  echo "\n\n";
1473
  echo "GENERATED (WP time): ", date ("Y-m-d H:i:s", time() + get_option ('gmt_offset') * 3600), "\n";
1474
  echo "GENERATED (Server time): ", date ("Y-m-d H:i:s", time()), "\n";
1475
+ echo "PLUGIN CODE PROCESSING: ", number_format (($ai_total_plugin_time - $ai_total_php_time) * 1000, 2, '.' , '') , " ms\n";
1476
+ echo "USER CODE PROCESSING: ", number_format ($ai_total_php_time * 1000, 2, '.' , ''), " ms\n";
1477
+ echo "TOTAL PROCESSING TIME: ", number_format ($ai_total_plugin_time * 1000, 2, '.' , ''), " ms\n";
1478
 
1479
  echo "SETTINGS: ";
1480
  if (isset ($ai_db_options [AI_GLOBAL_OPTION_NAME]['VERSION']))
1752
  $plugin_options ['PLUGIN_PRIORITY'] = $plugin_priority;
1753
 
1754
 
1755
+ if (!isset ($plugin_options ['DYNAMIC_BLOCKS'])) $plugin_options ['DYNAMIC_BLOCKS'] = DEFAULT_DYNAMIC_BLOCKS;
1756
+ if (!isset ($plugin_options ['PARAGRAPH_COUNTING_FUNCTIONS'])) $plugin_options ['PARAGRAPH_COUNTING_FUNCTIONS'] = DEFAULT_PARAGRAPH_COUNTING_FUNCTIONS;
1757
+ if (!isset ($plugin_options ['ADMIN_TOOLBAR_DEBUGGING'])) $plugin_options ['ADMIN_TOOLBAR_DEBUGGING'] = DEFAULT_ADMIN_TOOLBAR_DEBUGGING;
1758
+ if (!isset ($plugin_options ['REMOTE_DEBUGGING'])) $plugin_options ['REMOTE_DEBUGGING'] = DEFAULT_REMOTE_DEBUGGING;
1759
+ if (!isset ($plugin_options ['JAVASCRIPT_DEBUGGING'])) $plugin_options ['JAVASCRIPT_DEBUGGING'] = DEFAULT_JAVASCRIPT_DEBUGGING;
1760
 
1761
  for ($viewport = 1; $viewport <= AD_INSERTER_VIEWPORTS; $viewport ++) {
1762
  $viewport_name_option_name = 'VIEWPORT_NAME_' . $viewport;
1891
  return ($ai_db_options [AI_GLOBAL_OPTION_NAME]['DYNAMIC_BLOCKS']);
1892
  }
1893
 
1894
+ function get_paragraph_counting_functions(){
1895
+ global $ai_db_options;
1896
+
1897
+ if (!isset ($ai_db_options [AI_GLOBAL_OPTION_NAME]['PARAGRAPH_COUNTING_FUNCTIONS'])) $ai_db_options [AI_GLOBAL_OPTION_NAME]['PARAGRAPH_COUNTING_FUNCTIONS'] = DEFAULT_PARAGRAPH_COUNTING_FUNCTIONS;
1898
+
1899
+ return ($ai_db_options [AI_GLOBAL_OPTION_NAME]['PARAGRAPH_COUNTING_FUNCTIONS']);
1900
+ }
1901
+
1902
  function get_admin_toolbar_debugging () {
1903
  global $ai_db_options;
1904
 
2411
 
2412
  if (function_exists ('ai_filter_global_settings')) ai_filter_global_settings ($options);
2413
 
2414
+ $options ['SYNTAX_HIGHLIGHTER_THEME'] = filter_string ($_POST ['syntax-highlighter-theme']);
2415
+ $options ['BLOCK_CLASS_NAME'] = filter_html_class ($_POST ['block-class-name']);
2416
+ $options ['MINIMUM_USER_ROLE'] = filter_string ($_POST ['minimum-user-role']);
2417
+ $options ['PLUGIN_PRIORITY'] = filter_option ('plugin_priority', $_POST ['plugin_priority']);
2418
+ $options ['DYNAMIC_BLOCKS'] = filter_option ('dynamic_blocks', $_POST ['dynamic_blocks']);
2419
+ $options ['PARAGRAPH_COUNTING_FUNCTIONS'] = filter_option ('paragraph_counting_functions', $_POST ['paragraph_counting_functions']);
2420
+ $options ['ADMIN_TOOLBAR_DEBUGGING'] = filter_option ('admin_toolbar_debugging', $_POST ['admin_toolbar_debugging']);
2421
+ $options ['REMOTE_DEBUGGING'] = filter_option ('remote_debugging', $_POST ['remote_debugging']);
2422
+ $options ['JAVASCRIPT_DEBUGGING'] = filter_option ('javascript_debugging', $_POST ['javascript_debugging']);
2423
 
2424
  for ($viewport = 1; $viewport <= AD_INSERTER_VIEWPORTS; $viewport ++) {
2425
  if (isset ($_POST ['viewport-name-'.$viewport]))
2626
  global $ai_last_check, $ai_wp_data, $ai_total_plugin_time;
2627
 
2628
  $debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
2629
+ if ($debug_processing) $start_time = microtime (true);
2630
 
2631
  $ai_last_check = AI_CHECK_NONE;
2632
  $code = ai_adinserter ($block, $ignore);
2633
 
2634
  if ($debug_processing) {
2635
+ $ai_total_plugin_time += microtime (true) - $start_time;
2636
  if ($ai_last_check != AI_CHECK_NONE) ai_log (ai_log_block_status ($block, $ai_last_check));
2637
  ai_log ("PHP FUNCTION CALL END\n");
2638
  }
2656
 
2657
  if ($debug_processing) {
2658
  ai_log ("CONTENT HOOK START [" . $ad_inserter_globals [$globals_name] . ']');
2659
+ $start_time = microtime (true);
2660
  }
2661
 
2662
  $ai_wp_data [AI_CONTEXT] = AI_CONTEXT_CONTENT;
2797
  }
2798
 
2799
  if ($debug_processing) {
2800
+ $ai_total_plugin_time += microtime (true) - $start_time;
2801
  ai_log ("CONTENT HOOK END\n");
2802
  }
2803
 
2819
 
2820
  if ($debug_processing) {
2821
  ai_log ("EXCERPT HOOK START [" . $ad_inserter_globals [$globals_name] . ']');
2822
+ $start_time = microtime (true);
2823
  }
2824
 
2825
  $ai_wp_data [AI_CONTEXT] = AI_CONTEXT_EXCERPT;
2889
  }
2890
 
2891
  if ($debug_processing) {
2892
+ $ai_total_plugin_time += microtime (true) - $start_time;
2893
  ai_log ("EXCERPT HOOK END\n");
2894
  }
2895
 
2975
  $debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
2976
  if ($debug_processing) {
2977
  ai_log ("LOOP START HOOK START");
2978
+ $start_time = microtime (true);
2979
  }
2980
  ai_before_after_post ($query, AI_AUTOMATIC_INSERTION_BEFORE_POST);
2981
  if ($debug_processing) {
2982
+ $ai_total_plugin_time += microtime (true) - $start_time;
2983
  ai_log ("LOOP START HOOK END\n");
2984
  }
2985
  }
2991
  $debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
2992
  if ($debug_processing) {
2993
  ai_log ("LOOP END HOOK START");
2994
+ $start_time = microtime (true);
2995
  }
2996
  ai_before_after_post ($query, AI_AUTOMATIC_INSERTION_AFTER_POST);
2997
  if ($debug_processing) {
2998
+ $ai_total_plugin_time += microtime (true) - $start_time;
2999
  ai_log ("LOOP END HOOK END\n");
3000
  }
3001
  }
3022
  $debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
3023
  if ($debug_processing) {
3024
  ai_log ('POST HOOK START [' . $ad_inserter_globals [AI_POST_COUNTER_NAME] . ']');
3025
+ $start_time = microtime (true);
3026
  }
3027
 
3028
  $ai_wp_data [AI_CONTEXT] = AI_CONTEXT_BETWEEN_POSTS;
3071
  echo $ad_code;
3072
 
3073
  if ($debug_processing) {
3074
+ $ai_total_plugin_time += microtime (true) - $start_time;
3075
  ai_log ("POST HOOK END\n");
3076
  }
3077
 
3166
  $atts_string .= $index . '="'.$att.'" ';
3167
  }
3168
  ai_log ("PROCESS SHORTCODES [adinserter ".trim ($atts_string).']');
3169
+ $start_time = microtime (true);
3170
  }
3171
  $ai_last_check = AI_CHECK_NONE;
3172
  $block = - 1;
3173
  $shortcode = process_shortcode ($block, $atts);
3174
 
3175
  if ($debug_processing) {
3176
+ $ai_total_plugin_time += microtime (true) - $start_time;
3177
  if ($ai_last_check != AI_CHECK_NONE) ai_log (ai_log_block_status ($block, $ai_last_check));
3178
  ai_log ("SHORTCODE END\n");
3179
  }
3253
  global $ai_last_check, $ai_wp_data, $ai_total_plugin_time;
3254
 
3255
  $debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
3256
+ if ($debug_processing) $start_time = microtime (true);
3257
 
3258
  $ai_last_check = AI_CHECK_NONE;
3259
 
3261
  ai_widget_draw ($args, $instance, $block);
3262
 
3263
  if ($debug_processing) {
3264
+ $ai_total_plugin_time += microtime (true) - $start_time;
3265
  if ($ai_last_check != AI_CHECK_NONE) ai_log (ai_log_block_status ($block, $ai_last_check));
3266
  ai_log ("WIDGET END\n");
3267
  }
class.php CHANGED
@@ -318,7 +318,7 @@ abstract class ai_BaseCodeBlock {
318
  $code = $obj->get_ad_data();
319
 
320
  if ($obj->get_process_php () && (!is_multisite() || is_main_site () || multisite_php_processing ())) {
321
- $start_time = microtime ();
322
 
323
  $global_name = 'GENERATED_CODE';
324
  if (isset ($obj->wp_options [$global_name])) return $obj->wp_options [$global_name];
@@ -334,7 +334,7 @@ abstract class ai_BaseCodeBlock {
334
  // Cache generated code
335
  $obj->wp_options [$global_name] = $code;
336
 
337
- $ai_total_php_time += microtime () - $start_time;
338
  }
339
 
340
  return $code;
@@ -1298,7 +1298,9 @@ abstract class ai_CodeBlock extends ai_BaseCodeBlock {
1298
  }
1299
  }
1300
 
1301
- public function before_paragraph ($content, $position_preview = false) {
 
 
1302
  global $ai_wp_data, $ai_last_check;
1303
 
1304
  $paragraph_positions = array ();
@@ -1324,7 +1326,6 @@ abstract class ai_CodeBlock extends ai_BaseCodeBlock {
1324
 
1325
  // $paragraph_start_len = strlen ($paragraph_start);
1326
  $paragraph_start_len = mb_strlen ($paragraph_start);
1327
-
1328
  // while (stripos ($content, $paragraph_start, $last_position + 1) !== false) {
1329
  // $last_position = stripos ($content, $paragraph_start, $last_position + 1);
1330
  while (mb_stripos ($content, $paragraph_start, $last_position + 1) !== false) {
@@ -1343,6 +1344,7 @@ abstract class ai_CodeBlock extends ai_BaseCodeBlock {
1343
  sort ($paragraph_positions);
1344
 
1345
  if (!$this->get_count_inside_blockquote ()) {
 
1346
  preg_match_all ('/<\/?blockquote/i', $content, $blockquotes, PREG_OFFSET_CAPTURE);
1347
 
1348
  $blockquote_offsets = array ();
@@ -1355,19 +1357,26 @@ abstract class ai_CodeBlock extends ai_BaseCodeBlock {
1355
  }
1356
 
1357
  if (count ($blockquote_offsets) != 0) {
 
1358
  $filtered_paragraph_positions = array ();
 
 
1359
  foreach ($blockquote_offsets as $blockquote_offset) {
1360
  foreach ($paragraph_positions as $paragraph_position) {
1361
- if ($paragraph_position >= $blockquote_offset [0] && $paragraph_position <= $blockquote_offset [1]) continue;
1362
- $filtered_paragraph_positions []= $paragraph_position;
1363
  }
1364
  }
 
 
 
 
 
1365
  $paragraph_positions = $filtered_paragraph_positions;
1366
  }
1367
- }
1368
 
1369
- $ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_BLOCKQUOTE;
1370
- if (count ($paragraph_positions) == 0) return $content;
 
1371
 
1372
  $paragraph_min_words = intval ($this->get_minimum_paragraph_words());
1373
  $paragraph_max_words = intval ($this->get_maximum_paragraph_words());
@@ -1386,7 +1395,6 @@ abstract class ai_CodeBlock extends ai_BaseCodeBlock {
1386
  $ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_MIN_MAX_WORDS;
1387
  if (count ($paragraph_positions) == 0) return $content;
1388
 
1389
-
1390
  $paragraph_texts = explode (",", html_entity_decode ($this->get_paragraph_text()));
1391
  if ($this->get_paragraph_text() != "" && count ($paragraph_texts) != 0) {
1392
 
@@ -1400,6 +1408,7 @@ abstract class ai_CodeBlock extends ai_BaseCodeBlock {
1400
  if ($paragraph_text_type == AD_CONTAIN) {
1401
  $found = true;
1402
  foreach ($paragraph_texts as $paragraph_text) {
 
1403
  // if (stripos ($paragraph_code, trim ($paragraph_text)) === false) {
1404
  if (mb_stripos ($paragraph_code, trim ($paragraph_text)) === false) {
1405
  $found = false;
@@ -1410,6 +1419,7 @@ abstract class ai_CodeBlock extends ai_BaseCodeBlock {
1410
  } elseif ($paragraph_text_type == AD_DO_NOT_CONTAIN) {
1411
  $found = false;
1412
  foreach ($paragraph_texts as $paragraph_text) {
 
1413
  // if (stripos ($paragraph_code, trim ($paragraph_text)) !== false) {
1414
  if (mb_stripos ($paragraph_code, trim ($paragraph_text)) !== false) {
1415
  $found = true;
@@ -1464,6 +1474,7 @@ abstract class ai_CodeBlock extends ai_BaseCodeBlock {
1464
  // $paragraph_code = substr ($content, $paragraph_positions [$paragraph_position_above], $paragraph_positions [$position] - $paragraph_positions [$paragraph_position_above]);
1465
  $paragraph_code = mb_substr ($content, $paragraph_positions [$paragraph_position_above], $paragraph_positions [$position] - $paragraph_positions [$paragraph_position_above]);
1466
  foreach ($avoid_paragraph_texts_above as $paragraph_text_above) {
 
1467
  // if (stripos ($paragraph_code, trim ($paragraph_text_above)) !== false) {
1468
  if (mb_stripos ($paragraph_code, trim ($paragraph_text_above)) !== false) {
1469
  $found_above = true;
@@ -1482,6 +1493,7 @@ abstract class ai_CodeBlock extends ai_BaseCodeBlock {
1482
  // $paragraph_code = substr ($content, $paragraph_positions [$position], $content_position_below - $paragraph_positions [$position]);
1483
  $paragraph_code = mb_substr ($content, $paragraph_positions [$position], $content_position_below - $paragraph_positions [$position]);
1484
  foreach ($avoid_paragraph_texts_below as $paragraph_text_below) {
 
1485
  // if (stripos ($paragraph_code, trim ($paragraph_text_below)) !== false) {
1486
  if (mb_stripos ($paragraph_code, trim ($paragraph_text_below)) !== false) {
1487
  $found_below = true;
@@ -1586,7 +1598,7 @@ abstract class ai_CodeBlock extends ai_BaseCodeBlock {
1586
  return $content;
1587
  }
1588
 
1589
- public function after_paragraph ($content, $position_preview = false) {
1590
  global $ai_wp_data, $ai_last_check;
1591
 
1592
  $paragraph_positions = array ();
@@ -1640,19 +1652,26 @@ abstract class ai_CodeBlock extends ai_BaseCodeBlock {
1640
  }
1641
 
1642
  if (count ($blockquote_offsets) != 0) {
 
1643
  $filtered_paragraph_positions = array ();
 
 
1644
  foreach ($blockquote_offsets as $blockquote_offset) {
1645
  foreach ($paragraph_positions as $paragraph_position) {
1646
- if ($paragraph_position >= $blockquote_offset [0] && $paragraph_position <= $blockquote_offset [1]) continue;
1647
- $filtered_paragraph_positions []= $paragraph_position;
1648
  }
1649
  }
 
 
 
 
 
1650
  $paragraph_positions = $filtered_paragraph_positions;
1651
  }
1652
- }
1653
 
1654
- $ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_BLOCKQUOTE;
1655
- if (count ($paragraph_positions) == 0) return $content;
 
1656
 
1657
  $paragraph_min_words = intval ($this->get_minimum_paragraph_words());
1658
  $paragraph_max_words = intval ($this->get_maximum_paragraph_words());
@@ -1685,6 +1704,7 @@ abstract class ai_CodeBlock extends ai_BaseCodeBlock {
1685
  if ($paragraph_text_type == AD_CONTAIN) {
1686
  $found = true;
1687
  foreach ($paragraph_texts as $paragraph_text) {
 
1688
  // if (stripos ($paragraph_code, trim ($paragraph_text)) === false) {
1689
  if (mb_stripos ($paragraph_code, trim ($paragraph_text)) === false) {
1690
  $found = false;
@@ -1695,6 +1715,7 @@ abstract class ai_CodeBlock extends ai_BaseCodeBlock {
1695
  } elseif ($paragraph_text_type == AD_DO_NOT_CONTAIN) {
1696
  $found = false;
1697
  foreach ($paragraph_texts as $paragraph_text) {
 
1698
  // if (stripos ($paragraph_code, trim ($paragraph_text)) !== false) {
1699
  if (mb_stripos ($paragraph_code, trim ($paragraph_text)) !== false) {
1700
  $found = true;
@@ -1753,6 +1774,7 @@ abstract class ai_CodeBlock extends ai_BaseCodeBlock {
1753
  $paragraph_code = mb_substr ($content, $content_position_above, $paragraph_positions [$position] - $content_position_above);
1754
 
1755
  foreach ($avoid_paragraph_texts_above as $paragraph_text_above) {
 
1756
  // if (stripos ($paragraph_code, trim ($paragraph_text_above)) !== false) {
1757
  if (mb_stripos ($paragraph_code, trim ($paragraph_text_above)) !== false) {
1758
  $found_above = true;
@@ -1768,6 +1790,7 @@ abstract class ai_CodeBlock extends ai_BaseCodeBlock {
1768
  // $paragraph_code = substr ($content, $paragraph_positions [$position] + 1, $paragraph_positions [$paragraph_position_below] - $paragraph_positions [$position]);
1769
  $paragraph_code = mb_substr ($content, $paragraph_positions [$position] + 1, $paragraph_positions [$paragraph_position_below] - $paragraph_positions [$position]);
1770
  foreach ($avoid_paragraph_texts_below as $paragraph_text_below) {
 
1771
  // if (stripos ($paragraph_code, trim ($paragraph_text_below)) !== false) {
1772
  if (mb_stripos ($paragraph_code, trim ($paragraph_text_below)) !== false) {
1773
  $found_below = true;
@@ -1877,6 +1900,583 @@ abstract class ai_CodeBlock extends ai_BaseCodeBlock {
1877
  return $content;
1878
  }
1879
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1880
  // Deprecated
1881
  function manual ($content){
1882
 
318
  $code = $obj->get_ad_data();
319
 
320
  if ($obj->get_process_php () && (!is_multisite() || is_main_site () || multisite_php_processing ())) {
321
+ $start_time = microtime (true);
322
 
323
  $global_name = 'GENERATED_CODE';
324
  if (isset ($obj->wp_options [$global_name])) return $obj->wp_options [$global_name];
334
  // Cache generated code
335
  $obj->wp_options [$global_name] = $code;
336
 
337
+ $ai_total_php_time += microtime (true) - $start_time;
338
  }
339
 
340
  return $code;
1298
  }
1299
  }
1300
 
1301
+ // Multibyte functions for international compatibility
1302
+
1303
+ public function before_paragraph_mb ($content, $position_preview = false) {
1304
  global $ai_wp_data, $ai_last_check;
1305
 
1306
  $paragraph_positions = array ();
1326
 
1327
  // $paragraph_start_len = strlen ($paragraph_start);
1328
  $paragraph_start_len = mb_strlen ($paragraph_start);
 
1329
  // while (stripos ($content, $paragraph_start, $last_position + 1) !== false) {
1330
  // $last_position = stripos ($content, $paragraph_start, $last_position + 1);
1331
  while (mb_stripos ($content, $paragraph_start, $last_position + 1) !== false) {
1344
  sort ($paragraph_positions);
1345
 
1346
  if (!$this->get_count_inside_blockquote ()) {
1347
+
1348
  preg_match_all ('/<\/?blockquote/i', $content, $blockquotes, PREG_OFFSET_CAPTURE);
1349
 
1350
  $blockquote_offsets = array ();
1357
  }
1358
 
1359
  if (count ($blockquote_offsets) != 0) {
1360
+
1361
  $filtered_paragraph_positions = array ();
1362
+ $inside_blockquote = array ();
1363
+
1364
  foreach ($blockquote_offsets as $blockquote_offset) {
1365
  foreach ($paragraph_positions as $paragraph_position) {
1366
+ if ($paragraph_position >= $blockquote_offset [0] && $paragraph_position <= $blockquote_offset [1]) $inside_blockquote [] = $paragraph_position;
 
1367
  }
1368
  }
1369
+
1370
+ foreach ($paragraph_positions as $paragraph_position) {
1371
+ if (!in_array ($paragraph_position, $inside_blockquote)) $filtered_paragraph_positions []= $paragraph_position;
1372
+ }
1373
+
1374
  $paragraph_positions = $filtered_paragraph_positions;
1375
  }
 
1376
 
1377
+ $ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_BLOCKQUOTE;
1378
+ if (count ($paragraph_positions) == 0) return $content;
1379
+ }
1380
 
1381
  $paragraph_min_words = intval ($this->get_minimum_paragraph_words());
1382
  $paragraph_max_words = intval ($this->get_maximum_paragraph_words());
1395
  $ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_MIN_MAX_WORDS;
1396
  if (count ($paragraph_positions) == 0) return $content;
1397
 
 
1398
  $paragraph_texts = explode (",", html_entity_decode ($this->get_paragraph_text()));
1399
  if ($this->get_paragraph_text() != "" && count ($paragraph_texts) != 0) {
1400
 
1408
  if ($paragraph_text_type == AD_CONTAIN) {
1409
  $found = true;
1410
  foreach ($paragraph_texts as $paragraph_text) {
1411
+ if (trim ($paragraph_text) == '') continue;
1412
  // if (stripos ($paragraph_code, trim ($paragraph_text)) === false) {
1413
  if (mb_stripos ($paragraph_code, trim ($paragraph_text)) === false) {
1414
  $found = false;
1419
  } elseif ($paragraph_text_type == AD_DO_NOT_CONTAIN) {
1420
  $found = false;
1421
  foreach ($paragraph_texts as $paragraph_text) {
1422
+ if (trim ($paragraph_text) == '') continue;
1423
  // if (stripos ($paragraph_code, trim ($paragraph_text)) !== false) {
1424
  if (mb_stripos ($paragraph_code, trim ($paragraph_text)) !== false) {
1425
  $found = true;
1474
  // $paragraph_code = substr ($content, $paragraph_positions [$paragraph_position_above], $paragraph_positions [$position] - $paragraph_positions [$paragraph_position_above]);
1475
  $paragraph_code = mb_substr ($content, $paragraph_positions [$paragraph_position_above], $paragraph_positions [$position] - $paragraph_positions [$paragraph_position_above]);
1476
  foreach ($avoid_paragraph_texts_above as $paragraph_text_above) {
1477
+ if (trim ($paragraph_text_above) == '') continue;
1478
  // if (stripos ($paragraph_code, trim ($paragraph_text_above)) !== false) {
1479
  if (mb_stripos ($paragraph_code, trim ($paragraph_text_above)) !== false) {
1480
  $found_above = true;
1493
  // $paragraph_code = substr ($content, $paragraph_positions [$position], $content_position_below - $paragraph_positions [$position]);
1494
  $paragraph_code = mb_substr ($content, $paragraph_positions [$position], $content_position_below - $paragraph_positions [$position]);
1495
  foreach ($avoid_paragraph_texts_below as $paragraph_text_below) {
1496
+ if (trim ($paragraph_text_below) == '') continue;
1497
  // if (stripos ($paragraph_code, trim ($paragraph_text_below)) !== false) {
1498
  if (mb_stripos ($paragraph_code, trim ($paragraph_text_below)) !== false) {
1499
  $found_below = true;
1598
  return $content;
1599
  }
1600
 
1601
+ public function after_paragraph_mb ($content, $position_preview = false) {
1602
  global $ai_wp_data, $ai_last_check;
1603
 
1604
  $paragraph_positions = array ();
1652
  }
1653
 
1654
  if (count ($blockquote_offsets) != 0) {
1655
+
1656
  $filtered_paragraph_positions = array ();
1657
+ $inside_blockquote = array ();
1658
+
1659
  foreach ($blockquote_offsets as $blockquote_offset) {
1660
  foreach ($paragraph_positions as $paragraph_position) {
1661
+ if ($paragraph_position >= $blockquote_offset [0] && $paragraph_position <= $blockquote_offset [1]) $inside_blockquote [] = $paragraph_position;
 
1662
  }
1663
  }
1664
+
1665
+ foreach ($paragraph_positions as $paragraph_position) {
1666
+ if (!in_array ($paragraph_position, $inside_blockquote)) $filtered_paragraph_positions []= $paragraph_position;
1667
+ }
1668
+
1669
  $paragraph_positions = $filtered_paragraph_positions;
1670
  }
 
1671
 
1672
+ $ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_BLOCKQUOTE;
1673
+ if (count ($paragraph_positions) == 0) return $content;
1674
+ }
1675
 
1676
  $paragraph_min_words = intval ($this->get_minimum_paragraph_words());
1677
  $paragraph_max_words = intval ($this->get_maximum_paragraph_words());
1704
  if ($paragraph_text_type == AD_CONTAIN) {
1705
  $found = true;
1706
  foreach ($paragraph_texts as $paragraph_text) {
1707
+ if (trim ($paragraph_text) == '') continue;
1708
  // if (stripos ($paragraph_code, trim ($paragraph_text)) === false) {
1709
  if (mb_stripos ($paragraph_code, trim ($paragraph_text)) === false) {
1710
  $found = false;
1715
  } elseif ($paragraph_text_type == AD_DO_NOT_CONTAIN) {
1716
  $found = false;
1717
  foreach ($paragraph_texts as $paragraph_text) {
1718
+ if (trim ($paragraph_text) == '') continue;
1719
  // if (stripos ($paragraph_code, trim ($paragraph_text)) !== false) {
1720
  if (mb_stripos ($paragraph_code, trim ($paragraph_text)) !== false) {
1721
  $found = true;
1774
  $paragraph_code = mb_substr ($content, $content_position_above, $paragraph_positions [$position] - $content_position_above);
1775
 
1776
  foreach ($avoid_paragraph_texts_above as $paragraph_text_above) {
1777
+ if (trim ($paragraph_text_above) == '') continue;
1778
  // if (stripos ($paragraph_code, trim ($paragraph_text_above)) !== false) {
1779
  if (mb_stripos ($paragraph_code, trim ($paragraph_text_above)) !== false) {
1780
  $found_above = true;
1790
  // $paragraph_code = substr ($content, $paragraph_positions [$position] + 1, $paragraph_positions [$paragraph_position_below] - $paragraph_positions [$position]);
1791
  $paragraph_code = mb_substr ($content, $paragraph_positions [$position] + 1, $paragraph_positions [$paragraph_position_below] - $paragraph_positions [$position]);
1792
  foreach ($avoid_paragraph_texts_below as $paragraph_text_below) {
1793
+ if (trim ($paragraph_text_below) == '') continue;
1794
  // if (stripos ($paragraph_code, trim ($paragraph_text_below)) !== false) {
1795
  if (mb_stripos ($paragraph_code, trim ($paragraph_text_below)) !== false) {
1796
  $found_below = true;
1900
  return $content;
1901
  }
1902
 
1903
+
1904
+
1905
+ public function before_paragraph ($content, $position_preview = false) {
1906
+ global $ai_wp_data, $ai_last_check;
1907
+
1908
+ if (get_paragraph_counting_functions() == AI_MULTIBYTE_PARAGRAPH_COUNTING_FUNCTIONS) {
1909
+ return ($this->before_paragraph_mb ($content, $position_preview));
1910
+ }
1911
+
1912
+ $paragraph_positions = array ();
1913
+
1914
+ $paragraph_tags = trim ($this->get_paragraph_tags());
1915
+ if ($paragraph_tags == '') return $content;
1916
+
1917
+ $paragraph_start_strings = explode (",", $paragraph_tags);
1918
+
1919
+ $ai_last_check = AI_CHECK_PARAGRAPH_TAGS;
1920
+ if (count ($paragraph_start_strings) == 0) return $content;
1921
+
1922
+ foreach ($paragraph_start_strings as $paragraph_start_string) {
1923
+ if (trim ($paragraph_start_string) == '') continue;
1924
+
1925
+ $last_position = - 1;
1926
+
1927
+ $paragraph_start_string = trim ($paragraph_start_string);
1928
+ if ($paragraph_start_string == "#") {
1929
+ $paragraph_start = "\r\n\r\n";
1930
+ if (!in_array (0, $paragraph_positions)) $paragraph_positions [] = 0;
1931
+ } else $paragraph_start = '<' . $paragraph_start_string;
1932
+
1933
+ $paragraph_start_len = strlen ($paragraph_start);
1934
+
1935
+ while (stripos ($content, $paragraph_start, $last_position + 1) !== false) {
1936
+ $last_position = stripos ($content, $paragraph_start, $last_position + 1);
1937
+ if ($paragraph_start_string == "#") $paragraph_positions [] = $last_position + 4; else
1938
+ if ($content [$last_position + $paragraph_start_len] == ">" || $content [$last_position + $paragraph_start_len] == " ")
1939
+ $paragraph_positions [] = $last_position;
1940
+ }
1941
+ }
1942
+
1943
+ // Nothing to do
1944
+ $ai_last_check = AI_CHECK_PARAGRAPHS_WITH_TAGS;
1945
+ if (count ($paragraph_positions) == 0) return $content;
1946
+
1947
+ sort ($paragraph_positions);
1948
+
1949
+ if (!$this->get_count_inside_blockquote ()) {
1950
+ preg_match_all ('/<\/?blockquote/i', $content, $blockquotes, PREG_OFFSET_CAPTURE);
1951
+
1952
+ $blockquote_offsets = array ();
1953
+ $blockquotes = $blockquotes [0];
1954
+ foreach ($blockquotes as $index => $blockquote) {
1955
+ if (strtolower ($blockquote [0]) == '<blockquote' && isset ($blockquotes [$index + 1][0]) && strtolower ($blockquotes [$index + 1][0]) == '</blockquote') {
1956
+ $blockquote_offsets []= array ($blockquote [1] + 11, $blockquotes [$index + 1][1]);
1957
+ }
1958
+ }
1959
+
1960
+ if (count ($blockquote_offsets) != 0) {
1961
+
1962
+ $filtered_paragraph_positions = array ();
1963
+ $inside_blockquote = array ();
1964
+
1965
+ foreach ($blockquote_offsets as $blockquote_offset) {
1966
+ foreach ($paragraph_positions as $paragraph_position) {
1967
+ if ($paragraph_position >= $blockquote_offset [0] && $paragraph_position <= $blockquote_offset [1]) $inside_blockquote [] = $paragraph_position;
1968
+ }
1969
+ }
1970
+
1971
+ foreach ($paragraph_positions as $paragraph_position) {
1972
+ if (!in_array ($paragraph_position, $inside_blockquote)) $filtered_paragraph_positions []= $paragraph_position;
1973
+ }
1974
+
1975
+ $paragraph_positions = $filtered_paragraph_positions;
1976
+ }
1977
+ }
1978
+
1979
+ $ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_BLOCKQUOTE;
1980
+ if (count ($paragraph_positions) == 0) return $content;
1981
+
1982
+ $paragraph_min_words = intval ($this->get_minimum_paragraph_words());
1983
+ $paragraph_max_words = intval ($this->get_maximum_paragraph_words());
1984
+
1985
+ if ($paragraph_min_words != 0 || $paragraph_max_words != 0) {
1986
+ $filtered_paragraph_positions = array ();
1987
+ foreach ($paragraph_positions as $index => $paragraph_position) {
1988
+ $paragraph_code = $index == count ($paragraph_positions) - 1 ? substr ($content, $paragraph_position) : substr ($content, $paragraph_position, $paragraph_positions [$index + 1] - $paragraph_position);
1989
+ if ($this->check_number_of_words_in_paragraph ($paragraph_code, $paragraph_min_words, $paragraph_max_words)) $filtered_paragraph_positions [] = $paragraph_position;
1990
+ }
1991
+ $paragraph_positions = $filtered_paragraph_positions;
1992
+ }
1993
+
1994
+ // Nothing to do
1995
+ $ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_MIN_MAX_WORDS;
1996
+ if (count ($paragraph_positions) == 0) return $content;
1997
+
1998
+
1999
+ $paragraph_texts = explode (",", html_entity_decode ($this->get_paragraph_text()));
2000
+ if ($this->get_paragraph_text() != "" && count ($paragraph_texts) != 0) {
2001
+
2002
+ $filtered_paragraph_positions = array ();
2003
+ $paragraph_text_type = $this->get_paragraph_text_type ();
2004
+
2005
+ foreach ($paragraph_positions as $index => $paragraph_position) {
2006
+ $paragraph_code = $index == count ($paragraph_positions) - 1 ? substr ($content, $paragraph_position) : substr ($content, $paragraph_position, $paragraph_positions [$index + 1] - $paragraph_position);
2007
+
2008
+ if ($paragraph_text_type == AD_CONTAIN) {
2009
+ $found = true;
2010
+ foreach ($paragraph_texts as $paragraph_text) {
2011
+ if (trim ($paragraph_text) == '') continue;
2012
+ if (stripos ($paragraph_code, trim ($paragraph_text)) === false) {
2013
+ $found = false;
2014
+ break;
2015
+ }
2016
+ }
2017
+ if ($found) $filtered_paragraph_positions [] = $paragraph_position;
2018
+ } elseif ($paragraph_text_type == AD_DO_NOT_CONTAIN) {
2019
+ $found = false;
2020
+ foreach ($paragraph_texts as $paragraph_text) {
2021
+ if (trim ($paragraph_text) == '') continue;
2022
+ if (stripos ($paragraph_code, trim ($paragraph_text)) !== false) {
2023
+ $found = true;
2024
+ break;
2025
+ }
2026
+ }
2027
+ if (!$found) $filtered_paragraph_positions [] = $paragraph_position;
2028
+ }
2029
+ }
2030
+
2031
+ $paragraph_positions = $filtered_paragraph_positions;
2032
+ }
2033
+
2034
+ // Nothing to do
2035
+ $ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_TEXT;
2036
+ if (count ($paragraph_positions) == 0) return $content;
2037
+
2038
+
2039
+ $position = $this->get_paragraph_number();
2040
+
2041
+ if ($position > 0 && $position < 1) {
2042
+ $position = intval ($position * (count ($paragraph_positions) - 1) + 0.5);
2043
+ }
2044
+ elseif ($position <= 0) {
2045
+ $position = rand (0, count ($paragraph_positions) - 1);
2046
+ } else $position --;
2047
+
2048
+ if ($this->get_direction_type() == AD_DIRECTION_FROM_BOTTOM) {
2049
+ $paragraph_positions = array_reverse ($paragraph_positions);
2050
+ }
2051
+
2052
+
2053
+ $avoid_paragraphs_above = intval ($this->get_avoid_paragraphs_above());
2054
+ $avoid_paragraphs_below = intval ($this->get_avoid_paragraphs_below());
2055
+
2056
+ if (($avoid_paragraphs_above != 0 || $avoid_paragraphs_below != 0) && count ($paragraph_positions) > $position) {
2057
+ $avoid_text_above = $this->get_avoid_text_above();
2058
+ $avoid_text_below = $this->get_avoid_text_below();
2059
+ $avoid_paragraph_texts_above = explode (",", html_entity_decode (trim ($avoid_text_above)));
2060
+ $avoid_paragraph_texts_below = explode (",", html_entity_decode (trim ($avoid_text_below)));
2061
+
2062
+ $direction = $this->get_avoid_direction();
2063
+ $max_checks = $this->get_avoid_try_limit();
2064
+
2065
+ $checks = $max_checks;
2066
+ $saved_position = $position;
2067
+ do {
2068
+ $found_above = false;
2069
+ if ($position != 0 && $avoid_paragraphs_above != 0 && $avoid_text_above != "" && is_array ($avoid_paragraph_texts_above) && count ($avoid_paragraph_texts_above) != 0) {
2070
+ $paragraph_position_above = $position - $avoid_paragraphs_above;
2071
+ if ($paragraph_position_above < 0) $paragraph_position_above = 0;
2072
+ $paragraph_code = substr ($content, $paragraph_positions [$paragraph_position_above], $paragraph_positions [$position] - $paragraph_positions [$paragraph_position_above]);
2073
+ foreach ($avoid_paragraph_texts_above as $paragraph_text_above) {
2074
+ if (trim ($paragraph_text_above) == '') continue;
2075
+ if (stripos ($paragraph_code, trim ($paragraph_text_above)) !== false) {
2076
+ $found_above = true;
2077
+ break;
2078
+ }
2079
+ }
2080
+ }
2081
+
2082
+ $found_below = false;
2083
+ if ($avoid_paragraphs_below != 0 && $avoid_text_below != "" && is_array ($avoid_paragraph_texts_below) && count ($avoid_paragraph_texts_below) != 0) {
2084
+ $paragraph_position_below = $position + $avoid_paragraphs_below;
2085
+ if ($paragraph_position_below > count ($paragraph_positions) - 1)
2086
+ $content_position_below = strlen ($content); else
2087
+ $content_position_below = $paragraph_positions [$paragraph_position_below];
2088
+ $paragraph_code = substr ($content, $paragraph_positions [$position], $content_position_below - $paragraph_positions [$position]);
2089
+ foreach ($avoid_paragraph_texts_below as $paragraph_text_below) {
2090
+ if (trim ($paragraph_text_below) == '') continue;
2091
+ if (stripos ($paragraph_code, trim ($paragraph_text_below)) !== false) {
2092
+ $found_below = true;
2093
+ break;
2094
+ }
2095
+ }
2096
+ }
2097
+
2098
+
2099
+ // echo "position: $position = before #", $position + 1, "<br />\n";
2100
+ // echo "checks: $checks<br />\n";
2101
+ // echo "direction: $direction<br />\n";
2102
+ // if ($found_above)
2103
+ // echo "found_above<br />\n";
2104
+ // if ($found_below)
2105
+ // echo "found_below<br />\n";
2106
+ // echo "=================<br />\n";
2107
+
2108
+
2109
+ if ($found_above || $found_below) {
2110
+ $ai_last_check = AI_CHECK_DO_NOT_INSERT;
2111
+ if ($this->get_avoid_action() == AD_DO_NOT_INSERT) return $content;
2112
+
2113
+ switch ($direction) {
2114
+ case AD_ABOVE: // Try above
2115
+ $ai_last_check = AI_CHECK_AD_ABOVE;
2116
+ if ($position == 0) return $content; // Already at the top - do not insert
2117
+ $position --;
2118
+ break;
2119
+ case AD_BELOW: // Try below
2120
+ $ai_last_check = AI_CHECK_AD_BELOW;
2121
+ if ($position >= count ($paragraph_positions) - 1) return $content; // Already at the bottom - do not insert
2122
+ $position ++;
2123
+ break;
2124
+ case AD_ABOVE_AND_THEN_BELOW: // Try first above and then below
2125
+ if ($position == 0 || $checks == 0) {
2126
+ // Try below
2127
+ $direction = AD_BELOW;
2128
+ $checks = $max_checks;
2129
+ $position = $saved_position;
2130
+ $ai_last_check = AI_CHECK_AD_BELOW;
2131
+ if ($position >= count ($paragraph_positions) - 1) return $content; // Already at the bottom - do not insert
2132
+ $position ++;
2133
+ } else $position --;
2134
+ break;
2135
+ case AD_BELOW_AND_THEN_ABOVE: // Try first below and then above
2136
+ if ($position >= count ($paragraph_positions) - 1 || $checks == 0) {
2137
+ // Try above
2138
+ $direction = AD_ABOVE;
2139
+ $checks = $max_checks;
2140
+ $position = $saved_position;
2141
+ $ai_last_check = AI_CHECK_AD_ABOVE;
2142
+ if ($position == 0) return $content; // Already at the top - do not insert
2143
+ $position --;
2144
+ } else $position ++;
2145
+ break;
2146
+ }
2147
+ } else break; // Text not found - insert
2148
+
2149
+ // Try next position
2150
+ if ($checks <= 0) return $content; // Suitable position not found - do not insert
2151
+ $checks --;
2152
+ } while (true);
2153
+ }
2154
+
2155
+ // Nothing to do
2156
+ $ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_CLEARANCE;
2157
+ if (count ($paragraph_positions) == 0) return $content;
2158
+
2159
+ if ($position_preview) {
2160
+ $offset = 0;
2161
+ foreach ($paragraph_positions as $counter => $paragraph_position) {
2162
+ $inserted_code = "[[AI_BP".($counter + 1)."]]";
2163
+ if ($this->get_direction_type() == AD_DIRECTION_FROM_BOTTOM) {
2164
+ $content = substr_replace ($content, $inserted_code, $paragraph_position, 0);
2165
+ } else {
2166
+ $content = substr_replace ($content, $inserted_code, $paragraph_position + $offset, 0);
2167
+ $offset += strlen ($inserted_code);
2168
+ }
2169
+ }
2170
+ return $content;
2171
+ }
2172
+
2173
+ $ai_last_check = AI_CHECK_PARAGRAPHS_MIN_NUMBER;
2174
+ if (count ($paragraph_positions) >= intval ($this->get_paragraph_number_minimum())) {
2175
+ $ai_last_check = AI_CHECK_PARAGRAPH_NUMBER;
2176
+ if (count ($paragraph_positions) > $position) {
2177
+ $this->increment_block_counter ();
2178
+ $ai_last_check = AI_CHECK_DEBUG_NO_INSERTION;
2179
+ if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_NO_INSERTION) == 0) {
2180
+ $content_position = $paragraph_positions [$position];
2181
+ $content = substr_replace ($content, $this->get_code_for_insertion (), $content_position, 0);
2182
+ $ai_last_check = AI_CHECK_INSERTED;
2183
+ }
2184
+ }
2185
+ }
2186
+
2187
+ return $content;
2188
+ }
2189
+
2190
+ public function after_paragraph ($content, $position_preview = false) {
2191
+ global $ai_wp_data, $ai_last_check;
2192
+
2193
+ if (get_paragraph_counting_functions() == AI_MULTIBYTE_PARAGRAPH_COUNTING_FUNCTIONS) {
2194
+ return ($this->after_paragraph_mb ($content, $position_preview));
2195
+ }
2196
+
2197
+ $paragraph_positions = array ();
2198
+ $last_content_position = strlen ($content) - 1;
2199
+
2200
+ $paragraph_tags = trim ($this->get_paragraph_tags());
2201
+ if ($paragraph_tags == '') return $content;
2202
+
2203
+ $paragraph_end_strings = explode (",", $paragraph_tags);
2204
+
2205
+ $ai_last_check = AI_CHECK_PARAGRAPH_TAGS;
2206
+ if (count ($paragraph_end_strings) == 0) return $content;
2207
+
2208
+ foreach ($paragraph_end_strings as $paragraph_end_string) {
2209
+ if (trim ($paragraph_end_string) == '') continue;
2210
+
2211
+ $last_position = - 1;
2212
+
2213
+ $paragraph_end_string = trim ($paragraph_end_string);
2214
+ if ($paragraph_end_string == "#") {
2215
+ $paragraph_end = "\r\n\r\n";
2216
+ if (!in_array ($last_content_position, $paragraph_positions)) $paragraph_positions [] = $last_content_position;
2217
+ } else $paragraph_end = '</' . $paragraph_end_string . '>';
2218
+
2219
+ while (stripos ($content, $paragraph_end, $last_position + 1) !== false) {
2220
+ $last_position = stripos ($content, $paragraph_end, $last_position + 1) + strlen ($paragraph_end) - 1;
2221
+ if ($paragraph_end_string == "#") $paragraph_positions [] = $last_position - 4; else
2222
+ $paragraph_positions [] = $last_position;
2223
+ }
2224
+ }
2225
+
2226
+ // Nothing to do
2227
+ $ai_last_check = AI_CHECK_PARAGRAPHS_WITH_TAGS;
2228
+ if (count ($paragraph_positions) == 0) return $content;
2229
+
2230
+ sort ($paragraph_positions);
2231
+
2232
+ if (!$this->get_count_inside_blockquote ()) {
2233
+ preg_match_all ('/<\/?blockquote/i', $content, $blockquotes, PREG_OFFSET_CAPTURE);
2234
+
2235
+ $blockquote_offsets = array ();
2236
+ $blockquotes = $blockquotes [0];
2237
+ foreach ($blockquotes as $index => $blockquote) {
2238
+ if (strtolower ($blockquote [0]) == '<blockquote' && isset ($blockquotes [$index + 1][0]) && strtolower ($blockquotes [$index + 1][0]) == '</blockquote') {
2239
+ $blockquote_offsets []= array ($blockquote [1] + 11, $blockquotes [$index + 1][1]);
2240
+ }
2241
+ }
2242
+
2243
+ if (count ($blockquote_offsets) != 0) {
2244
+
2245
+ $filtered_paragraph_positions = array ();
2246
+ $inside_blockquote = array ();
2247
+
2248
+ foreach ($blockquote_offsets as $blockquote_offset) {
2249
+ foreach ($paragraph_positions as $paragraph_position) {
2250
+ if ($paragraph_position >= $blockquote_offset [0] && $paragraph_position <= $blockquote_offset [1]) $inside_blockquote [] = $paragraph_position;
2251
+ }
2252
+ }
2253
+
2254
+ foreach ($paragraph_positions as $paragraph_position) {
2255
+ if (!in_array ($paragraph_position, $inside_blockquote)) $filtered_paragraph_positions []= $paragraph_position;
2256
+ }
2257
+
2258
+ $paragraph_positions = $filtered_paragraph_positions;
2259
+ }
2260
+ }
2261
+
2262
+ $ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_BLOCKQUOTE;
2263
+ if (count ($paragraph_positions) == 0) return $content;
2264
+
2265
+ $paragraph_min_words = intval ($this->get_minimum_paragraph_words());
2266
+ $paragraph_max_words = intval ($this->get_maximum_paragraph_words());
2267
+
2268
+ if ($paragraph_min_words != 0 || $paragraph_max_words != 0) {
2269
+ $filtered_paragraph_positions = array ();
2270
+ foreach ($paragraph_positions as $index => $paragraph_position) {
2271
+ $paragraph_code = $index == 0 ? substr ($content, 0, $paragraph_position + 1) : substr ($content, $paragraph_positions [$index - 1] + 1, $paragraph_position - $paragraph_positions [$index - 1]);
2272
+ if ($this->check_number_of_words_in_paragraph ($paragraph_code, $paragraph_min_words, $paragraph_max_words)) $filtered_paragraph_positions [] = $paragraph_position;
2273
+ }
2274
+ $paragraph_positions = $filtered_paragraph_positions;
2275
+ }
2276
+
2277
+ // Nothing to do
2278
+ $ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_MIN_MAX_WORDS;
2279
+ if (count ($paragraph_positions) == 0) return $content;
2280
+
2281
+
2282
+ $paragraph_texts = explode (",", html_entity_decode ($this->get_paragraph_text()));
2283
+ if ($this->get_paragraph_text() != "" && count ($paragraph_texts) != 0) {
2284
+
2285
+ $filtered_paragraph_positions = array ();
2286
+ $paragraph_text_type = $this->get_paragraph_text_type ();
2287
+
2288
+ foreach ($paragraph_positions as $index => $paragraph_position) {
2289
+ $paragraph_code = $index == 0 ? substr ($content, 0, $paragraph_position + 1) : substr ($content, $paragraph_positions [$index - 1] + 1, $paragraph_position - $paragraph_positions [$index - 1]);
2290
+
2291
+ if ($paragraph_text_type == AD_CONTAIN) {
2292
+ $found = true;
2293
+ foreach ($paragraph_texts as $paragraph_text) {
2294
+ if (trim ($paragraph_text) == '') continue;
2295
+ if (stripos ($paragraph_code, trim ($paragraph_text)) === false) {
2296
+ $found = false;
2297
+ break;
2298
+ }
2299
+ }
2300
+ if ($found) $filtered_paragraph_positions [] = $paragraph_position;
2301
+ } elseif ($paragraph_text_type == AD_DO_NOT_CONTAIN) {
2302
+ $found = false;
2303
+ foreach ($paragraph_texts as $paragraph_text) {
2304
+ if (trim ($paragraph_text) == '') continue;
2305
+ if (stripos ($paragraph_code, trim ($paragraph_text)) !== false) {
2306
+ $found = true;
2307
+ break;
2308
+ }
2309
+ }
2310
+ if (!$found) $filtered_paragraph_positions [] = $paragraph_position;
2311
+ }
2312
+ }
2313
+
2314
+ $paragraph_positions = $filtered_paragraph_positions;
2315
+ }
2316
+
2317
+ // Nothing to do
2318
+ $ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_TEXT;
2319
+ if (count ($paragraph_positions) == 0) return $content;
2320
+
2321
+
2322
+ $position = $this->get_paragraph_number();
2323
+
2324
+ if ($position > 0 && $position < 1) {
2325
+ $position = intval ($position * (count ($paragraph_positions) - 1) + 0.5);
2326
+ }
2327
+ elseif ($position <= 0) {
2328
+ $position = rand (0, count ($paragraph_positions) - 1);
2329
+ } else $position --;
2330
+
2331
+ if ($this->get_direction_type() == AD_DIRECTION_FROM_BOTTOM) {
2332
+ $paragraph_positions = array_reverse ($paragraph_positions);
2333
+ }
2334
+
2335
+
2336
+ $avoid_paragraphs_above = intval ($this->get_avoid_paragraphs_above());
2337
+ $avoid_paragraphs_below = intval ($this->get_avoid_paragraphs_below());
2338
+
2339
+ if (($avoid_paragraphs_above != 0 || $avoid_paragraphs_below != 0) && count ($paragraph_positions) > $position) {
2340
+ $avoid_text_above = $this->get_avoid_text_above();
2341
+ $avoid_text_below = $this->get_avoid_text_below();
2342
+ $avoid_paragraph_texts_above = explode (",", html_entity_decode (trim ($avoid_text_above)));
2343
+ $avoid_paragraph_texts_below = explode (",", html_entity_decode (trim ($avoid_text_below)));
2344
+
2345
+ $direction = $this->get_avoid_direction();
2346
+ $max_checks = $this->get_avoid_try_limit();
2347
+
2348
+ $checks = $max_checks;
2349
+ $saved_position = $position;
2350
+ do {
2351
+ $found_above = false;
2352
+ if ($avoid_paragraphs_above != 0 && $avoid_text_above != "" && is_array ($avoid_paragraph_texts_above) && count ($avoid_paragraph_texts_above) != 0) {
2353
+ $paragraph_position_above = $position - $avoid_paragraphs_above;
2354
+ if ($paragraph_position_above <= 0)
2355
+ $content_position_above = 0; else
2356
+ $content_position_above = $paragraph_positions [$paragraph_position_above] + 1;
2357
+
2358
+ $paragraph_code = substr ($content, $content_position_above, $paragraph_positions [$position] - $content_position_above);
2359
+
2360
+ foreach ($avoid_paragraph_texts_above as $paragraph_text_above) {
2361
+ if (trim ($paragraph_text_above) == '') continue;
2362
+ if (stripos ($paragraph_code, trim ($paragraph_text_above)) !== false) {
2363
+ $found_above = true;
2364
+ break;
2365
+ }
2366
+ }
2367
+ }
2368
+
2369
+ $found_below = false;
2370
+ if ($avoid_paragraphs_below != 0 && $position != count ($paragraph_positions) - 1 && $avoid_text_below != "" && is_array ($avoid_paragraph_texts_below) && count ($avoid_paragraph_texts_below) != 0) {
2371
+ $paragraph_position_below = $position + $avoid_paragraphs_below;
2372
+ if ($paragraph_position_below > count ($paragraph_positions) - 1) $paragraph_position_below = count ($paragraph_positions) - 1;
2373
+ $paragraph_code = substr ($content, $paragraph_positions [$position] + 1, $paragraph_positions [$paragraph_position_below] - $paragraph_positions [$position]);
2374
+ foreach ($avoid_paragraph_texts_below as $paragraph_text_below) {
2375
+ if (trim ($paragraph_text_below) == '') continue;
2376
+ if (stripos ($paragraph_code, trim ($paragraph_text_below)) !== false) {
2377
+ $found_below = true;
2378
+ break;
2379
+ }
2380
+ }
2381
+ }
2382
+
2383
+
2384
+ // echo "position: $position = after #", $position + 1, "<br />\n";
2385
+ // echo "checks: $checks<br />\n";
2386
+ // echo "direction: $direction<br />\n";
2387
+ // if ($found_above)
2388
+ // echo "found_above<br />\n";
2389
+ // if ($found_below)
2390
+ // echo "found_below<br />\n";
2391
+ // echo "=================<br />\n";
2392
+
2393
+
2394
+ if ($found_above || $found_below) {
2395
+ $ai_last_check = AI_CHECK_DO_NOT_INSERT;
2396
+ if ($this->get_avoid_action() == AD_DO_NOT_INSERT) return $content;
2397
+
2398
+ switch ($direction) {
2399
+ case AD_ABOVE: // Try above
2400
+ $ai_last_check = AI_CHECK_AD_ABOVE;
2401
+ if ($position == 0) return $content; // Already at the top - do not insert
2402
+ $position --;
2403
+ break;
2404
+ case AD_BELOW: // Try below
2405
+ $ai_last_check = AI_CHECK_AD_BELOW;
2406
+ if ($position >= count ($paragraph_positions) - 1) return $content; // Already at the bottom - do not insert
2407
+ $position ++;
2408
+ break;
2409
+ case AD_ABOVE_AND_THEN_BELOW: // Try first above and then below
2410
+ if ($position == 0 || $checks == 0) {
2411
+ // Try below
2412
+ $direction = AD_BELOW;
2413
+ $checks = $max_checks;
2414
+ $position = $saved_position;
2415
+ $ai_last_check = AI_CHECK_AD_BELOW;
2416
+ if ($position >= count ($paragraph_positions) - 1) return $content; // Already at the bottom - do not insert
2417
+ $position ++;
2418
+ } else $position --;
2419
+ break;
2420
+ case AD_BELOW_AND_THEN_ABOVE: // Try first below and then above
2421
+ if ($position >= count ($paragraph_positions) - 1 || $checks == 0) {
2422
+ // Try above
2423
+ $direction = AD_ABOVE;
2424
+ $checks = $max_checks;
2425
+ $position = $saved_position;
2426
+ $ai_last_check = AI_CHECK_AD_ABOVE;
2427
+ if ($position == 0) return $content; // Already at the top - do not insert
2428
+ $position --;
2429
+ } else $position ++;
2430
+ break;
2431
+ }
2432
+ } else break; // Text not found - insert
2433
+
2434
+ // Try next position
2435
+ if ($checks <= 0) return $content; // Suitable position not found - do not insert
2436
+ $checks --;
2437
+ } while (true);
2438
+ }
2439
+
2440
+ // Nothing to do
2441
+ $ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_CLEARANCE;
2442
+ if (count ($paragraph_positions) == 0) return $content;
2443
+
2444
+
2445
+ if ($position_preview) {
2446
+ $offset = 0;
2447
+ foreach ($paragraph_positions as $counter => $paragraph_position) {
2448
+ $inserted_code = "[[AI_AP".($counter + 1)."]]";
2449
+ if ($this->get_direction_type() == AD_DIRECTION_FROM_BOTTOM) {
2450
+ $content = substr_replace ($content, $inserted_code, $paragraph_position + 1, 0);
2451
+ } else {
2452
+ $content = substr_replace ($content, $inserted_code, $paragraph_position + $offset + 1, 0);
2453
+ $offset += strlen ($inserted_code);
2454
+ }
2455
+ }
2456
+ return $content;
2457
+ }
2458
+
2459
+ $ai_last_check = AI_CHECK_PARAGRAPHS_MIN_NUMBER;
2460
+ if (count ($paragraph_positions) >= intval ($this->get_paragraph_number_minimum())) {
2461
+ $ai_last_check = AI_CHECK_PARAGRAPH_NUMBER;
2462
+ if (count ($paragraph_positions) > $position) {
2463
+ $this->increment_block_counter ();
2464
+ $ai_last_check = AI_CHECK_DEBUG_NO_INSERTION;
2465
+ if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_NO_INSERTION) == 0) {
2466
+ $content_position = $paragraph_positions [$position];
2467
+ if ($content_position >= strlen ($content) - 1)
2468
+ $content = $content . $this->get_code_for_insertion (); else
2469
+ $content = substr_replace ($content, $this->get_code_for_insertion (), $content_position + 1, 0);
2470
+ $ai_last_check = AI_CHECK_INSERTED;
2471
+ }
2472
+ }
2473
+ }
2474
+
2475
+ return $content;
2476
+ }
2477
+
2478
+
2479
+
2480
  // Deprecated
2481
  function manual ($content){
2482
 
constants.php CHANGED
@@ -14,7 +14,7 @@ if (!defined( 'AD_INSERTER_NAME'))
14
  define ('AD_INSERTER_NAME', 'Ad Inserter');
15
 
16
  if (!defined( 'AD_INSERTER_VERSION'))
17
- define ('AD_INSERTER_VERSION', '2.1.3');
18
 
19
  if (!defined ('AD_INSERTER_PLUGIN_BASENAME'))
20
  define ('AD_INSERTER_PLUGIN_BASENAME', plugin_basename (__FILE__));
@@ -296,6 +296,13 @@ define('AI_TEXT_SERVER_SIDE', 'Server-side');
296
  define('AI_TEXT_CLIENT_SIDE', 'Client-side');
297
  define('AI_TEXT_SERVER_SIDE_W3TC', 'Server-side using W3 Total Cache');
298
 
 
 
 
 
 
 
 
299
  //Settings
300
  define('AD_SETTINGS_CHECKED', '1');
301
  define('AD_SETTINGS_NOT_CHECKED', '0');
@@ -307,6 +314,7 @@ define ('DEFAULT_BLOCK_CLASS_NAME', 'code-block');
307
  define ('DEFAULT_MINIMUM_USER_ROLE', 'administrator');
308
  define ('DEFAULT_PLUGIN_PRIORITY', 99999);
309
  define ('DEFAULT_DYNAMIC_BLOCKS', AI_DYNAMIC_BLOCKS_SERVER_SIDE);
 
310
  define ('DEFAULT_PARAGRAPH_TAGS', 'p');
311
  define ('DEFAULT_ADMIN_TOOLBAR_DEBUGGING', AD_SETTINGS_CHECKED);
312
  define ('DEFAULT_REMOTE_DEBUGGING', AD_SETTINGS_NOT_CHECKED);
14
  define ('AD_INSERTER_NAME', 'Ad Inserter');
15
 
16
  if (!defined( 'AD_INSERTER_VERSION'))
17
+ define ('AD_INSERTER_VERSION', '2.1.4');
18
 
19
  if (!defined ('AD_INSERTER_PLUGIN_BASENAME'))
20
  define ('AD_INSERTER_PLUGIN_BASENAME', plugin_basename (__FILE__));
296
  define('AI_TEXT_CLIENT_SIDE', 'Client-side');
297
  define('AI_TEXT_SERVER_SIDE_W3TC', 'Server-side using W3 Total Cache');
298
 
299
+ // Paragraph counting functions
300
+ define('AI_STANDARD_PARAGRAPH_COUNTING_FUNCTIONS', 0);
301
+ define('AI_MULTIBYTE_PARAGRAPH_COUNTING_FUNCTIONS', 1);
302
+
303
+ define('AI_TEXT_STANDARD', 'Standard');
304
+ define('AI_TEXT_MULTIBYTE', 'Multibyte');
305
+
306
  //Settings
307
  define('AD_SETTINGS_CHECKED', '1');
308
  define('AD_SETTINGS_NOT_CHECKED', '0');
314
  define ('DEFAULT_MINIMUM_USER_ROLE', 'administrator');
315
  define ('DEFAULT_PLUGIN_PRIORITY', 99999);
316
  define ('DEFAULT_DYNAMIC_BLOCKS', AI_DYNAMIC_BLOCKS_SERVER_SIDE);
317
+ define ('DEFAULT_PARAGRAPH_COUNTING_FUNCTIONS', AI_STANDARD_PARAGRAPH_COUNTING_FUNCTIONS);
318
  define ('DEFAULT_PARAGRAPH_TAGS', 'p');
319
  define ('DEFAULT_ADMIN_TOOLBAR_DEBUGGING', AD_SETTINGS_CHECKED);
320
  define ('DEFAULT_REMOTE_DEBUGGING', AD_SETTINGS_NOT_CHECKED);
css/ad-inserter.css CHANGED
@@ -1,5 +1,5 @@
1
  #data {
2
- font-family: "2.1.3"; /* Used for version number of the file */
3
  }
4
 
5
  #blocked-warning {
1
  #data {
2
+ font-family: "2.1.4"; /* Used for version number of the file */
3
  }
4
 
5
  #blocked-warning {
js/ad-inserter.js CHANGED
@@ -1,4 +1,4 @@
1
- var javascript_version = "2.1.3";
2
  var ignore_key = true;
3
  var start = 1;
4
  var end = 16;
1
+ var javascript_version = "2.1.4";
2
  var ignore_key = true;
3
  var start = 1;
4
  var end = 16;
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_i
4
  Tags: adsense, amazon, advertising, ad, advert, html, javascript, php, code, widget, sidebar, responsive, viewport, rotating, banner, ad rotation, geo targeting, multisite, contextual, shortcodes, widgets, header, footer, mobile, desktop, phone, tablet, custom css, category, tag, filter, url, caching support
5
  Requires at least: 4.0
6
  Tested up to: 4.7.3
7
- Stable tag: 2.1.2
8
  License: GPLv3
9
 
10
  Insert any advert or code into Wordpress. Perfect for all kinds of banners and ads including AdSense and Amazon. Powerful features, simple use.
@@ -676,6 +676,13 @@ AD CODE RIGHT
676
 
677
  == Changelog ==
678
 
 
 
 
 
 
 
 
679
  = 2.1.3 =
680
  - Added support for counting paragraphs with multibyte (unicode) characters
681
  - Fixed bug for class name not saved
@@ -1013,6 +1020,11 @@ AD CODE RIGHT
1013
 
1014
  == Upgrade Notice ==
1015
 
 
 
 
 
 
1016
  = 2.1.3 =
1017
  Added support for counting paragraphs with multibyte (unicode) characters;
1018
  Fixed bug for class name not saved;
4
  Tags: adsense, amazon, advertising, ad, advert, html, javascript, php, code, widget, sidebar, responsive, viewport, rotating, banner, ad rotation, geo targeting, multisite, contextual, shortcodes, widgets, header, footer, mobile, desktop, phone, tablet, custom css, category, tag, filter, url, caching support
5
  Requires at least: 4.0
6
  Tested up to: 4.7.3
7
+ Stable tag: 2.1.3
8
  License: GPLv3
9
 
10
  Insert any advert or code into Wordpress. Perfect for all kinds of banners and ads including AdSense and Amazon. Powerful features, simple use.
676
 
677
  == Changelog ==
678
 
679
+ = 2.1.4 =
680
+ - Paragraph counting restored to standard functions
681
+ - Added option to select paragraph counting functions with multibyte support (unicode characters)
682
+ - Fixed bug for wrong paragraph counting in posts with blockquote sections in some cases
683
+ - Fixed bug for wrong measuring of plugin processing time in some cases
684
+ - Fixed bug for "Empty delimiter" warning
685
+
686
  = 2.1.3 =
687
  - Added support for counting paragraphs with multibyte (unicode) characters
688
  - Fixed bug for class name not saved
1020
 
1021
  == Upgrade Notice ==
1022
 
1023
+ = 2.1.4 =
1024
+ Paragraph counting restored to standard functions;
1025
+ Added option to select paragraph counting functions with multibyte support (unicode characters);
1026
+ Few bugs fixed including for "Empty delimiter" warning
1027
+
1028
  = 2.1.3 =
1029
  Added support for counting paragraphs with multibyte (unicode) characters;
1030
  Fixed bug for class name not saved;
settings.php CHANGED
@@ -1103,6 +1103,17 @@ function generate_settings_form (){
1103
  </select>
1104
  </td>
1105
  </tr>
 
 
 
 
 
 
 
 
 
 
 
1106
  <tr>
1107
  <td style="padding-left: 10px;">
1108
  Plugin priority
1103
  </select>
1104
  </td>
1105
  </tr>
1106
+ <tr>
1107
+ <td style="padding-left: 10px;">
1108
+ Functions for Paragraph Counting
1109
+ </td>
1110
+ <td>
1111
+ <select style="border-radius: 5px; margin-bottom: 3px;" id="paragraph_counting_functions" name="paragraph_counting_functions" title="Standard PHP functions are faster and work in most cases, use Multibyte functions if paragraphs are not counted properly on non-english pages.">
1112
+ <option value="<?php echo AI_STANDARD_PARAGRAPH_COUNTING_FUNCTIONS; ?>" <?php echo get_paragraph_counting_functions() == AI_STANDARD_PARAGRAPH_COUNTING_FUNCTIONS ? AD_SELECT_SELECTED : AD_EMPTY_VALUE; ?>><?php echo AI_TEXT_STANDARD; ?></option>
1113
+ <option value="<?php echo AI_MULTIBYTE_PARAGRAPH_COUNTING_FUNCTIONS; ?>" <?php echo get_paragraph_counting_functions() == AI_MULTIBYTE_PARAGRAPH_COUNTING_FUNCTIONS ? AD_SELECT_SELECTED : AD_EMPTY_VALUE; ?>><?php echo AI_TEXT_MULTIBYTE; ?></option>
1114
+ </select>
1115
+ </td>
1116
+ </tr>
1117
  <tr>
1118
  <td style="padding-left: 10px;">
1119
  Plugin priority