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