e // This is because if a standalone plugin is installed, we expect the product to not show as "inactive" on My Jetpack if ( static::$requires_plan || ( ! static::has_any_plan_for_product() && static::$has_standalone_plugin && ! self::is_plugin_active() ) ) { $status = static::$has_free_offering ? 'needs_purchase_or_free' : 'needs_purchase'; } elseif ( static::$requires_site_connection && ! ( new Connection_Manager() )->is_connected() ) { // Site has never been connected before if ( ! \Jetpack_Options::get_option( 'id' ) ) { $status = 'needs_first_site_connection'; } else { $status = 'site_connection_error'; } } elseif ( static::$requires_user_connection && ! ( new Connection_Manager() )->has_connected_owner() ) { $status = 'user_connection_error'; } } elseif ( ! static::has_any_plan_for_product() ) { $status = static::$has_free_offering ? 'needs_purchase_or_free' : 'needs_purchase'; } else { $status = 'inactive'; } return $status; } /** * Checks whether the Product is active * * @return boolean */ public static function is_active() { return static::is_plugin_active() && ( static::has_any_plan_for_product() || ( ! static::$requires_plan && static::$has_free_offering ) ); } /** * Checks whether the plugin is installed * * @return boolean */ public static function is_plugin_installed() { return (bool) static::get_installed_plugin_filename(); } /** * Checks whether the plugin is active * * @return boolean */ public static function is_plugin_active() { return Plugins_Installer::is_plugin_active( static::get_installed_plugin_filename() ); } /** * Checks whether the Jetpack plugin is installed * * @return boolean */ public static function is_jetpack_plugin_installed() { return (bool) static::get_installed_plugin_filename( 'jetpack' ); } /** * Checks whether the Jetpack plugin is active * * @return boolean */ public static function is_jetpack_plugin_active() { return Plugins_Installer::is_plugin_active( static::get_installed_plugin_filename( 'jetpack' ) ); } /** * Checks whether the Jetpack module is active only if a module_name is defined * * @return bool */ public static function is_module_active() { if ( static::$module_name ) { return ( new Modules() )->is_active( static::$module_name ); } return true; } /** * Activates the plugin * * @return null|WP_Error Null on success, WP_Error on invalid file. */ public static function activate_plugin() { return activate_plugin( static::get_installed_plugin_filename() ); } /** * Perform the top level activation routines, which is installing and activating the required plugin * * @return bool|WP_Error */ private static function do_activation() { if ( static::is_active() ) { return true; } // Default to installing the standalone plugin for the product if ( ! self::is_plugin_installed() ) { $installed = Plugins_Installer::install_plugin( static::get_plugin_slug() ); if ( is_wp_error( $installed ) ) { return $installed; } } if ( ! current_user_can( 'activate_plugins' ) ) { return new WP_Error( 'not_allowed', __( 'You are not allowed to activate plugins on this site.', 'jetpack-my-jetpack' ) ); } $result = static::activate_plugin(); if ( is_wp_error( $result ) ) { return $result; } return true; } /** * Activates the product by installing and activating its plugin * * @return boolean|WP_Error */ final public static function activate() { $result = self::do_activation(); $result = static::do_product_specific_activation( $result ); $product_slug = static::$slug; /** * Fires after My Jetpack activates a product and filters the result * Use this filter to run additional routines for a product activation on stand-alone plugins * * @param bool|WP_Error $result The result of the previous steps of activation. */ $result = apply_filters( "my_jetpack_{$product_slug}_activation", $result ); return $result; } /** * Override this method to perform product specific activation routines. * * @param bool|WP_Error $current_result Is the result of the top level activation actions. You probably won't do anything if it is an WP_Error. * @return bool|WP_Error */ public static function do_product_specific_activation( $current_result ) { return $current_result; } /** * Deactivate the product * * @return boolean */ public static function deactivate() { deactivate_plugins( static::get_installed_plugin_filename() ); return true; } /** * Returns filtered Jetpack plugin actions links. * * @param array $actions - Jetpack plugin action links. * @return array Filtered Jetpack plugin actions links. */ public static function get_plugin_actions_links( $actions ) { // My Jetpack action link. $my_jetpack_home_link = array( 'jetpack-home' => sprintf( '%2$s', admin_url( 'admin.php?page=my-jetpack' ), __( 'My Jetpack', 'jetpack-my-jetpack' ), __( 'My Jetpack dashboard', 'jetpack-my-jetpack' ) ), ); // Otherwise, add it to the beginning of the array. return array_merge( $my_jetpack_home_link, $actions ); } /** * Filter the action links for the plugins specified. * * @param string|string[] $filenames The plugin filename(s) to filter the action links for. */ private static function filter_action_links( $filenames ) { foreach ( $filenames as $filename ) { $hook = 'plugin_action_links_' . $filename; $callback = array( static::class, 'get_plugin_actions_links' ); if ( ! has_filter( $hook, $callback ) ) { add_filter( $hook, $callback, 20, 2 ); } } } /** * Extend the plugin action links. */ public static function extend_plugin_action_links() { $filenames = static::get_plugin_filename(); if ( ! is_array( $filenames ) ) { $filenames = array( $filenames ); } self::filter_action_links( $filenames ); } /** * Extend the Jetpack plugin action links. */ public static function extend_core_plugin_action_links() { $filenames = self::JETPACK_PLUGIN_FILENAME; self::filter_action_links( $filenames ); } /** * Install and activate the standalone plugin in the case it's missing. * * @return boolean|WP_Error */ public static function install_and_activate_standalone() { /** * Check for the presence of the standalone plugin, ignoring Jetpack presence. * * If the standalone plugin is not installed and the user can install plugins, proceed with the installation. */ if ( ! static::is_plugin_installed() ) { /** * Check for permissions */ if ( ! current_user_can( 'install_plugins' ) ) { return new WP_Error( 'not_allowed', __( 'You are not allowed to install plugins on this site.', 'jetpack-my-jetpack' ) ); } /** * Install the plugin */ $installed = Plugins_Installer::install_plugin( static::get_plugin_slug() ); if ( is_wp_error( $installed ) ) { return $installed; } } /** * Activate the installed plugin */ $result = static::activate_plugin(); if ( is_wp_error( $result ) ) { return $result; } return true; } }