| 1 |  |  | <?php | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | * Plugin Name: PageSpeed Purge Button | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | * Plugin URI:  https://github.com/salaros/wp-purge-pagespeed-button | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | * Description: One-click PageSpeed cache purging using an admin bar button | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | * Version:     0.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | * Author:      Zhmayev Yaroslav aka Salaros | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | * Author URI:  https://salaros.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | * License:     MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | * License URI: https://opensource.org/licenses/MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | * Text Domain: wp-purge-pagespeed-button | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | * Domain Path: /languages/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | if ( ! defined( 'ABSPATH' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	exit; // Exit if accessed directly. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class PageSpeedPurge { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	public function __construct() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 		// Enqueue css and js files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 		if( ! is_admin() && is_admin_bar_showing() ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 			add_action( 'wp_enqueue_scripts', array( $this, 'embed_admin_assets' ), 101 ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 			add_action( 'admin_enqueue_scripts', array( $this, 'embed_admin_assets' ), 101 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 		// Add 'Clear Redis cache' button to the admin bar | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		add_action( 'admin_bar_menu', array( $this, 'add_admin_bar_button' ), 101 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		// Load plugin textdomain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | 	public function embed_admin_assets() { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  | 		// This is where you can add your CSS/JS entries for wp-admin UI | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  | 		$plugin_url = plugin_dir_url( __FILE__ ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  | 		$asset_suffix = ( defined( SCRIPT_DEBUG ) && ! empty( SCRIPT_DEBUG ) ) ? '.min' : ''; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  | 		wp_enqueue_style( 'admin-styles', sprintf( '%s/assets/wp-purge-pagespeed-button%s.css', $plugin_url, $asset_suffix ) ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  | 		wp_enqueue_script( 'admin-styles', sprintf( '%s/assets/wp-purge-pagespeed-button%s.js', $plugin_url, $asset_suffix ), array( 'jquery' ) ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	public function add_admin_bar_button( $wp_admin_bar ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		$args = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 			'id'	=> 'pagespeed_purge', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 			'href'	=> '#pagespeed_purge', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 			'title'	=> __( 'Purge Pagespeed Cache', 'wp-purge-pagespeed-button' ), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		$wp_admin_bar->add_menu( $args ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	public function load_plugin_textdomain() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		$textdomain = 'wp-purge-pagespeed-button'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$plugin_locale = apply_filters( 'plugin_locale', get_locale(), $textdomain ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		$plugin_dir = dirname( __FILE__ ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		// wp-content/languages/plugin-name/plugin-name-ru_RU.mo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		load_textdomain( $textdomain, sprintf( '%s/plugins/%s-%s.mo', WP_LANG_DIR , $textdomain, $plugin_locale ) ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		// wp-content/plugins/plugin-name/languages/plugin-name-ru_RU.mo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		load_textdomain( $textdomain, sprintf( '%s/languages/%s-%s.mo', $plugin_dir , $textdomain, $plugin_locale ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | add_action( 'plugins_loaded', function () { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     new PageSpeedPurge(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  | } ); | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |  | 
            
                        
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.