167 lines
3.6 KiB
PHP

<?php
namespace PluginNamespace;
use eftec\bladeone\BladeOne;
use Exception;
class App {
/** @var \PluginNamespace\App */
public static $instance;
/** @var \eftec\bladeone\BladeOne */
public $blade;
/**
* Any logic that needs executed when the plugin is activated.
*
* @since 1.0.0
*
* @return void
*/
public static function plugin_activation(): void {
self::ensure_template_cache_dir_exists();
}
/**
* Any logic that needs executed when the plugin is deactivated.
*
* @since 1.0.0
*
* @return void
*/
public static function plugin_deactivation(): void {
//
}
/**
* Check that the cache directory for Blade exists and
* if not then attempt to create it.
*
* @since 1.0.0
*
* @throws \Exception
*
* @return void
*/
public static function ensure_template_cache_dir_exists(): void {
if ( ! file_exists( NMSP_PLUGIN_BASE_DIR . '/cache/blade/.gitignore' ) ) {
try {
mkdir( NMSP_PLUGIN_BASE_DIR . '/cache/blade', 0755, true );
} catch ( Exception $e ) {
$error_payload = array(
'level' => 'critical',
'message' => $e->getMessage(),
'context' => array(
'file' => $e->getFile(),
'line' => $e->getLine(),
),
);
error_log( json_encode( $error_payload, JSON_PRETTY_PRINT ) );
}
}
}
/**
* Return an instance of this plugin class. If it has
* not been initialized, do so then return it.
*
* @since 1.0.0
*
* @return \PluginNamespace\App
*/
public static function get_instance(): App {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof App ) ) {
self::$instance = new App();
self::$instance->load_plugin_textdomain();
self::$instance->scaffold_plugin();
self::$instance->includes();
}
return self::$instance;
}
/**
* Load your translation files for this plugin.
*
* @since 1.0.0
*
* @return void
*/
public function load_plugin_textdomain(): void {
load_plugin_textdomain( 'nmsp-plugin-name-text-domain', false, 'languages' );
}
/**
* Steps to take when initializing this plugin??
*
* @since 1.0.0
*
* @return void
*/
public function scaffold_plugin(): void {
add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
}
/**
* Setting up the WordPress dashboard menu for this plugin.
* Setting this here to be overriden later so that a base is always available.
*
* @since 1.0.0
*
* @return void
*/
public function add_admin_menu(): void {
$page_title = __( 'Namespace Plugin Name', 'nmsp_plugin_name_menu' );
$menu_title = __( 'Namespace Plugin Name', 'nmsp_plugin_name_menu' );
$capability = 'manage_options';
$menu_slug = 'nmsp-plugin-name-menu-slug';
$callback = '';
$icon = plugins_url( '../assets/images/menu-icon.svg', __FILE__ );
$position = 100;
add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $callback, $icon, $position );
}
/**
* Other files to include so that this plugin will
* have a single entrypoint but be able to break
* up code in a logical manor.
*
* @codeCoverageIgnore
*
* @since 1.0.0
*
* @return void
*/
public function includes(): void {
// Files to include on the admin.
if ( is_admin() ) {
require_once NMSP_PLUGIN_BASE_DIR . '/admin/class-settings.php';
}
//
// Files to include on every request.
//
// Files to include that may be sub-namespaced.
require_once __DIR__ . '/front/class-custompage.php';
}
/**
* Load Blade as the templating engine for the plugin.
*
* @since 1.0.0
*
* @param \eftec\bladeone\BladeOne $blade
*
* @return void
*/
public function load_blade( BladeOne $blade ): void {
$this->blade = $blade;
}
}