Skip to content

Commit

Permalink
Warn if running plugin from source
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed Jul 7, 2020
1 parent 29e1542 commit a744d10
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions includes/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

namespace Google\Web_Stories;

global $heading, $body;

/**
* Handles plugin activation.
*
Expand Down Expand Up @@ -80,6 +82,60 @@ function deactivate( $network_wide ) {
register_activation_hook( WEBSTORIES_PLUGIN_FILE, '\Google\Web_Stories\activate' );
register_deactivation_hook( WEBSTORIES_PLUGIN_FILE, '\Google\Web_Stories\deactivate' );

global $heading;
global $body;

$heading = __( 'Web Stories plugin could not be initialized.', 'web-stories' );
$body = sprintf(
/* translators: %s: build commands. */
__( 'You appear to be running an incomplete version of the plugin. Please run %s to finish installation.', 'web-stories' ),
'<code>composer install &amp;&amp; npm install &amp;&amp; npm run build</code>'
);

if (
! class_exists( '\Google\Web_Stories\Plugin' ) ||
! file_exists( WEBSTORIES_PLUGIN_DIR_PATH . '/assets/js/edit-story.js' )
) {
/**
* Displays an admin notice about why the plugin is unable to load.
*/
function _print_missing_build_admin_notice() {
global $heading, $body;
?>
<div class="notice notice-error">
<p>
<strong><?php echo esc_html( $heading ); ?></strong>
</p>
<p>
<?php echo wp_kses_post( $body ); ?>
</p>
</div>
<?php

// Don't pollute global space.
unset( $heading, $body );
}

add_action( 'admin_notices', __NAMESPACE__ . '\_print_missing_build_admin_notice' );
}

// In CLI context, existence of the JS files is not required.
if (
! class_exists( '\Google\Web_Stories\Plugin' ) &&
( ( defined( 'WP_CLI' ) && WP_CLI ) || 'true' === getenv( 'CI' ) || 'cli' === PHP_SAPI )
) {
$body = html_entity_decode( str_replace( [ '<code>', '</code>' ], '`', $body ), ENT_QUOTES, 'UTF-8' );

if ( class_exists( '\WP_CLI' ) ) {
WP_CLI::warning( "$heading\n$body" );
} else {
echo "$heading\n$body\n"; // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
}

// Don't pollute global space.
unset( $heading, $body );
}

global $web_stories;

$web_stories = new Plugin();
Expand Down

0 comments on commit a744d10

Please sign in to comment.