| @@ -25,23 +25,25 @@ | ||
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | 27 | * Add actions and filters only if this is the plugin installation screen and it's the first page. |
| 28 | 28 | * |
| 29 | + * @since 4.12 | |
| 30 | + * | |
| 29 | 31 | * @param object $screen WP Screen object. |
| 30 | 32 | * |
| 31 | - * @since 4.12 | |
| 32 | - * | |
| 33 | 33 | * @return void |
| 34 | 34 | */ |
| 35 | 35 | public function start( $screen ) { |
| 36 | 36 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 37 | - if ( 'plugin-install' === $screen->base && ( ! isset( $_GET['paged'] ) || 1 === intval( $_GET['paged'] ) ) ) { | |
| 38 | - add_filter( 'plugins_api_result', array( $this, 'inject_suggestion' ), 10, 3 ); | |
| 39 | - add_filter( 'self_admin_url', array( $this, 'plugin_details' ) ); | |
| 40 | - add_filter( 'plugin_install_action_links', array( $this, 'insert_related_links' ), 10, 2 ); | |
| 41 | - add_action( 'admin_enqueue_scripts', array( $this, 'load_plugins_search_script' ) ); | |
| 42 | - $this->maybe_dismiss(); | |
| 37 | + if ( 'plugin-install' !== $screen->base || ( isset( $_GET['paged'] ) && 1 !== intval( $_GET['paged'] ) ) ) { | |
| 38 | + return; | |
| 43 | 39 | } |
| 40 | + | |
| 41 | + add_filter( 'plugins_api_result', array( $this, 'inject_suggestion' ), 10, 3 ); | |
| 42 | + add_filter( 'self_admin_url', array( $this, 'plugin_details' ) ); | |
| 43 | + add_filter( 'plugin_install_action_links', array( $this, 'insert_related_links' ), 10, 2 ); | |
| 44 | + add_action( 'admin_enqueue_scripts', array( $this, 'load_plugins_search_script' ) ); | |
| 45 | + $this->maybe_dismiss(); | |
| 44 | 46 | } |
| 45 | 47 | |
| 46 | 48 | /** |
| 47 | 49 | * Intercept the plugins API response and add in an appropriate card. |
| @@ -57,13 +59,19 @@ | ||
| 57 | 59 | if ( empty( $args->search ) ) { |
| 58 | 60 | return $result; |
| 59 | 61 | } |
| 60 | 62 | |
| 63 | + if ( is_wp_error( $result ) ) { | |
| 64 | + // This may happen if the request failed due to an internet connection issue. | |
| 65 | + return $result; | |
| 66 | + } | |
| 67 | + | |
| 61 | 68 | $addon_list = $this->get_addons(); |
| 62 | 69 | |
| 63 | 70 | // Lowercase, trim, remove punctuation/special chars, decode url, remove 'formidable'. |
| 64 | 71 | $normalized_term = $this->search_to_array( $args->search ); |
| 65 | - if ( empty( $normalized_term ) ) { | |
| 72 | + | |
| 73 | + if ( ! $normalized_term ) { | |
| 66 | 74 | // Don't add anything extra. |
| 67 | 75 | return $result; |
| 68 | 76 | } |
| 69 | 77 | |
| @@ -68,24 +76,25 @@ | ||
| 68 | 76 | } |
| 69 | 77 | |
| 70 | 78 | $matching_addon = $this->matching_addon( $addon_list, $normalized_term ); |
| 71 | 79 | |
| 72 | - if ( empty( $matching_addon ) || ! $this->should_display_hint( $matching_addon ) ) { | |
| 80 | + if ( ! $matching_addon || ! $this->should_display_hint( $matching_addon ) ) { | |
| 73 | 81 | return $result; |
| 74 | 82 | } |
| 75 | 83 | |
| 76 | 84 | $inject = (array) $this->get_plugin_data(); |
| 77 | 85 | $overrides = array( |
| 78 | - 'plugin-search' => true, // Helps to determine if that an injected card. | |
| 79 | - 'name' => sprintf( | |
| 86 | + // Helps to determine if that an injected card. | |
| 87 | + 'plugin-search' => true, | |
| 88 | + 'name' => sprintf( | |
| 80 | 89 | /* translators: Formidable addon name */ |
| 81 | 90 | esc_html_x( 'Formidable %s', 'Formidable Addon Name', 'formidable' ), |
| 82 | 91 | $addon_list[ $matching_addon ]['name'] |
| 83 | 92 | ), |
| 84 | - 'addon' => $addon_list[ $matching_addon ]['slug'], | |
| 85 | - 'short_description' => $addon_list[ $matching_addon ]['excerpt'], | |
| 86 | - 'slug' => self::$slug, | |
| 87 | - 'version' => $addon_list[ $matching_addon ]['version'], | |
| 93 | + 'addon' => $addon_list[ $matching_addon ]['slug'], | |
| 94 | + 'short_description' => $addon_list[ $matching_addon ]['excerpt'], | |
| 95 | + 'slug' => self::$slug, | |
| 96 | + 'version' => $addon_list[ $matching_addon ]['version'], | |
| 88 | 97 | ); |
| 89 | 98 | |
| 90 | 99 | if ( ! empty( $addon_list[ $matching_addon ]['external'] ) ) { |
| 91 | 100 | unset( $overrides['name'] ); |
| @@ -122,13 +131,12 @@ | ||
| 122 | 131 | if ( ! isset( $addon_opts['search_terms'] ) ) { |
| 123 | 132 | $addon_opts['search_terms'] = ''; |
| 124 | 133 | } |
| 125 | 134 | |
| 126 | - $addon_terms = $this->search_to_array( $addon_opts['search_terms'] . ' ' . $addon_opts['name'] ); | |
| 135 | + $addon_terms = $this->search_to_array( $addon_opts['search_terms'] . ' ' . $addon_opts['name'] ); | |
| 136 | + $matched_terms = array_intersect( $addon_terms, $normalized_term ); | |
| 127 | 137 | |
| 128 | - $matches = ! empty( array_intersect( $addon_terms, $normalized_term ) ); | |
| 129 | - | |
| 130 | - if ( $matches ) { | |
| 138 | + if ( count( $matched_terms ) === count( $normalized_term ) ) { | |
| 131 | 139 | $matching_addon = $addon_id; |
| 132 | 140 | break; |
| 133 | 141 | } |
| 134 | 142 | } |
| @@ -181,18 +189,20 @@ | ||
| 181 | 189 | |
| 182 | 190 | /** |
| 183 | 191 | * Modify URL used to fetch to plugin information so it pulls Formidable plugin page. |
| 184 | 192 | * |
| 193 | + * @since 4.12 | |
| 194 | + * | |
| 185 | 195 | * @param string $url URL to load in dialog pulling the plugin page from wporg. |
| 186 | 196 | * |
| 187 | - * @since 4.12 | |
| 188 | - * | |
| 189 | 197 | * @return string The URL with 'formidable' instead of 'frm-plugin-search'. |
| 190 | 198 | */ |
| 191 | 199 | public function plugin_details( $url ) { |
| 200 | + // phpcs:disable Generic.WhiteSpace.ScopeIndent | |
| 192 | 201 | return false !== stripos( $url, 'tab=plugin-information&plugin=' . self::$slug ) |
| 193 | 202 | ? 'plugin-install.php?tab=plugin-information&plugin=formidable&TB_iframe=true&width=600&height=550' |
| 194 | 203 | : $url; |
| 204 | + // phpcs:enable Generic.WhiteSpace.ScopeIndent | |
| 195 | 205 | } |
| 196 | 206 | |
| 197 | 207 | /** |
| 198 | 208 | * @since 4.12 |
| @@ -200,9 +210,10 @@ | ||
| 200 | 210 | * @return void |
| 201 | 211 | */ |
| 202 | 212 | private function maybe_dismiss() { |
| 203 | 213 | $addon = FrmAppHelper::get_param( 'frm-dismiss', '', 'get', 'absint' ); |
| 204 | - if ( ! empty( $addon ) ) { | |
| 214 | + | |
| 215 | + if ( $addon ) { | |
| 205 | 216 | $this->add_to_dismissed_hints( $addon ); |
| 206 | 217 | } |
| 207 | 218 | } |
| 208 | 219 | |
| @@ -214,9 +225,9 @@ | ||
| 214 | 225 | * @return array List of dismissed hints. |
| 215 | 226 | */ |
| 216 | 227 | protected function get_dismissed_hints() { |
| 217 | 228 | $dismissed_hints = get_option( self::$dismissed_opt ); |
| 218 | - return ! empty( $dismissed_hints ) && is_array( $dismissed_hints ) ? $dismissed_hints : array(); | |
| 229 | + return $dismissed_hints && is_array( $dismissed_hints ) ? $dismissed_hints : array(); | |
| 219 | 230 | } |
| 220 | 231 | |
| 221 | 232 | /** |
| 222 | 233 | * Save the hint in the list of dismissed hints. |
| @@ -228,9 +239,9 @@ | ||
| 228 | 239 | * @return bool Whether the card was added to the list and hence dismissed. |
| 229 | 240 | */ |
| 230 | 241 | protected function add_to_dismissed_hints( $hint ) { |
| 231 | 242 | $hints = array_merge( $this->get_dismissed_hints(), array( $hint ) ); |
| 232 | - return update_option( self::$dismissed_opt, $hints, 'no' ); | |
| 243 | + return update_option( self::$dismissed_opt, $hints, false ); | |
| 233 | 244 | } |
| 234 | 245 | |
| 235 | 246 | /** |
| 236 | 247 | * Checks that the addon slug passed should be displayed. |
| @@ -258,20 +269,19 @@ | ||
| 258 | 269 | * Take a raw search query and return something a bit more standardized and |
| 259 | 270 | * easy to work with. |
| 260 | 271 | * |
| 261 | 272 | * @param string $term The raw search term. |
| 273 | + * | |
| 262 | 274 | * @return string A simplified/sanitized version. |
| 263 | 275 | */ |
| 264 | 276 | private function sanitize_search_term( $term ) { |
| 265 | 277 | $term = strtolower( urldecode( $term ) ); |
| 266 | 278 | |
| 267 | - // remove non-alpha/space chars. | |
| 279 | + // Remove non-alpha/space chars. | |
| 268 | 280 | $term = preg_replace( '/[^a-z ]/', '', $term ); |
| 269 | 281 | |
| 270 | - // remove strings that don't help matches. | |
| 271 | - $term = trim( str_replace( array( 'formidable', 'free', 'wordpress', 'wp ', 'plugin' ), '', $term ) ); | |
| 272 | - | |
| 273 | - return $term; | |
| 282 | + // Remove strings that don't help matches. | |
| 283 | + return trim( str_replace( array( 'formidable', 'free', 'wordpress', 'wp ', 'plugin' ), '', $term ) ); | |
| 274 | 284 | } |
| 275 | 285 | |
| 276 | 286 | /** |
| 277 | 287 | * @since 4.12 |
| @@ -276,8 +286,9 @@ | ||
| 276 | 286 | /** |
| 277 | 287 | * @since 4.12 |
| 278 | 288 | * |
| 279 | 289 | * @param string $terms |
| 290 | + * | |
| 280 | 291 | * @return array |
| 281 | 292 | */ |
| 282 | 293 | private function search_to_array( $terms ) { |
| 283 | 294 | $terms = $this->sanitize_search_term( $terms ); |
| @@ -299,9 +310,9 @@ | ||
| 299 | 310 | |
| 300 | 311 | // By the time this filter is applied, self_admin_url was already applied and we don't need it anymore. |
| 301 | 312 | remove_filter( 'self_admin_url', array( $this, 'plugin_details' ) ); |
| 302 | 313 | |
| 303 | - $links = array(); | |
| 314 | + $links = array(); | |
| 304 | 315 | $is_installed = $this->is_installed( $plugin['plugin'] ); |
| 305 | 316 | $is_active = is_plugin_active( $plugin['plugin'] ); |
| 306 | 317 | |
| 307 | 318 | // Plugin installed, active, feature not enabled; prompt to enable. |
| @@ -306,17 +317,17 @@ | ||
| 306 | 317 | |
| 307 | 318 | // Plugin installed, active, feature not enabled; prompt to enable. |
| 308 | 319 | if ( ! $is_active && $is_installed ) { |
| 309 | 320 | if ( current_user_can( 'activate_plugins' ) ) { |
| 310 | - $activate_url = add_query_arg( | |
| 321 | + $activate_url = add_query_arg( | |
| 311 | 322 | array( |
| 312 | - 'action' => 'activate', | |
| 323 | + 'action' => 'activate', | |
| 313 | 324 | '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $plugin['plugin'] ), |
| 314 | 325 | 'plugin' => $plugin['plugin'], |
| 315 | 326 | ), |
| 316 | 327 | admin_url( 'plugins.php' ) |
| 317 | 328 | ); |
| 318 | - $links['frm_get_started'] = '<a href="' . esc_url( $activate_url ) . '" class="button activate-now" aria-label="Activate ' . esc_attr( $plugin['name'] ) . '">' . __( 'Activate', 'formidable' ) . '</a>'; | |
| 329 | + $links['frm_get_started'] = '<a href="' . esc_url( $activate_url ) . '" class="button activate-now" aria-label="Activate ' . esc_attr( $plugin['name'] ) . '">' . esc_html__( 'Activate', 'formidable' ) . '</a>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 319 | 330 | } |
| 320 | 331 | } elseif ( ! $is_active && isset( $plugin['url'] ) ) { |
| 321 | 332 | // Go to the add-ons page to install. |
| 322 | 333 | $links[] = '<a |
| @@ -321,10 +332,9 @@ | ||
| 321 | 332 | // Go to the add-ons page to install. |
| 322 | 333 | $links[] = '<a |
| 323 | 334 | class="button-secondary" |
| 324 | 335 | href="' . esc_url( admin_url( 'admin.php?page=formidable-addons' ) ) . '" |
| 325 | - >' . __( 'Install Now', 'formidable' ) . '</a>'; | |
| 326 | - | |
| 336 | + >' . esc_html__( 'Install Now', 'formidable' ) . '</a>'; | |
| 327 | 337 | } elseif ( ! empty( $plugin['link'] ) ) { |
| 328 | 338 | // Add link pointing to a relevant doc page in formidable.com. |
| 329 | 339 | $links[] = '<a |
| 330 | 340 | class="button-primary frm-plugin-search__learn-more" |
| @@ -331,14 +341,14 @@ | ||
| 331 | 341 | href="' . esc_url( FrmAppHelper::admin_upgrade_link( 'plugin-learn-more', $plugin['link'] ) ) . '" |
| 332 | 342 | target="_blank" |
| 333 | 343 | data-addon="' . esc_attr( $plugin['addon'] ) . '" |
| 334 | 344 | >' . esc_html__( 'Learn more', 'formidable' ) . '</a>'; |
| 335 | - } | |
| 345 | + }//end if | |
| 336 | 346 | |
| 337 | 347 | // Dismiss link. |
| 338 | 348 | $dismiss = add_query_arg( array( 'frm-dismiss' => $plugin['id'] ) ); |
| 339 | 349 | $links[] = '<a |
| 340 | - href="' . $dismiss . '" | |
| 350 | + href="' . esc_url( $dismiss ) . '" | |
| 341 | 351 | class="frm-plugin-search__dismiss" |
| 342 | 352 | data-addon="' . esc_attr( $plugin['addon'] ) . '" |
| 343 | 353 | >' . esc_html__( 'Hide this suggestion', 'formidable' ) . '</a>'; |
| 344 | 354 | |
| @@ -346,8 +356,9 @@ | ||
| 346 | 356 | } |
| 347 | 357 | |
| 348 | 358 | /** |
| 349 | 359 | * @param string $plugin |
| 360 | + * | |
| 350 | 361 | * @return bool |
| 351 | 362 | */ |
| 352 | 363 | protected function is_installed( $plugin ) { |
| 353 | 364 | $all_plugins = FrmAppHelper::get_plugins(); |