e = sprintf( /* Translators: %1$s - opening link tag, %2$s - closing link tag. */ __( 'There was an error and plugin could not be installed, %1$splease install manually%2$s.', 'tribe-common' ), '', '' ); /** * Filters the error message for a plugin install. * * @since 1.0.0 * * @param string $message The error message. * @param string $slug The plugin slug. * @param Plugin $plugin The plugin handler. */ return apply_filters( "stellarwp/installer/{$hook_prefix}/install_error_message", $message, $this->slug, $this ); } /** * @inheritDoc */ public function get_js_action(): string { return $this->js_action; } /** * @inheritDoc */ public function get_name(): string { return $this->name; } /** * Gets the resource permission. * * @since 1.0.0 * * @return string */ protected function get_permission(): string { $hook_prefix = Config::get_hook_prefix(); /** * Filters the permission for installing the resource. * * @since 1.0.0 * * @param string|null $permission The permission. * @param string $slug The resource slug. * @param Handler $handler The installer object. */ return apply_filters( "stellarwp/installer/{$hook_prefix}/get_permission", $this->permission, $this->slug, $this ); } /** * Tests to see if the requested variable is set either as a post field or as a URL * param and returns the value if so. * * Post data takes priority over fields passed in the URL query. If the field is not * set then $default (null unless a different value is specified) will be returned. * * The variable being tested for can be an array if you wish to find a nested value. * * @since 1.0.0 * * @param string|array $var * @param mixed $default * * @return mixed */ protected function get_request_var( $var, $default = null ) { $requests = []; // Prevent a slew of warnings every time we call this. $requests[] = $_REQUEST; $requests[] = $_GET; $requests[] = $_POST; $unsafe = Array_Utils::get_in_any( $requests, $var, $default ); return Array_Utils::sanitize_deep( $unsafe ); } /** * @inheritDoc */ public function get_slug(): string { return $this->slug; } /** * @inheritDoc */ public function get_wordpress_org_data() { if ( $this->wordpress_org_data ) { return $this->wordpress_org_data; } if ( ! function_exists( 'plugins_api' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; } $hook_prefix = Config::get_hook_prefix(); $api_results = plugins_api( 'plugin_information', [ 'slug' => $this->slug, 'fields' => [ 'short_description' => false, 'sections' => false, 'requires' => false, 'rating' => false, 'ratings' => false, 'downloaded' => false, 'last_updated' => false, 'added' => false, 'tags' => false, 'compatibility' => false, 'homepage' => false, 'donate_link' => false, ], ] ); /** * Filters the WordPress.org data for a plugin. * * @since 1.0.0 * * @param object|WP_Error $api_results The WordPress.org data. * @param string $slug The plugin slug. * @param Plugin $plugin The plugin handler. */ $api_results = apply_filters( "stellarwp/installer/{$hook_prefix}/wordpress_org_data", $api_results, $this->slug, $this ); $this->wordpress_org_data = $api_results; return $this->wordpress_org_data; } /** * @inheritDoc */ public function handle_request() { $installer = Installer::get(); if ( ! check_ajax_referer( $installer->get_nonce_name(), 'nonce', false ) ) { $response['message'] = wpautop( __( 'Insecure request.', 'tribe-common' ) ); wp_send_json_error( $response ); } if ( ! current_user_can( $this->get_permission() ) ) { wp_send_json_error( [ 'message' => wpautop( sprintf( __( 'Security Error, Need higher permissions to install %1$s.' , 'tribe-common' ), $this->name ) ) ] ); } $vars = [ 'request' => $this->get_request_var( 'request' ), ]; $success = false; if ( 'install' === $vars['request'] ) { $success = $this->install(); } elseif ( 'activate' === $vars['request'] ) { $success = $this->activate(); } if ( false === $success ) { wp_send_json_error( [ 'message' => wpautop( $this->get_error_message() ) ] ); } else { wp_send_json_success( [ 'message' => __( 'Success.', 'tribe-common' ) ] ); } } /** * @inheritDoc */ public function install(): bool { if ( ! class_exists( 'WP_Upgrader' ) ) { require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; } $url = $this->get_download_url(); if ( ! is_wp_error( $this->wordpress_org_data ) ) { $upgrader = new \Plugin_Upgrader( new \WP_Ajax_Upgrader_Skin() ); $installed = $upgrader->install( $url ); if ( $installed ) { $activate = activate_plugin( $this->get_basename(), '', false, true ); $success = ! is_wp_error( $activate ); } else { $success = false; } } else { $success = false; } return $success; } /** * @inheritDoc */ public function is_active(): bool { if ( $this->is_active === null ) { $did_action = false; if ( isset( $this->did_action ) ) { $did_action = did_action( $this->did_action ); } $this->is_active = is_plugin_active( $this->get_basename() ) || is_plugin_active_for_network( $this->get_basename() ) || $did_action; } return $this->is_active; } /** * Checks if the plugin is installed. * * @since 1.0.0 * * @return boolean True if active */ public function is_installed(): bool { if ( $this->is_installed === null ) { $this->is_installed = false; $installed_plugins = get_plugins(); foreach ( $installed_plugins as $file => $plugin ) { if ( $plugin['Name'] === $this->name ) { $this->basename = $file; $this->is_installed = true; break; } } } return $this->is_installed; } }