|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Plugin Name: AMP App Shell for WordPress |
| 4 | + * Description: Support for app shell navigation via AMP Shadow DOM. |
| 5 | + * Plugin URI: https://github.com/xwp/amp-app-shell/ |
| 6 | + * Author: AMP Project Contributors, XWP |
| 7 | + * Author URI: https://github.com/xwp/amp-app-shell/graphs/contributors |
| 8 | + * Version: 0.1.0 |
| 9 | + * Text Domain: amp-app-shell |
| 10 | + * Domain Path: /languages/ |
| 11 | + * License: GPLv2 or later |
| 12 | + * Requires at least: 4.9 |
| 13 | + * Requires PHP: 5.6 |
| 14 | + * |
| 15 | + * @package AmpAppShell |
| 16 | + */ |
| 17 | + |
| 18 | +define( 'AMP_APP_SHELL__FILE__', __FILE__ ); |
| 19 | +define( 'AMP_APP_SHELL__DIR__', dirname( __FILE__ ) ); |
| 20 | +define( 'AMP_APP_SHELL__VERSION', '0.1.0' ); |
| 21 | + |
| 22 | +/** |
| 23 | + * Errors encountered while loading the plugin. |
| 24 | + * |
| 25 | + * This has to be a global for the sake of PHP 5.2. |
| 26 | + * |
| 27 | + * @var WP_Error $_amp_app_shell_load_errors |
| 28 | + */ |
| 29 | +global $_amp_app_shell_load_errors; |
| 30 | + |
| 31 | +$_amp_app_shell_load_errors = new WP_Error(); |
| 32 | + |
| 33 | +if ( version_compare( phpversion(), '5.6', '<' ) ) { |
| 34 | + $_amp_app_shell_load_errors->add( |
| 35 | + 'insufficient_php_version', |
| 36 | + sprintf( |
| 37 | + /* translators: %s: required PHP version */ |
| 38 | + __( 'The AMP App Shell plugin requires PHP %s. Please contact your host to update your PHP version.', 'amp-app-shell' ), |
| 39 | + '5.6+' |
| 40 | + ) |
| 41 | + ); |
| 42 | +} |
| 43 | + |
| 44 | +if ( ! file_exists( AMP_APP_SHELL__DIR__ . '/assets/js/amp-wp-app-shell.js' ) ) { |
| 45 | + $_amp_app_shell_load_errors->add( |
| 46 | + 'build_required', |
| 47 | + sprintf( |
| 48 | + /* translators: %s: composer install && npm install && npm run build */ |
| 49 | + __( 'You appear to be running the AMP App Shell plugin from source. Please do %s to finish installation.', 'amp-app-shell' ), // phpcs:ignore WordPress.Security.EscapeOutput |
| 50 | + '<code>composer install && npm install && npm run build</code>' |
| 51 | + ) |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +function _amp_app_shell_check_dependecies() { |
| 56 | + global $_amp_app_shell_load_errors; |
| 57 | + |
| 58 | + if ( ! is_plugin_active( 'amp/amp.php' ) ) { |
| 59 | + $_amp_app_shell_load_errors->add( |
| 60 | + 'amp_plugin_required', |
| 61 | + sprintf( |
| 62 | + /* translators: %s: <a href="https://wordpress.org/plugins/amp/" target="_blank">AMP</a> */ |
| 63 | + __( 'It seems that %s plugin is not installed. Please install and activate the plugin to finish installation.', 'amp-app-shell' ), // phpcs:ignore WordPress.Security.EscapeOutput |
| 64 | + '<a href="https://wordpress.org/plugins/amp/" target="_blank">AMP</a>' |
| 65 | + ) |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + if ( ! is_plugin_active( 'pwa/pwa.php' ) ) { |
| 70 | + $_amp_app_shell_load_errors->add( |
| 71 | + 'amp_plugin_required', |
| 72 | + sprintf( |
| 73 | + /* translators: %s: <a href="https://wordpress.org/plugins/pwa/" target="_blank">PWA</a> */ |
| 74 | + __( 'It seems that %s plugin is not installed. Please install and activate the plugin to finish installation.', 'amp-app-shell' ), // phpcs:ignore WordPress.Security.EscapeOutput |
| 75 | + '<a href="https://wordpress.org/plugins/pwa/" target="_blank">PWA</a>' |
| 76 | + ) |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + // Abort if dependencies are not satisfied. |
| 81 | + if ( ! empty( $_amp_app_shell_load_errors->errors ) ) { |
| 82 | + add_action( 'admin_notices', '_amp_app_shell_show_load_errors_admin_notice' ); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +add_action( 'admin_init', '_amp_app_shell_check_dependecies' ); |
| 87 | + |
| 88 | +/** |
| 89 | + * Displays an admin notice about why the plugin is unable to load. |
| 90 | + * |
| 91 | + * @since 1.1.2 |
| 92 | + * @global WP_Error $_amp_app_shell_load_errors |
| 93 | + */ |
| 94 | +function _amp_app_shell_show_load_errors_admin_notice() { |
| 95 | + global $_amp_app_shell_load_errors; |
| 96 | + ?> |
| 97 | + <div class="notice notice-error"> |
| 98 | + <p> |
| 99 | + <strong><?php esc_html_e( 'AMP App Shell plugin unable to initialize.', 'amp-app-shell' ); ?></strong> |
| 100 | + <ul> |
| 101 | + <?php foreach ( array_keys( $_amp_app_shell_load_errors->errors ) as $error_code ) : ?> |
| 102 | + <?php foreach ( $_amp_app_shell_load_errors->get_error_messages( $error_code ) as $message ) : ?> |
| 103 | + <li> |
| 104 | + <?php echo wp_kses_post( $message ); ?> |
| 105 | + </li> |
| 106 | + <?php endforeach; ?> |
| 107 | + <?php endforeach; ?> |
| 108 | + </ul> |
| 109 | + </p> |
| 110 | + </div> |
| 111 | + <?php |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Print admin notice if plugin installed with incorrect slug (which impacts WordPress's auto-update system). |
| 116 | + * |
| 117 | + * @since 1.0 |
| 118 | + */ |
| 119 | +function _amp_app_shell_incorrect_plugin_slug_admin_notice() { |
| 120 | + $actual_slug = basename( AMP_APP_SHELL__DIR__ ); |
| 121 | + ?> |
| 122 | + <div class="notice notice-warning"> |
| 123 | + <p> |
| 124 | + <?php |
| 125 | + echo wp_kses_post( |
| 126 | + sprintf( |
| 127 | + /* translators: %1$s is the current directory name, and %2$s is the required directory name */ |
| 128 | + __( 'You appear to have installed the AMP App Shell plugin incorrectly. It is currently installed in the <code>%1$s</code> directory, but it needs to be placed in a directory named <code>%2$s</code>. Please rename the directory.', 'amp-app-shell' ), |
| 129 | + $actual_slug, |
| 130 | + 'amp-app-shell' |
| 131 | + ) |
| 132 | + ); |
| 133 | + ?> |
| 134 | + </p> |
| 135 | + </div> |
| 136 | + <?php |
| 137 | +} |
| 138 | + |
| 139 | +if ( 'amp-app-shell' !== basename( AMP_APP_SHELL__DIR__ ) ) { |
| 140 | + add_action( 'admin_notices', '_amp_app_shell_incorrect_plugin_slug_admin_notice' ); |
| 141 | +} |
| 142 | + |
| 143 | +require_once AMP_APP_SHELL__DIR__ . '/includes/amp-app-shell-helper-functions.php'; |
| 144 | + |
| 145 | +register_activation_hook( __FILE__, 'amp_app_shell_activate' ); |
| 146 | + |
| 147 | +amp_app_shell_bootstrap_plugin(); |
0 commit comments