-
Notifications
You must be signed in to change notification settings - Fork 799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent module activation when plan doesn't support them #8983
Changes from 4 commits
8730bf1
43c6662
7477f02
1905189
ff8fce0
9b2712c
5ed8a9f
0850e6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1452,6 +1452,13 @@ public static function refresh_active_plan_from_wpcom() { | |
* @return array Active Jetpack plan details | ||
*/ | ||
public static function get_active_plan() { | ||
global $active_plan_cache; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any better way to do this than introducing another global? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately I couldn't think of one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be clear: We could refactor the whole way that we list out modules, and then we'd only need to call this one - but that requires a lot more time than "hack week" necessarily allows. |
||
|
||
// this can be expensive to compute so we cache for the duration of a request | ||
if ( $active_plan_cache ) { | ||
return $active_plan_cache; | ||
} | ||
|
||
$plan = get_option( 'jetpack_active_plan', array() ); | ||
|
||
// Set the default options | ||
|
@@ -1463,6 +1470,8 @@ public static function get_active_plan() { | |
) ); | ||
} | ||
|
||
$supports = array(); | ||
|
||
// Define what paid modules are supported by personal plans | ||
$personal_plans = array( | ||
'jetpack_personal', | ||
|
@@ -1471,9 +1480,8 @@ public static function get_active_plan() { | |
); | ||
|
||
if ( in_array( $plan['product_slug'], $personal_plans ) ) { | ||
$plan['supports'] = array( | ||
'akismet', | ||
); | ||
// special support value, not a module but a separate plugin | ||
$supports[] = 'akismet'; | ||
$plan['class'] = 'personal'; | ||
} | ||
|
||
|
@@ -1485,12 +1493,8 @@ public static function get_active_plan() { | |
); | ||
|
||
if ( in_array( $plan['product_slug'], $premium_plans ) ) { | ||
$plan['supports'] = array( | ||
'videopress', | ||
'akismet', | ||
'vaultpress', | ||
'wordads', | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you mean to remove videopress and wordads from here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes - they are modules, the others are plugins. Modules are automatically added to the list based on metadata. |
||
$supports[] = 'akismet'; | ||
$supports[] = 'vaultpress'; | ||
$plan['class'] = 'premium'; | ||
} | ||
|
||
|
@@ -1503,23 +1507,23 @@ public static function get_active_plan() { | |
); | ||
|
||
if ( in_array( $plan['product_slug'], $business_plans ) ) { | ||
$plan['supports'] = array( | ||
'videopress', | ||
'akismet', | ||
'vaultpress', | ||
'seo-tools', | ||
'google-analytics', | ||
'wordads', | ||
'search', | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above, did you mean to remove some items from this list? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above - yes ;) |
||
$supports[] = 'akismet'; | ||
$supports[] = 'vaultpress'; | ||
$plan['class'] = 'business'; | ||
} | ||
|
||
// Make sure we have an array here in the event database data is stale | ||
if ( ! isset( $plan['supports'] ) ) { | ||
$plan['supports'] = array(); | ||
// get available features | ||
foreach( self::get_available_modules() as $module_slug ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spacing -- |
||
$module = self::get_module( $module_slug ); | ||
if ( in_array( 'free', $module['plan_classes'] ) || in_array( $plan['class'], $module['plan_classes'] ) ) { | ||
$supports[] = $module_slug; | ||
} | ||
} | ||
|
||
$plan['supports'] = $supports; | ||
|
||
$active_plan_cache = $plan; | ||
|
||
return $plan; | ||
} | ||
|
||
|
@@ -2411,6 +2415,7 @@ public static function get_module( $module ) { | |
'module_tags' => 'Module Tags', | ||
'feature' => 'Feature', | ||
'additional_search_queries' => 'Additional Search Queries', | ||
'plan_classes' => 'Plans', | ||
); | ||
|
||
$file = Jetpack::get_module_path( Jetpack::get_module_slug( $module ) ); | ||
|
@@ -2440,6 +2445,13 @@ public static function get_module( $module ) { | |
$mod['module_tags'] = array( self::translate_module_tag( 'Other' ) ); | ||
} | ||
|
||
if ( $mod['plan_classes'] ) { | ||
$mod['plan_classes'] = explode( ',', $mod['plan_classes'] ); | ||
$mod['plan_classes'] = array_map( 'strtolower', array_map( 'trim', $mod['plan_classes'] ) ); | ||
} else { | ||
$mod['plan_classes'] = array( 'free' ); | ||
} | ||
|
||
if ( $mod['feature'] ) { | ||
$mod['feature'] = explode( ',', $mod['feature'] ); | ||
$mod['feature'] = array_map( 'trim', $mod['feature'] ); | ||
|
@@ -2840,6 +2852,12 @@ public static function activate_module( $module, $exit = true, $redirect = true | |
} | ||
} | ||
|
||
$plan = Jetpack::get_active_plan(); | ||
|
||
if ( ! in_array( $module, $plan['supports'] ) ) { | ||
return false; | ||
} | ||
|
||
// Check the file for fatal errors, a la wp-admin/plugins.php::activate | ||
Jetpack::state( 'module', $module ); | ||
Jetpack::state( 'error', 'module_activation_failed' ); // we'll override this later if the plugin can be included without fatal error | ||
|
@@ -2876,7 +2894,7 @@ public static function activate_module( $module, $exit = true, $redirect = true | |
} | ||
|
||
function activate_module_actions( $module ) { | ||
_deprecated_function( __METHOD__, 'jeptack-4.2' ); | ||
_deprecated_function( __METHOD__, 'jetpack-4.2' ); | ||
} | ||
|
||
public static function deactivate_module( $module ) { | ||
|
@@ -4033,7 +4051,9 @@ function admin_page_load() { | |
$module = stripslashes( $_GET['module'] ); | ||
check_admin_referer( "jetpack_activate-$module" ); | ||
Jetpack::log( 'activate', $module ); | ||
Jetpack::activate_module( $module ); | ||
if ( ! Jetpack::activate_module( $module ) ) { | ||
Jetpack::state( 'error', sprintf( __( 'Could not activated %s', 'jetpack' ), $module ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
} | ||
// The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end. | ||
wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) ); | ||
exit; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed