storefront/inc/admin/class-storefront-plugin-install.php
Rua Haszard 7bd2171866
reinstate phpcs php linting (#1463)
* update squizlabs/php_codesniffer and WP coding standards to latest

* revamp phpcs sniffer rulesets:
- update to php-compatibility (wimg is defunct)
- add version for woocommerce-sniffs

* streamline phpcs config (get it working at all):
- update min WP version to 5.3
- remove references to rules we don't have installed
  - WordPress.VIP
  - WordPress.XSS

* fix 2x phpcs errors (automatically via phpcbf)

* update phps ignore unescaped output comment to current format

* add npm scripts for php linter and linter autofixer

* auto-fix PHP linter errors using phpcbf :

Before phpcbf:

[x] PEAR      Functions           Function call signature multiple arguments          127
[x] Generic   White space         Disallow space indent spaces used                   10
[ ] WordPres  WP                  Enqueued resource parameters missing version        6
[ ] PHPCompa  Language construct  New language constructs t_ns_separator found        4
[ ] WordPres  Security            Escape output output not escaped                    4
[ ] PHPCompa  Parameter values    New HTMLEntities encoding default not set           2
[ ] WordPres  Date time           Restricted functions date_date                      2
[x] Generic   Files               End file newline not found                          1
[x] PEAR      Functions           Function call signature close bracket line          1
[x] PEAR      Functions           Function call signature content after open bracket  1
[x] Squiz     White space         Superfluous whitespace end line                     1
[x] WordPres  Arrays              Comma after array item no comma                     1

phpcbf fixed all [x] violations, all whitespace/formatting

After phpcbf:

WordPres  WP                  Enqueued resource parameters missing version      6
PHPCompa  Language construct  New language constructs t_ns_separator found      4
WordPres  Security            Escape output output not escaped                  4
PHPCompa  Parameter values    New HTMLEntities encoding default not set         2
WordPres  Date time           Restricted functions date_date                    2

Note - this commit does not include auto-fixed files with other
violations. These will follow in separate commit (after fixing!)

* fix phpcs violations:
- numerous formatting issues fixed automatically
- manually fix missing version param in calls to wp_enqueue_style

* fix phpcs violations:
- numerous formatting issues fixed automatically
- fix missing deps param in call to wp_enqueue_style

* update phpcs test php version to match min requirement (5.6)

* fix phpcs violations including some missing escaping:
- numerous formatting issues fixed automatically
- prefer gmdate() over potentially ambiguous date()
- escape output (a real issue!) of comment dates

* fix violations (all automated formatting fixes)

* reinstate WordPress rule/standard in phpcs.xml (minimise PR changes)

* exclude build (pre-zip) ./storefront & tighten excludes for dep folders

* bulk-update Security.EscapeOutput.OutputNotEscaped ignore comment:
- the previous comment format is no longer supported
- bulk replacing these to reduce phpcs warning overhead
2020-09-21 15:50:46 +01:00

151 lines
5 KiB
PHP

<?php
/**
* Storefront Plugin Install Class
*
* @package storefront
* @since 2.2.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'Storefront_Plugin_Install' ) ) :
/**
* The Storefront plugin install class
*/
class Storefront_Plugin_Install {
/**
* Setup class.
*
* @since 1.0
*/
public function __construct() {
add_action( 'admin_enqueue_scripts', array( $this, 'plugin_install_scripts' ) );
}
/**
* Load plugin install scripts
*
* @param string $hook_suffix the current page hook suffix.
* @return void
* @since 1.4.4
*/
public function plugin_install_scripts( $hook_suffix ) {
global $storefront_version;
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
wp_enqueue_script( 'storefront-plugin-install', get_template_directory_uri() . '/assets/js/admin/plugin-install' . $suffix . '.js', array( 'jquery', 'updates' ), $storefront_version, 'all' );
wp_enqueue_style( 'storefront-plugin-install', get_template_directory_uri() . '/assets/css/admin/plugin-install.css', array(), $storefront_version, 'all' );
}
/**
* Output a button that will install or activate a plugin if it doesn't exist, or display a disabled button if the
* plugin is already activated.
*
* @param string $plugin_slug The plugin slug.
* @param string $plugin_file The plugin file.
* @param string $plugin_name The plugin name.
* @param string $classes CSS classes.
* @param string $activated Button activated text.
* @param string $activate Button activate text.
* @param string $install Button install text.
*/
public static function install_plugin_button( $plugin_slug, $plugin_file, $plugin_name, $classes = array(), $activated = '', $activate = '', $install = '' ) {
if ( current_user_can( 'install_plugins' ) && current_user_can( 'activate_plugins' ) ) {
if ( is_plugin_active( $plugin_slug . '/' . $plugin_file ) ) {
// The plugin is already active.
$button = array(
'message' => esc_attr__( 'Activated', 'storefront' ),
'url' => '#',
'classes' => array( 'storefront-button', 'disabled' ),
);
if ( '' !== $activated ) {
$button['message'] = esc_attr( $activated );
}
} elseif ( self::_is_plugin_installed( $plugin_slug ) ) {
$url = self::_is_plugin_installed( $plugin_slug );
// The plugin exists but isn't activated yet.
$button = array(
'message' => esc_attr__( 'Activate', 'storefront' ),
'url' => $url,
'classes' => array( 'activate-now' ),
);
if ( '' !== $activate ) {
$button['message'] = esc_attr( $activate );
}
} else {
// The plugin doesn't exist.
$url = wp_nonce_url(
add_query_arg(
array(
'action' => 'install-plugin',
'plugin' => $plugin_slug,
),
self_admin_url( 'update.php' )
),
'install-plugin_' . $plugin_slug
);
$button = array(
'message' => esc_attr__( 'Install now', 'storefront' ),
'url' => $url,
'classes' => array( 'sf-install-now', 'install-now', 'install-' . $plugin_slug ),
);
if ( '' !== $install ) {
$button['message'] = esc_attr( $install );
}
}
if ( ! empty( $classes ) ) {
$button['classes'] = array_merge( $button['classes'], $classes );
}
$button['classes'] = implode( ' ', $button['classes'] );
?>
<span class="plugin-card-<?php echo esc_attr( $plugin_slug ); ?>">
<a href="<?php echo esc_url( $button['url'] ); ?>" class="<?php echo esc_attr( $button['classes'] ); ?>" data-originaltext="<?php echo esc_attr( $button['message'] ); ?>" data-name="<?php echo esc_attr( $plugin_name ); ?>" data-slug="<?php echo esc_attr( $plugin_slug ); ?>" aria-label="<?php echo esc_attr( $button['message'] ); ?>"><?php echo esc_attr( $button['message'] ); ?></a>
</span> <?php echo /* translators: conjunction of two alternative options user can choose (in missing plugin admin notice). Example: "Activate WooCommerce or learn more" */ esc_html__( 'or', 'storefront' ); ?>
<a href="https://wordpress.org/plugins/<?php echo esc_attr( $plugin_slug ); ?>" target="_blank"><?php esc_attr_e( 'learn more', 'storefront' ); ?></a>
<?php
}
}
/**
* Check if a plugin is installed and return the url to activate it if so.
*
* @param string $plugin_slug The plugin slug.
*/
private static function _is_plugin_installed( $plugin_slug ) {
if ( file_exists( WP_PLUGIN_DIR . '/' . $plugin_slug ) ) {
$plugins = get_plugins( '/' . $plugin_slug );
if ( ! empty( $plugins ) ) {
$keys = array_keys( $plugins );
$plugin_file = $plugin_slug . '/' . $keys[0];
$url = wp_nonce_url(
add_query_arg(
array(
'action' => 'activate',
'plugin' => $plugin_file,
),
admin_url( 'plugins.php' )
),
'activate-plugin_' . $plugin_file
);
return $url;
}
}
return false;
}
}
endif;
return new Storefront_Plugin_Install();