Feature
3 months ago
Integration
2 months ago
Interfaces
1 year ago
Util
1 year ago
WordPress
2 months ago
ModuleHandler.php
4 months ago
PageSpeedBoost.php
3 months ago
PluginStateHandler.php
11 months ago
PageSpeedBoost.php
283 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WP Engine NitroPack. |
| 4 | * |
| 5 | * @owner wpengine/vortechs |
| 6 | */ |
| 7 | |
| 8 | namespace NitroPack; |
| 9 | |
| 10 | use WP_CLI; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || die( 'No script kiddies please!' ); |
| 13 | |
| 14 | /** |
| 15 | * NitroPack class adds WP Engine specific content and navigation to NitroPack plugin. |
| 16 | */ |
| 17 | class PageSpeedBoost { |
| 18 | |
| 19 | /** |
| 20 | * Pair of public and private keys |
| 21 | * |
| 22 | * |
| 23 | * @var null|object |
| 24 | */ |
| 25 | protected $keys; |
| 26 | /** |
| 27 | * Registers hooks. |
| 28 | */ |
| 29 | public function __construct() { |
| 30 | |
| 31 | global $pagenow; |
| 32 | |
| 33 | $is_wp_cli = nitropack_is_wp_cli(); |
| 34 | |
| 35 | if ( ! is_admin() && ! $is_wp_cli ) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | // These filters are triggered only when the dependent plugin is in OneClick mode. |
| 40 | add_filter( 'nitropack_oneclick_connect_url', array( $this, 'get_connect_url' ), 10, 1 ); |
| 41 | add_filter( 'nitropack_oneclick_vendor_widget', array( $this, 'get_vendor_widget' ), 10, 1 ); |
| 42 | add_filter( 'nitropack_oneclick_action_links', array( $this, 'get_action_links' ), 10, 1 ); |
| 43 | add_filter( 'nitropack_oneclick_safemode_message', array( $this, 'get_safe_mode_warning' ), 10, 1 ); |
| 44 | |
| 45 | // Make sure to run these filters when the dependent plugin is in OneClick mode. |
| 46 | if ( $this->is_one_click() ) { |
| 47 | add_filter( 'plugin_row_meta', array( $this, 'add_plugin_metadata' ), 10, 4 ); |
| 48 | add_filter( 'plugin_action_links', array( $this, 'plugin_action_links' ), 10, 4 ); |
| 49 | add_filter( 'network_admin_plugin_action_links', array( $this, 'plugin_action_links' ), 10, 4 ); |
| 50 | } |
| 51 | |
| 52 | if ( $is_wp_cli ) { |
| 53 | WP_CLI::add_command( 'psb connect', array( $this, 'command_psb_connect' ) ); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Gets the product landing page URL. |
| 59 | * |
| 60 | * @return string Returns the product landing page URL. |
| 61 | */ |
| 62 | protected function get_product_landing_page_url() { |
| 63 | return 'https://wpengine.com/page-speed-boost'; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Gets the product page URL. |
| 68 | * |
| 69 | * @return string Returns the product page URL. |
| 70 | */ |
| 71 | protected function get_product_page_url() { |
| 72 | return 'https://my.wpengine.com/products/nitropack'; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Gets the URL to complete product enablement. |
| 77 | * |
| 78 | * @param string $url The URL to filter. |
| 79 | * |
| 80 | * @return string Returns the URL. |
| 81 | */ |
| 82 | public function get_connect_url( $url = '' ) { |
| 83 | return $this->get_product_page_url() . ( defined( 'PWP_NAME' ) ? '/enable?selected=' . PWP_NAME : '/add' ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Gets WP Engine specific content for NitroPack settings page. |
| 88 | * |
| 89 | * @param string $html HTML to filter. |
| 90 | * |
| 91 | * @return string Returns HTML. |
| 92 | */ |
| 93 | public function get_vendor_widget( $html = '' ) { |
| 94 | return '<h5 class="card-title">' |
| 95 | . __( 'What is NitroPack?', 'wpengine' ) |
| 96 | . '</h5>' |
| 97 | . '<div class="row mt-4 mx-0" style="line-height:24px">' |
| 98 | . __( 'NitroPack caching solution is preconfigured with essential features for immediate use. Activate it effortlessly and enjoy an instant boost in page speed.', 'wpengine' ) |
| 99 | . '</div>' |
| 100 | . '<div class="row mt-4 mx-0">' |
| 101 | . '<a href="' . $this->get_product_landing_page_url() . '" target="_blank">' . __( 'Learn more' ) . '</a>' |
| 102 | . '</div>' |
| 103 | . '<div class="row mt-4 mx-0">' |
| 104 | . '<a href="' . $this->get_product_page_url() . '" target="_blank" class="btn btn-light btn-outline-primary">' . __( 'Manage' ) . '</a>' |
| 105 | . '</div>'; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Gets the message to complete product enablement. |
| 110 | * |
| 111 | * @param string $html HTML message to filter. |
| 112 | * |
| 113 | * @return string Returns HTML message. |
| 114 | */ |
| 115 | public function get_safe_mode_warning( $html = '' ) { |
| 116 | return '<strong>Important:</strong> Product enablement is not finished. Currently, your website does not serve optimizations to regular users. Complete product enablement in the ' |
| 117 | . '<a href="' . $this->get_connect_url() . '" target="_blank">User Portal</a>.'; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Gets action links displayed on the plugins list. |
| 122 | * |
| 123 | * @param array $links An array of HTML links. |
| 124 | * |
| 125 | * @return array Returns the array of HTML links. |
| 126 | */ |
| 127 | public function get_action_links( $links = array() ) { |
| 128 | return array( |
| 129 | '<a href="https://wpengine.com/support/page-speed-boost" target="_blank" rel="noopener noreferrer">' . __( 'Support Center', 'wpengine' ) . '</a>', |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Filters the action links displayed for each plugin in the Plugins list table. |
| 135 | * |
| 136 | * @param array $actions An array of plugin action links. |
| 137 | * @param string $plugin_file Path to the plugin file relative to the plugins directory. |
| 138 | * @param array $plugin_data An array of plugin data. |
| 139 | * @param string $context The plugin context. By default this can include 'all', 'active', 'inactive', 'recently_activated', 'upgrade', 'mustuse', 'dropins', and 'search'. |
| 140 | * |
| 141 | * @return array Returns an array of plugin action links. |
| 142 | */ |
| 143 | public function plugin_action_links( $actions, $plugin_file, $plugin_data, $context = 'all' ) { |
| 144 | if ( strpos( $plugin_file, 'nitropack/' ) !== 0 ) { |
| 145 | return $actions; |
| 146 | } |
| 147 | ; |
| 148 | |
| 149 | if ( isset( $actions['edit'] ) ) { |
| 150 | unset( $actions['edit'] ); |
| 151 | } |
| 152 | |
| 153 | if ( isset( $actions['delete'] ) ) { |
| 154 | unset( $actions['delete'] ); |
| 155 | } |
| 156 | |
| 157 | return $actions; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Adds the link to the product landing page. |
| 162 | * Filters the array of row meta for each plugin in the Plugins list table. |
| 163 | * |
| 164 | * @param array $plugin_meta An array of the plugin's metadata, including the version, author, author URI, and plugin URI. |
| 165 | * @param string $plugin_file Path to the plugin file relative to the plugins directory. |
| 166 | * @param array $plugin_data An array of plugin data. |
| 167 | * @param string $status Status filter currently applied to the plugin list. Possible values are: 'all', 'active', 'inactive', 'recently_activated', 'upgrade', 'mustuse', 'dropins', 'search', 'paused', 'auto-update-enabled', 'auto-update-disabled'. |
| 168 | * |
| 169 | * @return array Returns an array of the plugin's metadata. |
| 170 | */ |
| 171 | public function add_plugin_metadata( $plugin_meta, $plugin_file, $plugin_data, $status = 'all' ) { |
| 172 | if ( strpos( $plugin_file, 'nitropack/' ) === 0 ) { |
| 173 | array_splice( $plugin_meta, 2, 0, '<a href="' . $this->get_product_landing_page_url() . '" target="_blank">WP Engine</a>' ); |
| 174 | } |
| 175 | |
| 176 | return $plugin_meta; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Checks if the plugin distribution is OneClick. |
| 181 | * |
| 182 | * @return bool |
| 183 | */ |
| 184 | protected function is_one_click() { |
| 185 | if ( function_exists( 'get_nitropack' ) ) { |
| 186 | if ( method_exists( get_nitropack(), 'getDistribution' ) ) { |
| 187 | return 'oneclick' === get_nitropack()->getDistribution(); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return 'oneclick' === get_option( 'nitropack-distribution' ); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Get vendor API. |
| 196 | * |
| 197 | * @return \NitroPack\SDK\Api API. |
| 198 | */ |
| 199 | protected function get_vendor_api() { |
| 200 | return get_nitropack()->getSdk()->getApi(); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Get site configuration. |
| 205 | * |
| 206 | * @return null|array Returns site configuration on success or exits with an error. |
| 207 | */ |
| 208 | protected function get_site_config() { |
| 209 | if ( ! function_exists( 'get_nitropack' ) ) { |
| 210 | WP_CLI::error( 'The dependent plugin is missing or is inactive!' ); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | $site_config = get_nitropack()->getSiteConfig(); |
| 215 | if ( empty( $site_config['siteId'] ) || empty( $site_config['siteSecret'] ) ) { |
| 216 | WP_CLI::error( 'The Site ID or Site Secret is missing!' ); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | return $site_config; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Generate and get public and private keys. |
| 225 | * |
| 226 | * @return object |
| 227 | */ |
| 228 | protected function keys_instance() { |
| 229 | // This must be executed only once per request. |
| 230 | if ( empty( $this->keys ) ) { |
| 231 | $this->keys = \NitroPack\SDK\Crypto::generateKeyPair(); |
| 232 | } |
| 233 | |
| 234 | return $this->keys; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Get vendor plugin file relative to plugins directory. |
| 239 | * |
| 240 | * @return string |
| 241 | */ |
| 242 | protected function get_vendor_plugin_file() { |
| 243 | $plugins = get_plugins(); |
| 244 | foreach ( $plugins as $plugin_file => $plugin ) { |
| 245 | if ( strpos( $plugin_file, 'nitropack/' ) === 0 ) { |
| 246 | return $plugin_file; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return ''; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * PSB connect. Do not run it manually. |
| 255 | * |
| 256 | * ## OPTIONS |
| 257 | * |
| 258 | * <site_ID> |
| 259 | * : The site ID. |
| 260 | * |
| 261 | * <site_secret> |
| 262 | * : The site secret. |
| 263 | * |
| 264 | * @when before_wp_load |
| 265 | * @param array $psb_command_args Command arguments. |
| 266 | * @param array $psb_command_assoc_args Command parameters. |
| 267 | */ |
| 268 | public function command_psb_connect( $psb_command_args, $psb_command_assoc_args ) { |
| 269 | if ( ! function_exists( 'nitropack_verify_connect' ) ) { |
| 270 | $psb_vendor_plugin_file = $this->get_vendor_plugin_file(); |
| 271 | include_once WP_PLUGIN_DIR . '/' . $psb_vendor_plugin_file; |
| 272 | if ( ! function_exists( 'nitropack_verify_connect' ) ) { |
| 273 | WP_CLI::error( 'The dependent plugin in incompatible!' ); |
| 274 | return; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | nitropack_verify_connect( |
| 279 | empty( $psb_command_args[0] ) ? '' : $psb_command_args[0], |
| 280 | empty( $psb_command_args[1] ) ? '' : $psb_command_args[1] |
| 281 | ); |
| 282 | } |
| 283 | } |