| @@ -25,25 +25,21 @@ | ||
| 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 | - * | |
| 31 | 29 | * @param object $screen WP Screen object. |
| 32 | 30 | * |
| 33 | - * @return void | |
| 31 | + * @since 4.12 | |
| 34 | 32 | */ |
| 35 | 33 | public function start( $screen ) { |
| 36 | 34 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 37 | - if ( 'plugin-install' !== $screen->base || ( isset( $_GET['paged'] ) && 1 !== intval( $_GET['paged'] ) ) ) { | |
| 38 | - return; | |
| 35 | + if ( 'plugin-install' === $screen->base && ( ! isset( $_GET['paged'] ) || 1 === intval( $_GET['paged'] ) ) ) { | |
| 36 | + add_filter( 'plugins_api_result', array( $this, 'inject_suggestion' ), 10, 3 ); | |
| 37 | + add_filter( 'self_admin_url', array( $this, 'plugin_details' ) ); | |
| 38 | + add_filter( 'plugin_install_action_links', array( $this, 'insert_related_links' ), 10, 2 ); | |
| 39 | + add_action( 'admin_enqueue_scripts', array( $this, 'load_plugins_search_script' ) ); | |
| 40 | + $this->maybe_dismiss(); | |
| 39 | 41 | } |
| 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(); | |
| 46 | 42 | } |
| 47 | 43 | |
| 48 | 44 | /** |
| 49 | 45 | * Intercept the plugins API response and add in an appropriate card. |
| @@ -50,10 +46,8 @@ | ||
| 50 | 46 | * |
| 51 | 47 | * @param object $result Plugin search results. |
| 52 | 48 | * @param string $action unused. |
| 53 | 49 | * @param object $args Search args. |
| 54 | - * | |
| 55 | - * @return object | |
| 56 | 50 | */ |
| 57 | 51 | public function inject_suggestion( $result, $action, $args ) { |
| 58 | 52 | // Looks like a search query; it's matching time. |
| 59 | 53 | if ( empty( $args->search ) ) { |
| @@ -59,19 +53,13 @@ | ||
| 59 | 53 | if ( empty( $args->search ) ) { |
| 60 | 54 | return $result; |
| 61 | 55 | } |
| 62 | 56 | |
| 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 | - | |
| 68 | 57 | $addon_list = $this->get_addons(); |
| 69 | 58 | |
| 70 | 59 | // Lowercase, trim, remove punctuation/special chars, decode url, remove 'formidable'. |
| 71 | 60 | $normalized_term = $this->search_to_array( $args->search ); |
| 72 | - | |
| 73 | - if ( ! $normalized_term ) { | |
| 61 | + if ( empty( $normalized_term ) ) { | |
| 74 | 62 | // Don't add anything extra. |
| 75 | 63 | return $result; |
| 76 | 64 | } |
| 77 | 65 | |
| @@ -76,31 +64,26 @@ | ||
| 76 | 64 | } |
| 77 | 65 | |
| 78 | 66 | $matching_addon = $this->matching_addon( $addon_list, $normalized_term ); |
| 79 | 67 | |
| 80 | - if ( ! $matching_addon || ! $this->should_display_hint( $matching_addon ) ) { | |
| 68 | + if ( empty( $matching_addon ) || ! $this->should_display_hint( $matching_addon ) ) { | |
| 81 | 69 | return $result; |
| 82 | 70 | } |
| 83 | 71 | |
| 84 | 72 | $inject = (array) $this->get_plugin_data(); |
| 85 | 73 | $overrides = array( |
| 86 | - // Helps to determine if that an injected card. | |
| 87 | - 'plugin-search' => true, | |
| 88 | - 'name' => sprintf( | |
| 74 | + 'plugin-search' => true, // Helps to determine if that an injected card. | |
| 75 | + 'name' => sprintf( | |
| 89 | 76 | /* translators: Formidable addon name */ |
| 90 | 77 | esc_html_x( 'Formidable %s', 'Formidable Addon Name', 'formidable' ), |
| 91 | 78 | $addon_list[ $matching_addon ]['name'] |
| 92 | 79 | ), |
| 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'], | |
| 80 | + 'addon' => $addon_list[ $matching_addon ]['slug'], | |
| 81 | + 'short_description' => $addon_list[ $matching_addon ]['excerpt'], | |
| 82 | + 'slug' => self::$slug, | |
| 83 | + 'version' => $addon_list[ $matching_addon ]['version'], | |
| 97 | 84 | ); |
| 98 | 85 | |
| 99 | - if ( ! empty( $addon_list[ $matching_addon ]['external'] ) ) { | |
| 100 | - unset( $overrides['name'] ); | |
| 101 | - } | |
| 102 | - | |
| 103 | 86 | // Splice in the base addon data. |
| 104 | 87 | $inject = array_merge( $inject, $addon_list[ $matching_addon ], $overrides ); |
| 105 | 88 | |
| 106 | 89 | // Add it to the top of the list. |
| @@ -113,12 +96,9 @@ | ||
| 113 | 96 | * Search for any addons that match the searched terms. |
| 114 | 97 | * |
| 115 | 98 | * @since 4.12 |
| 116 | 99 | * |
| 117 | - * @param array $addon_list | |
| 118 | - * @param array $normalized_term | |
| 119 | - * | |
| 120 | - * @return int|null | |
| 100 | + * @return int | |
| 121 | 101 | */ |
| 122 | 102 | private function matching_addon( $addon_list, $normalized_term ) { |
| 123 | 103 | $matching_addon = null; |
| 124 | 104 | |
| @@ -127,16 +107,22 @@ | ||
| 127 | 107 | if ( ! is_array( $addon_opts ) || empty( $addon_opts['excerpt'] ) ) { |
| 128 | 108 | continue; |
| 129 | 109 | } |
| 130 | 110 | |
| 111 | + /* | |
| 112 | + * Does the site's current plan support the feature? | |
| 113 | + */ | |
| 114 | + $is_supported_by_plan = ! empty( $addon_opts['url'] ); | |
| 115 | + | |
| 131 | 116 | if ( ! isset( $addon_opts['search_terms'] ) ) { |
| 132 | 117 | $addon_opts['search_terms'] = ''; |
| 133 | 118 | } |
| 134 | 119 | |
| 135 | - $addon_terms = $this->search_to_array( $addon_opts['search_terms'] . ' ' . $addon_opts['name'] ); | |
| 136 | - $matched_terms = array_intersect( $addon_terms, $normalized_term ); | |
| 120 | + $addon_terms = $this->search_to_array( $addon_opts['search_terms'] . ', ' . $addon_opts['name'] ); | |
| 137 | 121 | |
| 138 | - if ( count( $matched_terms ) === count( $normalized_term ) ) { | |
| 122 | + $matches = ! empty( array_intersect( $addon_terms, $normalized_term ) ); | |
| 123 | + | |
| 124 | + if ( $matches && $is_supported_by_plan ) { | |
| 139 | 125 | $matching_addon = $addon_id; |
| 140 | 126 | break; |
| 141 | 127 | } |
| 142 | 128 | } |
| @@ -150,10 +136,9 @@ | ||
| 150 | 136 | * @return array |
| 151 | 137 | */ |
| 152 | 138 | private function get_addons() { |
| 153 | 139 | $api = new FrmFormApi(); |
| 154 | - $addons = $api->get_api_info(); | |
| 155 | - return apply_filters( 'frm_plugin_search', $addons ); | |
| 140 | + return $api->get_api_info(); | |
| 156 | 141 | } |
| 157 | 142 | |
| 158 | 143 | /** |
| 159 | 144 | * Get the plugin repo's data to populate the fields with. |
| @@ -189,31 +174,26 @@ | ||
| 189 | 174 | |
| 190 | 175 | /** |
| 191 | 176 | * Modify URL used to fetch to plugin information so it pulls Formidable plugin page. |
| 192 | 177 | * |
| 178 | + * @param string $url URL to load in dialog pulling the plugin page from wporg. | |
| 179 | + * | |
| 193 | 180 | * @since 4.12 |
| 194 | 181 | * |
| 195 | - * @param string $url URL to load in dialog pulling the plugin page from wporg. | |
| 196 | - * | |
| 197 | 182 | * @return string The URL with 'formidable' instead of 'frm-plugin-search'. |
| 198 | 183 | */ |
| 199 | 184 | public function plugin_details( $url ) { |
| 200 | - // phpcs:disable Generic.WhiteSpace.ScopeIndent | |
| 201 | 185 | return false !== stripos( $url, 'tab=plugin-information&plugin=' . self::$slug ) |
| 202 | 186 | ? 'plugin-install.php?tab=plugin-information&plugin=formidable&TB_iframe=true&width=600&height=550' |
| 203 | 187 | : $url; |
| 204 | - // phpcs:enable Generic.WhiteSpace.ScopeIndent | |
| 205 | 188 | } |
| 206 | 189 | |
| 207 | 190 | /** |
| 208 | 191 | * @since 4.12 |
| 209 | - * | |
| 210 | - * @return void | |
| 211 | 192 | */ |
| 212 | 193 | private function maybe_dismiss() { |
| 213 | 194 | $addon = FrmAppHelper::get_param( 'frm-dismiss', '', 'get', 'absint' ); |
| 214 | - | |
| 215 | - if ( $addon ) { | |
| 195 | + if ( ! empty( $addon ) ) { | |
| 216 | 196 | $this->add_to_dismissed_hints( $addon ); |
| 217 | 197 | } |
| 218 | 198 | } |
| 219 | 199 | |
| @@ -225,9 +205,9 @@ | ||
| 225 | 205 | * @return array List of dismissed hints. |
| 226 | 206 | */ |
| 227 | 207 | protected function get_dismissed_hints() { |
| 228 | 208 | $dismissed_hints = get_option( self::$dismissed_opt ); |
| 229 | - return $dismissed_hints && is_array( $dismissed_hints ) ? $dismissed_hints : array(); | |
| 209 | + return ! empty( $dismissed_hints ) && is_array( $dismissed_hints ) ? $dismissed_hints : array(); | |
| 230 | 210 | } |
| 231 | 211 | |
| 232 | 212 | /** |
| 233 | 213 | * Save the hint in the list of dismissed hints. |
| @@ -239,9 +219,9 @@ | ||
| 239 | 219 | * @return bool Whether the card was added to the list and hence dismissed. |
| 240 | 220 | */ |
| 241 | 221 | protected function add_to_dismissed_hints( $hint ) { |
| 242 | 222 | $hints = array_merge( $this->get_dismissed_hints(), array( $hint ) ); |
| 243 | - return update_option( self::$dismissed_opt, $hints, false ); | |
| 223 | + return update_option( self::$dismissed_opt, $hints, 'no' ); | |
| 244 | 224 | } |
| 245 | 225 | |
| 246 | 226 | /** |
| 247 | 227 | * Checks that the addon slug passed should be displayed. |
| @@ -269,31 +249,30 @@ | ||
| 269 | 249 | * Take a raw search query and return something a bit more standardized and |
| 270 | 250 | * easy to work with. |
| 271 | 251 | * |
| 272 | 252 | * @param string $term The raw search term. |
| 273 | - * | |
| 274 | 253 | * @return string A simplified/sanitized version. |
| 275 | 254 | */ |
| 276 | 255 | private function sanitize_search_term( $term ) { |
| 277 | 256 | $term = strtolower( urldecode( $term ) ); |
| 278 | 257 | |
| 279 | - // Remove non-alpha/space chars. | |
| 258 | + // remove non-alpha/space chars. | |
| 280 | 259 | $term = preg_replace( '/[^a-z ]/', '', $term ); |
| 281 | 260 | |
| 282 | - // Remove strings that don't help matches. | |
| 283 | - return trim( str_replace( array( 'formidable', 'free', 'wordpress', 'wp ', 'plugin' ), '', $term ) ); | |
| 261 | + // remove strings that don't help matches. | |
| 262 | + $term = trim( str_replace( array( 'formidable', 'free', 'wordpress', 'wp ', 'plugin' ), '', $term ) ); | |
| 263 | + | |
| 264 | + return $term; | |
| 284 | 265 | } |
| 285 | 266 | |
| 286 | 267 | /** |
| 287 | 268 | * @since 4.12 |
| 288 | 269 | * |
| 289 | - * @param string $terms | |
| 290 | - * | |
| 291 | 270 | * @return array |
| 292 | 271 | */ |
| 293 | 272 | private function search_to_array( $terms ) { |
| 294 | 273 | $terms = $this->sanitize_search_term( $terms ); |
| 295 | - return array_filter( explode( ' ', $terms ) ); | |
| 274 | + return array_filter( explode( ',', $terms ) ); | |
| 296 | 275 | } |
| 297 | 276 | |
| 298 | 277 | /** |
| 299 | 278 | * Put some more appropriate links on our custom result cards. |
| @@ -299,10 +278,8 @@ | ||
| 299 | 278 | * Put some more appropriate links on our custom result cards. |
| 300 | 279 | * |
| 301 | 280 | * @param array $links Related links. |
| 302 | 281 | * @param array $plugin Plugin result information. |
| 303 | - * | |
| 304 | - * @return array | |
| 305 | 282 | */ |
| 306 | 283 | public function insert_related_links( $links, $plugin ) { |
| 307 | 284 | if ( self::$slug !== $plugin['slug'] ) { |
| 308 | 285 | return $links; |
| @@ -310,24 +287,25 @@ | ||
| 310 | 287 | |
| 311 | 288 | // By the time this filter is applied, self_admin_url was already applied and we don't need it anymore. |
| 312 | 289 | remove_filter( 'self_admin_url', array( $this, 'plugin_details' ) ); |
| 313 | 290 | |
| 314 | - $links = array(); | |
| 291 | + $links = array(); | |
| 315 | 292 | $is_installed = $this->is_installed( $plugin['plugin'] ); |
| 316 | 293 | $is_active = is_plugin_active( $plugin['plugin'] ); |
| 294 | + $has_access = ! empty( $plugin['url'] ); | |
| 317 | 295 | |
| 318 | 296 | // Plugin installed, active, feature not enabled; prompt to enable. |
| 319 | 297 | if ( ! $is_active && $is_installed ) { |
| 320 | 298 | if ( current_user_can( 'activate_plugins' ) ) { |
| 321 | - $activate_url = add_query_arg( | |
| 299 | + $activate_url = add_query_arg( | |
| 322 | 300 | array( |
| 323 | - 'action' => 'activate', | |
| 301 | + 'action' => 'activate', | |
| 324 | 302 | '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $plugin['plugin'] ), |
| 325 | 303 | 'plugin' => $plugin['plugin'], |
| 326 | 304 | ), |
| 327 | 305 | admin_url( 'plugins.php' ) |
| 328 | 306 | ); |
| 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 | |
| 307 | + $links['frm_get_started'] = '<a href="' . esc_url( $activate_url ) . '" class="button activate-now" aria-label="Activate ' . esc_attr( $plugin['name'] ) . '">' . __( 'Activate', 'formidable' ) . '</a>'; | |
| 330 | 308 | } |
| 331 | 309 | } elseif ( ! $is_active && isset( $plugin['url'] ) ) { |
| 332 | 310 | // Go to the add-ons page to install. |
| 333 | 311 | $links[] = '<a |
| @@ -332,9 +310,10 @@ | ||
| 332 | 310 | // Go to the add-ons page to install. |
| 333 | 311 | $links[] = '<a |
| 334 | 312 | class="button-secondary" |
| 335 | 313 | href="' . esc_url( admin_url( 'admin.php?page=formidable-addons' ) ) . '" |
| 336 | - >' . esc_html__( 'Install Now', 'formidable' ) . '</a>'; | |
| 314 | + >' . __( 'Install Now', 'formidable' ) . '</a>'; | |
| 315 | + | |
| 337 | 316 | } elseif ( ! empty( $plugin['link'] ) ) { |
| 338 | 317 | // Add link pointing to a relevant doc page in formidable.com. |
| 339 | 318 | $links[] = '<a |
| 340 | 319 | class="button-primary frm-plugin-search__learn-more" |
| @@ -341,14 +320,14 @@ | ||
| 341 | 320 | href="' . esc_url( FrmAppHelper::admin_upgrade_link( 'plugin-learn-more', $plugin['link'] ) ) . '" |
| 342 | 321 | target="_blank" |
| 343 | 322 | data-addon="' . esc_attr( $plugin['addon'] ) . '" |
| 344 | 323 | >' . esc_html__( 'Learn more', 'formidable' ) . '</a>'; |
| 345 | - }//end if | |
| 324 | + } | |
| 346 | 325 | |
| 347 | 326 | // Dismiss link. |
| 348 | 327 | $dismiss = add_query_arg( array( 'frm-dismiss' => $plugin['id'] ) ); |
| 349 | 328 | $links[] = '<a |
| 350 | - href="' . esc_url( $dismiss ) . '" | |
| 329 | + href="' . $dismiss . '" | |
| 351 | 330 | class="frm-plugin-search__dismiss" |
| 352 | 331 | data-addon="' . esc_attr( $plugin['addon'] ) . '" |
| 353 | 332 | >' . esc_html__( 'Hide this suggestion', 'formidable' ) . '</a>'; |
| 354 | 333 | |
| @@ -354,22 +333,20 @@ | ||
| 354 | 333 | |
| 355 | 334 | return $links; |
| 356 | 335 | } |
| 357 | 336 | |
| 358 | - /** | |
| 359 | - * @param string $plugin | |
| 360 | - * | |
| 361 | - * @return bool | |
| 362 | - */ | |
| 363 | 337 | protected function is_installed( $plugin ) { |
| 364 | - $all_plugins = FrmAppHelper::get_plugins(); | |
| 338 | + if ( ! function_exists( 'get_plugins' ) ) { | |
| 339 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 340 | + } | |
| 341 | + | |
| 342 | + $all_plugins = get_plugins(); | |
| 343 | + | |
| 365 | 344 | return isset( $all_plugins[ $plugin ] ); |
| 366 | 345 | } |
| 367 | 346 | |
| 368 | 347 | /** |
| 369 | 348 | * Load the search scripts and CSS for PSH. |
| 370 | - * | |
| 371 | - * @return void | |
| 372 | 349 | */ |
| 373 | 350 | public function load_plugins_search_script() { |
| 374 | 351 | wp_enqueue_script( self::$slug, FrmAppHelper::plugin_url() . '/js/plugin-search.js', array(), FrmAppHelper::plugin_version(), true ); |
| 375 | 352 | wp_localize_script( |