'card', 'label' => __( 'Card', 'endpaper' ), 'style_data' => array( 'color' => array( 'background' => 'color-mix(in srgb, currentcolor 5%, transparent)', ), 'border' => array( 'radius' => '0.75em', ), 'spacing' => array( 'padding' => '1.5em', ), ), ) ); } add_action( 'init', __NAMESPACE__ . '\register_block_styles', 11 ); /** * Tells a block's editor script whether the API key works. * * The block shows a message with a link to the settings, instead of a search * that cannot work, when there is no usable key. The link is only given to * people who can open the settings. * * @param \WP_Block_Type $block_type The registered block. * @return void */ function add_editor_data( $block_type ) { $handle = $block_type->editor_script_handles[0] ?? ''; if ( ! $handle ) { return; } // Looked up when the editor loads, not on every request. add_action( 'enqueue_block_editor_assets', function () use ( $handle ) { $data = array( 'keyTrouble' => get_token_trouble(), 'settingsUrl' => current_user_can( 'manage_options' ) ? get_settings_url() : '', 'defaultBookStyle' => get_default_book_style(), 'searchResults' => get_search_results_count(), 'cacheGeneration' => absint( get_option( 'endpaper_cache_generation', 0 ) ), ); wp_add_inline_script( $handle, 'window.endpaperEditor = ' . wp_json_encode( $data ) . ';', 'before' ); } ); } /** * Gives a block's stylesheets the version of its latest build. * * WordPress versions a block's stylesheets by the version in block.json, * which only changes when someone remembers to change it. Browsers would * then keep showing an old stylesheet after an update. The build writes a * new version to index.asset.php every time anything changes, so that is * used instead. * * @param \WP_Block_Type $block_type The registered block. * @param string $asset_file Path to the block's index.asset.php. * @return void */ function set_block_style_versions( $block_type, $asset_file ) { if ( ! is_readable( $asset_file ) ) { return; } $asset = include $asset_file; if ( ! is_array( $asset ) || empty( $asset['version'] ) ) { return; } $styles = wp_styles(); foreach ( array_merge( $block_type->style_handles, $block_type->editor_style_handles ) as $handle ) { if ( isset( $styles->registered[ $handle ] ) ) { $styles->registered[ $handle ]->ver = $asset['version']; } } } /** * Notes that the plugin was just activated, so the next screen can be the settings screen. * * Not when it is activated for a whole network or from the command line, * where nobody is waiting to be sent anywhere. * * @param bool $network_wide Whether the plugin is being activated for all sites in a network. * @return void */ function set_activation_redirect( $network_wide ) { if ( $network_wide || ( defined( 'WP_CLI' ) && WP_CLI ) ) { return; } set_transient( 'endpaper_activation_redirect', 1, 30 ); } register_activation_hook( __FILE__, __NAMESPACE__ . '\set_activation_redirect' ); register_deactivation_hook( __FILE__, __NAMESPACE__ . '\unschedule_cover_jobs' );