Skip to content

Commit

Permalink
Hotfix: WP Rocket in the Install Plugins page:
Browse files Browse the repository at this point in the history
- Introduced `IMAGIFY_INT_MAX`.
- Make sure to remove the "Install Now" button in the Rocket item.
- Make sure Rocket is displayed in first place in the Featured tab.
- Fixed a translators comment.
  • Loading branch information
Screenfeed committed Aug 11, 2017
1 parent 2c75138 commit 477ed34
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
1 change: 1 addition & 0 deletions imagify.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
define( 'IMAGIFY_ASSETS_CSS_URL' , IMAGIFY_ASSETS_URL . 'css/' );
define( 'IMAGIFY_ASSETS_IMG_URL' , IMAGIFY_ASSETS_URL . 'images/' );
define( 'IMAGIFY_MAX_BYTES' , 5242880 );
define( 'IMAGIFY_INT_MAX' , PHP_INT_MAX - 30 );

add_action( 'plugins_loaded', '_imagify_init' );
/**
Expand Down
66 changes: 55 additions & 11 deletions inc/classes/class-imagify-rocket-infos.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,14 @@ public function init() {
global $wp_version;

// Filter the plugin API results to inject WP Rocket.
add_filter( 'plugins_api_result', array( $this, 'add_api_result' ), 11, 3 );
add_filter( 'plugins_api_result', array( $this, 'add_api_result' ), IMAGIFY_INT_MAX, 3 );

// Change order for the featured tab.
if ( is_network_admin() ) {
add_filter( 'views_plugin-install-network', array( $this, 'change_featured_items_order' ), IMAGIFY_INT_MAX );
} else {
add_filter( 'views_plugin-install', array( $this, 'change_featured_items_order' ), IMAGIFY_INT_MAX );
}

// Filter the iframe src to return WP Rocket's site URL (More Details popup).
if ( version_compare( $wp_version, '4.9' ) >= 0 ) {
Expand All @@ -142,10 +149,9 @@ public function init() {
* @since 1.6.9
* @author Grégory Viguier
*
* @param object $result Response object or WP_Error object.
* @param string $action The type of information being requested from the Plugin Install API.
* @param object $args Plugin API arguments.
*
* @param object $result Response object or WP_Error object.
* @param string $action The type of information being requested from the Plugin Install API.
* @param object $args Plugin API arguments.
* @return array|object Updated array of results or WP_Error object.
*/
public function add_api_result( $result, $action, $args ) {
Expand All @@ -171,11 +177,7 @@ public function add_api_result( $result, $action, $args ) {
return $result;
}

if ( 'featured' === $args->browse ) {
array_push( $result->plugins, $plugin_info );
} else {
array_unshift( $result->plugins, $plugin_info );
}
array_unshift( $result->plugins, $plugin_info );

if ( isset( $result->info['results'] ) ) {
++$result->info['results'];
Expand All @@ -184,6 +186,37 @@ public function add_api_result( $result, $action, $args ) {
return $result;
}

/**
* Put WP Rocket back at the first place in the Featured tab.
* In the Featured tab, WP uses `uasort()` to reorder the items. But the order is made on a non-existing property (group), so as a result, the items are ramdomized.
*
* @access public
* @since 1.6.9
* @author Grégory Viguier
*
* @param array $views An array of available list table views.
* @return array The same array.
*/
function change_featured_items_order( $views ) {
global $wp_list_table;

if ( 'group' !== $wp_list_table->orderby ) {
return $views;
}

foreach ( $wp_list_table->items as $i => $item ) {
$item = (object) $item;

if ( 'wp-rocket' === $item->slug ) {
unset( $wp_list_table->items[ $i ] );
array_unshift( $wp_list_table->items, $item );
break;
}
}

return $views;
}

/**
* Filter the iframe src to return WP Rocket's site URL (More Details popup).
*
Expand Down Expand Up @@ -238,6 +271,17 @@ public function add_action_link( $links, $plugin ) {
return $links;
}

// Remove the "Install Now" button.
if ( $links ) {
foreach ( $links as $i => $link ) {
if ( strpos( $link, 'install-now' ) !== false ) {
unset( $links[ $i ] );
break;
}
}
}

// Add the "Buy Now" button.
$link = '<a class="button" target="_blank" data-slug="wp-rocket" href="%s" aria-label="%s" data-name="WP Rocket">%s</a>';
$url = imagify_get_wp_rocket_url( false, array(
'utm_source' => 'wpaddplugins',
Expand Down Expand Up @@ -362,7 +406,7 @@ protected function translate_informations() {
return self::$plugin_information;
}

/* translators: %s is a plugin version. */
/* translators: %s is a WordPress version. */
self::$plugin_information->requires = preg_replace( '@([\d.]+) or higher@', sprintf( __( '%s or higher', 'imagify' ), '$1' ), self::$plugin_information->requires );
self::$plugin_information->requires = str_replace( 'Requires PHP:', __( 'Requires PHP:', 'imagify' ), self::$plugin_information->requires );
self::$plugin_information->homepage = imagify_get_wp_rocket_url( false, array(
Expand Down

0 comments on commit 477ed34

Please sign in to comment.