| 1 |
<?php |
| 2 |
/** |
| 3 |
* Adds Better Block Editor specific welcome guide to Block Editor. |
| 4 |
* |
| 5 |
* @package BetterBlockEditor |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BetterBlockEditor\Modules\WelcomeGuide; |
| 9 |
|
| 10 |
use BetterBlockEditor\Base\ModuleBase; |
| 11 |
use BetterBlockEditor\Base\ModuleInterface; |
| 12 |
use BetterBlockEditor\Plugin; |
| 13 |
|
| 14 |
defined( 'ABSPATH' ) || exit; |
| 15 |
|
| 16 |
class Module extends ModuleBase implements ModuleInterface { |
| 17 |
|
| 18 |
const RESET_QUERY_PARAM = 'better-block-editor_reset-welcome-guide'; |
| 19 |
const METADATA_PREFERENCES_KEY = 'wpbbe/welcome-guide'; |
| 20 |
|
| 21 |
const MODULE_IDENTIFIER = 'core-welcome-guide'; |
| 22 |
const PLUGIN_ASSETS_BUILD_PATH = 'editor/plugins/welcome-guide/'; |
| 23 |
|
| 24 |
const IS_CORE_MODULE = true; |
| 25 |
|
| 26 |
public function init() { |
| 27 |
parent::init(); |
| 28 |
|
| 29 |
add_action( 'admin_init', array( $this, 'maybe_reset_welcome_guide' ) ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Checks if the welcome guide needs to be reset and performs the reset if necessary. |
| 34 |
*/ |
| 35 |
public function maybe_reset_welcome_guide(): void { |
| 36 |
global $pagenow; |
| 37 |
|
| 38 |
if ( ( ! current_user_can( 'edit_user', get_current_user_id() ) ) || $pagenow !== 'profile.php' ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
if ( ! sanitize_key( wp_unslash( $_GET[ self::RESET_QUERY_PARAM ] ?? '' ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
$this->reset_welcome_guide(); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Resets the welcome guide for the current user by removing the relevant metadata. |
| 51 |
*/ |
| 52 |
protected function reset_welcome_guide(): void { |
| 53 |
// User is on their profile page and can edit it |
| 54 |
$new_value = $this->get_current_user_preferences(); |
| 55 |
|
| 56 |
unset( $new_value[ self::METADATA_PREFERENCES_KEY ] ); |
| 57 |
|
| 58 |
// this function will return false if there is no such key in user meta |
| 59 |
update_user_meta( get_current_user_id(), 'wp_persisted_preferences', $new_value ); |
| 60 |
|
| 61 |
$current_value = $this->get_current_user_preferences(); |
| 62 |
|
| 63 |
$message = __( 'Now Welcome Guide for Better Block Editor will be shown again.', 'better-block-editor' ); |
| 64 |
|
| 65 |
if ( ! array_key_exists( self::METADATA_PREFERENCES_KEY, $current_value ) ) { |
| 66 |
add_action( |
| 67 |
'admin_notices', |
| 68 |
function () use ( $message ) { |
| 69 |
echo '<div class="notice notice-success"><p>' . esc_html( $message ) . '</p></div>'; |
| 70 |
} |
| 71 |
); |
| 72 |
// we don't do redirect here as this URL is more for service purposes than for user experience |
| 73 |
// so to avoid tricks with notice disappearing on redirect we just show it on the same page |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
protected function get_current_user_preferences(): array { |
| 78 |
$user_id = get_current_user_id(); |
| 79 |
|
| 80 |
$preferences = get_user_meta( $user_id, 'wp_persisted_preferences', true ); |
| 81 |
if ( ! is_array( $preferences ) ) { |
| 82 |
$preferences = array(); |
| 83 |
} |
| 84 |
|
| 85 |
return $preferences; |
| 86 |
} |
| 87 |
|
| 88 |
public function process_assets() { |
| 89 |
parent::process_assets(); |
| 90 |
|
| 91 |
// in asset bundle mode plugin assets are already registered |
| 92 |
if ( Plugin::instance()->is_asset_bundle_mode() ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
$asset_file = require WPBBE_DIST . $this::PLUGIN_ASSETS_BUILD_PATH . 'editor.asset.php'; |
| 97 |
|
| 98 |
wp_register_script( |
| 99 |
$this->build_script_handle( 'editor-plugin' ), |
| 100 |
WPBBE_URL_DIST . $this::PLUGIN_ASSETS_BUILD_PATH . $this::EDITOR_ASSET_KEY . '.js', |
| 101 |
$asset_file['dependencies'], |
| 102 |
$asset_file['version'], |
| 103 |
array( |
| 104 |
'strategy' => 'defer', |
| 105 |
'in_footer' => true, |
| 106 |
) |
| 107 |
); |
| 108 |
|
| 109 |
add_action( |
| 110 |
'enqueue_block_editor_assets', |
| 111 |
function () { |
| 112 |
$this->enqueue_assets( 'editor-plugin' ); |
| 113 |
} |
| 114 |
); |
| 115 |
} |
| 116 |
} |
| 117 |
|