PageRenderTime 57ms CodeModel.GetById 15ms app.highlight 30ms RepoModel.GetById 2ms app.codeStats 1ms

/shared/classes/class.debug.php

https://github.com/deltafactory/landing-pages
PHP | 417 lines | 289 code | 64 blank | 64 comment | 49 complexity | 71ac4093f5c08312ba397eaeb3295535 MD5 | raw file
  1<?php
  2/* Inbound Now Debug Class
  3*
  4*  This class enabled users to dequeue third party javascript from pages to stop JS errors
  5*/
  6
  7if (!defined('INBOUND_CLASS_URL'))
  8    define('INBOUND_CLASS_URL', plugin_dir_url(__FILE__));
  9
 10  //update_option( 'inbound_global_dequeue', "" );
 11  /*
 12  $global_array = get_option( 'inbound_global_dequeue' );
 13  print_r($global_array);
 14  /**/
 15
 16if (!class_exists('Inbound_Debug_Scripts')) {
 17  class Inbound_Debug_Scripts {
 18    static $add_debug;
 19
 20  /*  Contruct
 21   *  --------------------------------------------------------- */
 22    static function init() {
 23      self::$add_debug = true;
 24      //add_action('wp_loaded', array(__CLASS__, 'inbound_check_for_error'));
 25      //add_action('wp_footer', array(__CLASS__, 'display_errors'));
 26      //add_action('init', array(__CLASS__, 'admin_display_errors'));
 27      add_action( 'init',  array(__CLASS__, 'inbound_output_meta_debug') );
 28      add_action('wp_enqueue_scripts', array(__CLASS__, 'inbound_kill_bogus_scripts'), 100);
 29      add_action('wp_enqueue_scripts', array(__CLASS__, 'inbound_compatibilities'), 101);
 30      add_action('admin_enqueue_scripts', array(__CLASS__, 'inbound_compatibilities'), 101);
 31      add_action('admin_enqueue_scripts', array(__CLASS__, 'inbound_kill_bogus_admin_scripts'), 100);
 32      add_action('wp_ajax_inbound_dequeue_js', array(__CLASS__, 'inbound_dequeue_js'));
 33      add_action('wp_ajax_nopriv_inbound_dequeue_js', array(__CLASS__, 'inbound_dequeue_js'));
 34      add_action('wp_ajax_inbound_dequeue_admin_js', array(__CLASS__, 'inbound_dequeue_admin_js'));
 35      add_action('wp_ajax_nopriv_inbound_dequeue_admin_js', array(__CLASS__, 'inbound_dequeue_admin_js'));
 36      if (isset($_GET['inbound_js'])){
 37      add_action('wp_enqueue_scripts', array(__CLASS__, 'run_debug_script'), 102);
 38      add_action('admin_enqueue_scripts', array(__CLASS__, 'run_debug_script'), 102);
 39
 40      }
 41    }
 42
 43    static function inbound_output_meta_debug() {
 44        //print all global fields for post
 45        if (isset($_GET['debug']) && ( isset($_GET['post']) && is_numeric($_GET['post']) ) ) {
 46          global $wpdb;
 47          $data   =   array();
 48          $wpdb->query("
 49            SELECT `meta_key`, `meta_value`
 50            FROM $wpdb->postmeta
 51            WHERE `post_id` = ".mysql_real_escape_string($_GET['post'])."
 52          ");
 53
 54          foreach($wpdb->last_result as $k => $v){
 55            $data[$v->meta_key] =   $v->meta_value;
 56          };
 57          if (isset($_GET['post']))
 58          {
 59            echo "<pre>";
 60            print_r($data);
 61            echo "</pre>";
 62          }
 63        }
 64    }
 65
 66    // dequeue all js and set first script, then requeue scripts
 67    static function run_debug_script() {
 68      global $wp_scripts;
 69
 70        if ( !empty( $wp_scripts->queue ) ) {
 71            $store = $wp_scripts->queue; // store the scripts
 72            foreach ( $wp_scripts->queue as $handle ) {
 73                wp_dequeue_script( $handle );
 74            }
 75            //wp_enqueue_script( 'jquery' );
 76            wp_register_script('inbound-debug', INBOUDNOW_SHARED_URLPATH . 'classes/js/debug.js', array('jquery'));
 77            wp_enqueue_script( 'inbound-debug' );
 78
 79            foreach ( $store as $handle ) {
 80                wp_enqueue_script( $handle );
 81            }
 82        }
 83
 84    }
 85
 86    static function inbound_dequeue_js() {
 87      if ( ! self::$add_debug )
 88      return;
 89
 90          // Post Values
 91          $post_id = (isset( $_POST['post_id'] )) ? $_POST['post_id'] : "";
 92          $the_script = (isset( $_POST['the_script'] )) ? $_POST['the_script'] : "";
 93          $status = (isset( $_POST['status'] )) ? $_POST['status'] : "";
 94          $admin_screen = (isset( $_POST['admin_screen'] )) ? $_POST['admin_screen'] : "";
 95
 96          /* Store Script Data to Post */
 97        $script_data = get_post_meta( $post_id, 'inbound_dequeue_js', TRUE );
 98        $script_data = json_decode($script_data,true);
 99          if(is_array($script_data)) {
100
101            if($status === 'off') {
102              // add or remove from list
103              $script_data[$the_script] = $status;
104            } else {
105              unset($script_data[$the_script]);
106            }
107
108        } else {
109          // Create the first item in array
110          if($status === 'off') {
111          $script_data[$the_script] = $status;
112          }
113        }
114          $script_save = json_encode($script_data);
115
116          update_post_meta( $post_id, 'inbound_dequeue_js', $script_save );
117
118          // Set global option inbound_global_dequeue_js
119
120        $output =  array('encode'=> $script_save );
121
122        echo json_encode($output,JSON_FORCE_OBJECT);
123        wp_die();
124     }
125
126     static function inbound_dequeue_admin_js() {
127       if ( ! self::$add_debug )
128       return;
129
130           // Post Values
131           $post_id = (isset( $_POST['post_id'] )) ? $_POST['post_id'] : "";
132           $the_script = (isset( $_POST['the_script'] )) ? $_POST['the_script'] : "";
133           $status = (isset( $_POST['status'] )) ? $_POST['status'] : "";
134           $admin_screen = (isset( $_POST['admin_screen'] )) ? $_POST['admin_screen'] : "";
135
136           /* Store Script Data to Post */
137         $script_data = get_option( 'inbound_global_dequeue' );
138
139         if(is_array($script_data)) {
140
141             if($status === 'off') {
142               // add or remove from list
143               $script_data[$the_script] = $admin_screen;
144             } else {
145               unset($script_data[$the_script]);
146             }
147
148         } else {
149           // Create the first item in array
150           if($status === 'off') {
151           $script_data[$the_script] = $admin_screen;
152           }
153         }
154           update_option( 'inbound_global_dequeue', $script_data );
155
156           // Set global option inbound_global_dequeue_js
157
158         $output =  array('encode'=> $script_data );
159
160         echo json_encode($output,JSON_FORCE_OBJECT);
161         wp_die();
162      }
163
164   static function wp_core_script_whitelist() {
165       // Wordpress Core Scripts List
166       $wp_core_scripts = array("jcrop", "swfobject", "swfupload", "swfupload-degrade", "swfupload-queue", "swfupload-handlers", "jquery", "jquery-form", "jquery-color", "jquery-masonry", "jquery-ui-core", "jquery-ui-widget", "jquery-ui-mouse", "jquery-ui-accordion", "jquery-ui-autocomplete", "jquery-ui-slider", "jquery-ui-progressbar", "jquery-ui-tabs", "jquery-ui-sortable", "jquery-ui-draggable", "jquery-ui-droppable", "jquery-ui-selectable", "jquery-ui-position", "jquery-ui-datepicker", "jquery-ui-tooltip", "jquery-ui-resizable", "jquery-ui-dialog", "jquery-ui-button", "jquery-effects-core", "jquery-effects-blind", "jquery-effects-bounce", "jquery-effects-clip", "jquery-effects-drop", "jquery-effects-explode", "jquery-effects-fade", "jquery-effects-fold", "jquery-effects-highlight", "jquery-effects-pulsate", "jquery-effects-scale", "jquery-effects-shake", "jquery-effects-slide", "jquery-effects-transfer", "wp-mediaelement", "schedule", "suggest", "thickbox", "hoverIntent", "jquery-hotkeys", "sack", "quicktags", "iris", "farbtastic", "colorpicker", "tiny_mce", "autosave", "wp-ajax-response", "wp-lists", "common", "editorremov", "editor-functions", "ajaxcat", "admin-categories", "admin-tags", "admin-custom-fields", "password-strength-meter", "admin-comments", "admin-users", "admin-forms", "xfn", "upload", "postbox", "slug", "post", "page", "link", "comment", "comment-reply", "admin-gallery", "media-upload", "admin-widgets", "word-count", "theme-preview", "json2", "plupload", "plupload-all", "plupload-html4", "plupload-html5", "plupload-flash", "plupload-silverlight", "underscore", "backbone", 'admin-bar', 'media-editor', 'svg-painter', 'wp-auth-check', 'editor', 'utils', 'customize-controls', 'plugin-install', 'customize-loader', 'dashboard');
167
168       // add filter;
169
170       return $wp_core_scripts;
171   }
172
173   static function inbound_now_script_whitelist() {
174       global $wp_scripts;
175       // Match our plugins and whitelist them
176       $registered_scripts = $wp_scripts->registered;
177       $inbound_white_list = array();
178       foreach ($registered_scripts as $handle) {
179          $src = $handle->src;
180          if (!is_array($src)) {
181             if(preg_match("/\/plugins\/leads\//", $src)) {
182               //echo $handle->handle;
183               $inbound_white_list[] = $handle->handle;
184             }
185             if(preg_match("/\/plugins\/cta\//", $handle->src)) {
186               //echo $handle->handle;
187               $inbound_white_list[]= $handle->handle;
188             }
189             if(preg_match("/\/plugins\/landing-pages\//", $handle->src)) {
190               //echo $handle->handle;
191               $inbound_white_list[]= $handle->handle;
192             }
193         }
194       }
195       //print_r($inbound_white_list);
196       return $inbound_white_list;
197     }
198    // Destroy all bad frontend scripts
199    static function inbound_kill_bogus_scripts() {
200        if (!isset($_GET['inbound-dequeue-scripts'])) {
201          global $wp_scripts, $wp_query;
202          $script_list = $wp_scripts->queue; // All enqueued scripts
203          $current_page_id = $wp_query->get_queried_object_id();
204          $script_data = get_post_meta( $current_page_id , 'inbound_dequeue_js', TRUE );
205          $script_data = json_decode($script_data,true);
206
207          $inbound_white_list = self::inbound_now_script_whitelist();
208          $wp_core_scripts = self::wp_core_script_whitelist();
209
210            // dequeue frontent scripts
211            foreach ($script_list as $key => $value) {
212            if (!in_array($value, $inbound_white_list) && !in_array($value, $wp_core_scripts)){
213               // Kill bad scripts
214               if (isset($script_data[$value]) && in_array($script_data[$value], $script_data)) {
215                 wp_dequeue_script( $value ); // Kill bad script
216               }
217             }
218            }
219
220        }
221    }
222
223    // Destroy all bad admin scripts
224    static function inbound_kill_bogus_admin_scripts() {
225        if (!isset($_GET['inbound-dequeue-scripts'])) {
226            // dequeue admin scripts
227            $screen = get_current_screen();
228
229              $array = array('load-qtip' => 'wp-call-to-action');
230              //update_option( 'inbound_global_dequeue', $array );
231              $global_array = get_option( 'inbound_global_dequeue' );
232              //print_r($global_array);
233
234
235            if (!$global_array){
236              return;
237            }
238
239           if(is_array($global_array)) {
240              foreach ($global_array as $key => $value) {
241                if ( $screen->id === $value) {
242                wp_dequeue_script( $key );
243                }
244              }
245            }
246        }
247    }
248
249    static function inbound_compatibilities() {
250
251      if (isset($_GET['inbound-dequeue-scripts']) && current_user_can( 'manage_options' ) ) {
252
253          global $wp_query;
254          $current_page_id = $wp_query->get_queried_object_id();
255          $global_array = get_option( 'inbound_global_dequeue' );
256          if( is_admin() ) {
257            global $post;
258            $screen = get_current_screen();
259            $current =  $screen->id;
260            $page_id = (isset($post->ID)) ? $post->ID : '';
261          } else {
262            $current = '';
263            $screen = '';
264            $page_id = $current_page_id;
265          }
266
267          //show_admin_bar( false );
268          wp_enqueue_script('inbound-dequeue-scripts', INBOUND_CLASS_URL . 'js/inbound-dequeue-scripts.js', array( 'jquery' ));
269          wp_localize_script( 'inbound-dequeue-scripts' , 'inbound_debug' , array( 'admin_url' => admin_url( 'admin-ajax.php' ), 'admin_screen' => $current, 'page_id' => $page_id));
270
271            global $wp_scripts;
272
273            $scripts_registers = $wp_scripts->registered;
274            //echo "<pre>";
275            //print_r($scripts_registers);
276            //echo $scripts_registers['common']->src;
277
278
279            $script_list = $wp_scripts->queue; // All enqueued scripts
280            $inbound_white_list = self::inbound_now_script_whitelist();
281            $wp_core_scripts = self::wp_core_script_whitelist();
282            // TURN OFF ALL OTHER SCRIPTS FOR DISABLING
283            $count = 0;
284            foreach ($script_list as $key => $value) {
285            // echo $key . $value;
286            if (!in_array($value, $inbound_white_list) && !in_array($value, $wp_core_scripts)){
287               wp_dequeue_script( $value );
288               $count++;
289             }
290
291            }
292            // If no scripts third party enqueued scripts leave
293
294           /* echo "<pre>";
295             print_r($wp_scripts->queue);
296             echo "</pre>"; */
297
298             echo '<style type="text/css" media="screen">
299             #launch-feedback {
300             display:none;
301           }
302             #group{text-align: left;border-bottom: 1px solid #fff;position:relative;margin:0 auto;padding:6px 10px 10px;background-image:linear-gradient(top,rgba(255,255,255,.1),rgba(0,0,0,.1));background-color:#555;width:300px}#group:after{content:" ";position:absolute;z-index:1;top:0;left:0;right:0;bottom:0;border-radius:5px}.switch{margin: 0px;position:relative;border:0;padding:0;width:245px;font-family:helvetica;font-weight:700;font-size:22px;color:#222;text-shadow:0 1px 0 rgba(255,255,255,.3)}.switch legend{float:left;width: 98px;padding:7px 10% 3px 0;text-align:left; color:#fff;}
303           .switch input{position:absolute;opacity:0}
304           .switch legend:after{content:"";position:absolute;top:0;left:50%;z-index:0;width:50%;height:100%;padding:2px;background-color:#222;border-radius:3px;box-shadow:inset -1px 2px 5px rgba(0,0,0,.8),0 1px 0 rgba(255,255,255,.2)}
305           .switch label{position:relative;z-index:2;float:left;width:61px;margin-top:2px;padding:5px 0 3px;text-align:center;color:#64676b;text-shadow:0 1px 0 #000;cursor:pointer;transition:color 0s ease .1s}
306           .switch input:checked+label{color:#fff}.switch input:focus+label{outline:0}.switch .switch-button{clear:both;position:absolute;top:-1px;left:50%;z-index:1;width:63px; height:100%;margin:2px;background-color:#70c66b;background-image:linear-gradient(top,rgba(255,255,255,.2),rgba(0,0,0,0));border-radius:3px;box-shadow:0 0 0 2px #70c66b,-2px 3px 2px #000;transition:all .3s ease-out}.switch .switch-button:after{content:" ";position:absolute;z-index:1;top:0;left:0;right:0;bottom:0;border-radius:3px;border:1px dashed #fff}#inbound-dequeue-id{display:none}.switch input:last-of-type:checked~.switch-button{left:75%}.switch .switch-button.status-off{background-color:red;box-shadow:0 0 0 3px red,-2px 3px 5px #000}.switch label.turn-on{color:#fff}
307           .script-info {padding-left:5px; position: absolute; z-index:999999999;}
308           .debug-plugin-name { font-size:13px; color:#fff; text-shadow:none; padding-bottom: 6px;
309display: inline-block; }
310           .debug-plugin-name span.debug-head, .debug-script-head {color:#ccc; width: 45px;
311display: inline-block;}
312           .js-title { display:block; margin-bottom: 7px; color:#fff;}
313           .fa-info-circle:before{ content: "\f05a"; font-family: FontAwesome !important; font-style:normal;}
314           #group:last-of-type {
315            padding-bottom:80px;
316            }
317            #main-debug-title {
318            font-family: helvetica;
319            font-weight: 700;
320            font-size: 22px;
321            color: #222;
322            background: #ccc;
323            padding: 10px;
324            text-align: center;
325            text-shadow: 0 1px 0 rgba(255,255,255,.3);
326          }
327          #no-js-to-turn-off {width: 286px;}
328          #no-js-to-turn-off span {font-size:22px; line-height:25px; padding:10px; display:inline-block;}
329          #debug-close-link { color:red; float:right; font-size:10px; text-decoration:none;}
330            </style>';
331
332        $script_data = get_post_meta( $current_page_id , 'inbound_dequeue_js', TRUE );
333        $script_data = json_decode($script_data,true);
334        $close_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
335        $close_link = str_replace(array("&inbound-dequeue-scripts", "?inbound-dequeue-scripts"), "", $close_link);
336
337             echo '<div id="inbound-fix-page" class="'.$current_page_id.'" data-page-id="'.$page_id.'" data-admin-screen="'.$current.'" style="position:fixed; right:0px; padding-bottom: 80px; background-color: #555; overflow:auto; height: 100%; top: 32px; background:#fff; border: 1px solid; z-index: 999999999999; line-height: 1; width: 317px;">';
338             echo "<div id='main-debug-title'>Turn off Javascript<a id='debug-close-link' href='".$close_link."'>Close</a></div>";
339             if ($count === 0) {
340               echo "<div id='no-js-to-turn-off'><span style=''>No javascript files found to dequeue</span></div></div>";
341               return;
342             }
343             echo "<span id='inbound-dequeue-id'>".$current_page_id."</span>";
344
345             foreach ($script_list as $key => $value) {
346              if (!in_array($value, $inbound_white_list) && !in_array($value, $wp_core_scripts)){
347              $checked =  "";
348              $status_class = "";
349                // Kill bad frontend script
350                if (isset($script_data[$value]) && in_array($script_data[$value], $script_data)){
351                  $checked =  "checked";
352                  $status_class =  "status-off";
353                  wp_dequeue_script( $value ); // Kill bad script
354                }
355                // Kill bad admin script
356              if (is_array($global_array)) {
357                  if (is_admin() && array_key_exists($value, $global_array)) {
358
359                    if ($current === $global_array[$value] ) {
360                    $checked =  "checked";
361                    $status_class =  "status-off";
362                    wp_dequeue_script( $value ); // Kill bad script
363                    }
364                  }
365              }
366
367              $actual_link = $scripts_registers[$value]->src;
368              str_replace("?frontend=false", "", $actual_link);
369              preg_match('/plugins\/([^\/]+?)\/(?:[^\/]+\/)?(.+)/', $actual_link, $matches);
370              preg_match('/themes\/([^\/]+?)\/(?:[^\/]+\/)?(.+)/', $actual_link, $matches_two);
371
372              $name_of_file = (isset($matches_two[1])) ? "<span class='debug-head'>Theme:</span> " . $matches_two[1] : '';
373              if ($name_of_file === "") {
374                  $name_of_file = (isset($matches[1])) ? "<span class='debug-head'>Plugin:</span> " . $matches[1] : '<span class="debug-head">From:</span> Wordpress Core Script <span style="color:#db3d3d; font-size:12px;">(Don\'t turn off)</span>';
375              }
376
377
378              echo '<div id="group">';
379              echo '<span class="debug-plugin-name">'.$name_of_file.'</span>';
380              echo "<div class='js-title'><span class='debug-script-head'>Script:</span> ". $value ."<span  title='".$scripts_registers[$value]->src."' class='script-info'><i class='fa fa-info-circle'></i></span></div>";
381               echo '<fieldset class="switch" id="'.$value.'">
382                    <legend>Status:</legend>
383
384                    <input id="'.$value.'-on" name="'.$value.'-status" type="radio" '.$checked.'>
385                    <label for="'.$value.'-on" class="turn-on">On</label>
386
387                    <input id="'.$value.'-off" name="'.$value.'-status" type="radio" '.$checked.'>
388                    <label for="'.$value.'-off" class="turn-off">Off</label>
389
390                    <span class="switch-button '.$status_class.'"></span>
391                  </fieldset>
392
393                  </div>';
394              }
395             }
396             echo "</div>";
397
398             // This will control the dequing
399             /*
400             foreach ($scripts_queued as $key => $value) {
401              //echo $key . $value;
402              if (!in_array($value, $white_list_scripts)){
403                wp_dequeue_script( $value );
404              }
405
406             } */
407        }
408
409    }
410}
411}
412/*  Initialize InboundNow Debug
413 *  --------------------------------------------------------- */
414
415Inbound_Debug_Scripts::init();
416
417?>