Created
April 17, 2023 16:40
-
-
Save landbryo/f8b5dd25d8b7b2bdf738d67a4f3c5296 to your computer and use it in GitHub Desktop.
Plugin setup class template. All client name references replaced with "plugin".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Core setup. | |
* | |
* @package Plugin_Core | |
*/ | |
namespace PluginCore; | |
/** | |
* Setup class. | |
*/ | |
class Setup { | |
/** | |
* A mapping of all blocks with dynamic PHP templates. | |
* | |
* @var array | |
*/ | |
private $blocks_with_template = []; | |
/** | |
* Return only one instance of Setup. | |
* | |
* @return Setup | |
*/ | |
public static function get_instance() { | |
static $instance; | |
if ( empty( $instance ) ) { | |
$instance = new self(); | |
} | |
return $instance; | |
} | |
/** | |
* Add Hooks and Actions | |
*/ | |
protected function __construct() { | |
add_action( 'plugins_loaded', [ $this, 'setup' ] ); | |
} | |
/** | |
* Set up the includes and actions. | |
* | |
* @return void | |
*/ | |
public function setup() { | |
$this->includes(); | |
$this->actions(); | |
} | |
/** | |
* Includes | |
*/ | |
protected function includes() { | |
( new Settings() )->init(); | |
( new Posts() )->init(); | |
( new Universal() )->init(); | |
( new Authors() )->init(); | |
( new Terms() )->init(); | |
( new PostTypes() )->init(); | |
} | |
/** | |
* Actions and Filters | |
*/ | |
protected function actions() { | |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin' ] ); | |
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_public' ] ); | |
add_action( 'init', [ $this, 'register_blocks' ], 10 ); | |
add_action( 'init', [ $this, 'register_block_patterns' ], 10 ); | |
add_action( 'init', [ $this, 'register_block_pattern_categories' ], 10 ); | |
add_action( 'widgets_init', [ $this, 'register_widgets' ], 10 ); | |
add_filter( 'block_categories_all', [ $this, 'register_block_categories' ], 10, 1 ); | |
add_filter( 'template_include', [ $this, 'template_override' ], 10, 1 ); | |
} | |
/** | |
* Get the block pattern object from a block pattern json file. | |
* Uses transients in case object caching is not available. | |
* | |
* @param string $slug JSON filename slug in the `patterns` directory. | |
* | |
* @return mixed | |
*/ | |
private function get_pattern_by_filename_slug( $slug ) { | |
$cache_key = "patterns_$slug"; | |
$json = get_transient( $cache_key ); | |
if ( false === $json ) { | |
$json = file_get_contents( $this->get_plugin_path() . "patterns/$slug.json" ); | |
set_transient( $cache_key, $json, WEEK_IN_SECONDS ); | |
} | |
return json_decode( $json ); | |
} | |
/** | |
* Register block patterns. | |
* | |
* New patterns can be added by adding a JSON export from `wp_blocks` | |
* to the patterns directory and adding the filename slug to the array below. | |
* | |
* @return void | |
*/ | |
public function register_block_patterns() { | |
$filename_slugs = [ | |
'cta', | |
'featured-expert', | |
]; | |
foreach ( $filename_slugs as $slug ) { | |
$pattern = $this->get_pattern_by_filename_slug( $slug ); | |
if ( ! empty( $pattern ) ) { | |
register_block_pattern( | |
"plugin-core/pattern-$slug", | |
[ | |
'title' => $pattern->title, | |
'content' => $pattern->content, | |
'categories' => [ 'plugin' ], | |
] | |
); | |
} | |
} | |
} | |
/** | |
* Register block pattern categories. | |
* | |
* @return void | |
*/ | |
public function register_block_pattern_categories() { | |
register_block_pattern_category( | |
'plugin', | |
[ 'label' => __( 'Plugin', 'plugin-core' ) ] | |
); | |
} | |
/** | |
* Register sidebars/widgets. | |
* | |
* @return void | |
*/ | |
public function register_widgets() { | |
// Register article disclosure widget. | |
register_sidebar( | |
[ | |
'name' => __( 'Article Disclosure', 'plugin-core' ), | |
'id' => 'plugin_article_disclosure', | |
'before_widget' => '<div class="plugin-article-disclosure">', | |
'after_widget' => '</div>', | |
'before_title' => '<h2>', | |
'after_title' => '</h2>', | |
] | |
); | |
} | |
/** | |
* Override specific WP template paths. | |
* | |
* @param string $template template path. | |
* | |
* @return string | |
*/ | |
public function template_override( $template ) { | |
if ( is_author() ) { | |
return $this->get_plugin_path() . 'includes/templates/archive-author.php'; | |
} | |
if ( Universal::is_plugin_archive() ) { | |
return $this->get_plugin_path() . 'includes/templates/archive-default.php'; | |
} | |
return $template; | |
} | |
/** | |
* Register all defined blocks living in the "/blocks/" directory. | |
* | |
* @return void | |
*/ | |
public function register_blocks() { | |
$blocks = [ | |
'carousel', | |
'post-meta', | |
'vep', | |
'course-card', | |
'course-card-vertical', | |
'course-card-carousel', | |
]; | |
foreach ( $blocks as $block_dir_name ) { | |
$type = register_block_type( | |
$this->get_block_build_path( $block_dir_name ), | |
[ | |
'render_callback' => [ | |
$this, | |
'render', | |
], | |
] | |
); | |
if ( ! empty( $type ) ) { | |
$this->blocks_with_template[ $type->name ] = $this->get_template_path( $block_dir_name ); | |
} | |
} | |
} | |
/** | |
* Render Dynamic blocks. | |
* | |
* @param array $attributes Block attributes. | |
* @param string $content Block content. | |
* @param object $block Block object. | |
* | |
* @return string | |
*/ | |
public function render( $attributes, $content, $block ) { | |
$template = $this->blocks_with_template[ $block->name ]; | |
if ( empty( $template ) || ! file_exists( $template ) ) { | |
return ''; | |
} | |
ob_start(); | |
load_template( | |
$template, | |
false, | |
[ | |
'attributes' => $attributes, | |
'content' => $content, | |
'block' => $block, | |
] | |
); | |
return ob_get_clean(); | |
} | |
/** | |
* Register custom block categories. | |
* | |
* @param array $categories Existing block categories array. | |
* | |
* @return mixed | |
*/ | |
public function register_block_categories( $categories ) { | |
// Add plugin category to beginning of categories. | |
array_unshift( | |
$categories, | |
[ | |
'slug' => 'plugin', | |
'title' => __( 'Plugin', 'plugin-core' ), | |
] | |
); | |
return $categories; | |
} | |
/** | |
* Enqueue admin scripts and styles. | |
* | |
* @param string $page The current admin page. | |
* | |
* @return void | |
*/ | |
public function enqueue_admin( $page ) { | |
$screen = get_current_screen(); | |
if ( ! is_object( $screen ) ) { | |
return; | |
} | |
// Scripts for all admin area. | |
wp_enqueue_script( | |
$this->get_id() . '-global', | |
$this->get_plugin_url() . 'build/admin-global-scripts.js', | |
[ 'wp-dom-ready' ], | |
$this->get_version(), | |
true | |
); | |
// Styles for all admin area. | |
wp_enqueue_style( | |
$this->get_id() . '-global', | |
$this->get_plugin_url() . 'build/admin-global-scripts.css', | |
[], | |
$this->get_version() | |
); | |
// Enqueue for posts. | |
if ( in_array( $page, [ 'post.php', 'post-new.php' ], true ) && 'post' === $screen->post_type ) { | |
wp_enqueue_script( | |
$this->get_id() . '-post', | |
$this->get_plugin_url() . 'build/admin-post-scripts.js', | |
[ 'wp-dom-ready' ], | |
$this->get_version(), | |
true | |
); | |
wp_enqueue_style( | |
$this->get_id() . '-post', | |
$this->get_plugin_url() . 'build/admin-post-scripts.css', | |
[], | |
$this->get_version() | |
); | |
} | |
// Enqueue for pages & reusable blocks. | |
if ( in_array( $page, [ 'post.php', 'post-new.php' ], true ) && 'page' === $screen->post_type ) { | |
wp_enqueue_script( | |
$this->get_id() . '-page', | |
$this->get_plugin_url() . 'build/admin-page-scripts.js', | |
[ 'wp-dom-ready' ], | |
$this->get_version(), | |
true | |
); | |
wp_enqueue_style( | |
$this->get_id() . '-page', | |
$this->get_plugin_url() . 'build/admin-page-scripts.css', | |
[], | |
$this->get_version() | |
); | |
} | |
// Enqueue for pages & reusable blocks. | |
if ( in_array( $page, [ 'post.php', 'post-new.php' ], true ) && 'wp_block' === $screen->post_type ) { | |
// Dequeue Astra's block editor script which doesn't work well with Reusable Blocks. | |
wp_dequeue_script( 'astra-block-editor-script' ); | |
wp_enqueue_script( | |
$this->get_id() . '-wp-block', | |
$this->get_plugin_url() . 'build/admin-wp-block-scripts.js', | |
[ 'wp-dom-ready' ], | |
$this->get_version(), | |
true | |
); | |
wp_enqueue_style( | |
$this->get_id() . '-wp-block', | |
$this->get_plugin_url() . 'build/admin-wp-block-scripts.css', | |
[], | |
$this->get_version() | |
); | |
} | |
// Enqueue for sidebar custom post type admin. | |
if ( in_array( $page, [ 'post.php', 'post-new.php' ], true ) && 'plugin_sidebar' === $screen->post_type ) { | |
wp_enqueue_script( | |
$this->get_id() . '-sidebar', | |
$this->get_plugin_url() . 'build/admin-sidebar-scripts.js', | |
[ 'wp-dom-ready' ], | |
$this->get_version(), | |
true | |
); | |
wp_enqueue_style( | |
$this->get_id() . '-sidebar', | |
$this->get_plugin_url() . 'build/admin-sidebar-scripts.css', | |
[], | |
$this->get_version() | |
); | |
} | |
// Enqueue for admin user profile. | |
if ( in_array( $page, [ 'user-edit.php', 'profile.php' ] ) ) { | |
wp_enqueue_editor(); | |
wp_enqueue_script( | |
$this->get_id() . '-user', | |
$this->get_plugin_url() . 'build/admin-user-scripts.js', | |
[ 'editor' ], | |
$this->get_version(), | |
true | |
); | |
wp_enqueue_style( | |
$this->get_id() . '-user', | |
$this->get_plugin_url() . 'build/admin-user-scripts.css', | |
[], | |
$this->get_version() | |
); | |
} | |
// Enqueue for edit and add terms. | |
if ( in_array( $page, [ 'term.php', 'edit-tags.php' ] ) ) { | |
wp_enqueue_media(); | |
wp_enqueue_script( | |
$this->get_id() . '-term', | |
$this->get_plugin_url() . 'build/admin-term-scripts.js', | |
[ 'wp-i18n' ], | |
$this->get_version(), | |
true | |
); | |
wp_enqueue_style( | |
$this->get_id() . '-term', | |
$this->get_plugin_url() . 'build/admin-term-scripts.css', | |
[], | |
$this->get_version() | |
); | |
} | |
} | |
/** | |
* Enqueue public scripts and styles. | |
* | |
* @return void | |
*/ | |
public function enqueue_public() { | |
$public_handle = $this->get_id() . '-public'; | |
/** | |
* Styles | |
*/ | |
wp_enqueue_style( $public_handle, $this->get_plugin_url() . 'build/public-scripts.css', [], $this->get_version() ); | |
/** | |
* Scripts | |
*/ | |
wp_enqueue_script( $public_handle, $this->get_plugin_url() . 'build/public-scripts.js', [], $this->get_version(), true ); | |
} | |
/** | |
* Return the version of the plugin. | |
* | |
* @return string | |
* @since 1.0.0 | |
*/ | |
public function get_version() { | |
static $version; | |
if ( empty( $version ) ) { | |
$plugin_data = get_plugin_data( PLUGIN_CORE_PLUGIN_FILE ); | |
$version = $plugin_data['Version']; | |
} | |
return $version; | |
} | |
/** | |
* Returns the plugin ID. | |
* | |
* @return string | |
* @since 1.0.0 | |
*/ | |
public function get_id() { | |
return 'plugin-core'; | |
} | |
/** | |
* Get the plugin URL (memoized). | |
* | |
* @return string | |
*/ | |
public function get_plugin_url() { | |
static $url; | |
if ( empty( $url ) ) { | |
$url = plugin_dir_url( PLUGIN_CORE_PLUGIN_FILE ); | |
} | |
return $url; | |
} | |
/** | |
* Get plugin dir path (memoized). | |
* | |
* @return string | |
*/ | |
public function get_plugin_path() { | |
static $path; | |
if ( empty( $path ) ) { | |
$path = plugin_dir_path( PLUGIN_CORE_PLUGIN_FILE ); | |
} | |
return $path; | |
} | |
/** | |
* Get block path in the build directory. | |
* | |
* @param string $block_dir_name The name of the block directory found in the blocks directory. | |
* | |
* @return string | |
*/ | |
public function get_block_build_path( $block_dir_name ) { | |
return sprintf( '%sbuild/%s', $this->get_plugin_path(), $block_dir_name ); | |
} | |
/** | |
* Get block path in the blocks directory. | |
* | |
* @param string $block_dir_name The name of the block directory found in the blocks directory. | |
* | |
* @return string | |
*/ | |
public function get_template_path( $block_dir_name ) { | |
return sprintf( '%sblocks/%s/template.php', $this->get_plugin_path(), $block_dir_name ); | |
} | |
/** | |
* Get export setting by key. | |
* | |
* @param string $key setting key value. | |
* @param mixed $default fallback value. | |
* | |
* @return mixed|string | |
*/ | |
public function get_setting( $key, $default = '' ) { | |
$settings = get_option( 'plugin-core', [] ); | |
// phpcs:ignore PHPCompatibility.Operators.NewOperators.t_coalesceFound | |
return $settings[ $key ] ?? $default; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment