Initial commit, dashboard settings page seems to work fine.
This commit is contained in:
166
includes/class-app.php
Normal file
166
includes/class-app.php
Normal file
@ -0,0 +1,166 @@
|
||||
<?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 dashboard.
|
||||
if ( is_admin() ) {
|
||||
require_once __DIR__ . '/dashboard/class-settings.php';
|
||||
}
|
||||
//
|
||||
|
||||
// Files to include on every request.
|
||||
//
|
||||
|
||||
// Files to include that may be sub-namespaced.
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param \eftec\bladeone\BladeOne $blade
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_blade( BladeOne $blade ): void {
|
||||
$this->blade = $blade;
|
||||
}
|
||||
|
||||
}
|
44
includes/class-nmspcli.php
Normal file
44
includes/class-nmspcli.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace PluginNamespace;
|
||||
|
||||
use WP_CLI;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
class NmspCli {
|
||||
|
||||
/**
|
||||
* Class constructor. Can add commands here.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct() {
|
||||
WP_CLI::add_command(
|
||||
'nmsp-plugin do',
|
||||
array( $this, 'do_the_thing' ),
|
||||
array(
|
||||
'shortdesc' => 'The short description.',
|
||||
'longdesc' => "This is the super long description that will go on and on my friend. Some people started writing it not knowing what it was, and we'll continue writing it forever just because this is the description that never ends...",
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A sample method where something is done.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function do_the_thing(): void {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
new NmspCli();
|
73
includes/dashboard/class-settings.php
Normal file
73
includes/dashboard/class-settings.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace PluginNamespace;
|
||||
|
||||
class Settings {
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct() {
|
||||
add_action( 'admin_menu', array( $this, 'append_to_main_dashboard_menu' ), 12 );
|
||||
|
||||
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Append to the dashboard menu (prevents doubling).
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function append_to_main_dashboard_menu(): void {
|
||||
$parent_slug = 'nmsp-plugin-name-menu-slug';
|
||||
$page_title = __( 'Namespace Plugin Name - Settings', 'nmsp_plugin_name_settings_title' );
|
||||
$menu_title = __( 'Settings', 'nmsp_plugin_name_settings_title' );
|
||||
$capability = 'manage_options';
|
||||
$menu_slug = $parent_slug;
|
||||
$callback = array( $this, 'show_settings_page' );
|
||||
|
||||
add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $callback );
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the settings page on the dashboard.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function show_settings_page(): void {
|
||||
$data = array();
|
||||
|
||||
echo nmsp_plugin_app()->blade->run( 'dashboard.settings', $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register and enqueue stylesheets and javascript files.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function enqueue_scripts(): void {
|
||||
wp_enqueue_style( 'nmsp-plugin-dashboard-css', plugins_url( '../../assets/css/nmsp-plugin-name.css', __FILE__ ), array(), '1.0.0', 'screen' );
|
||||
|
||||
wp_register_script( 'nmsp-plugin-dashboard-js', plugins_url( '../../assets/js/nmsp-plugin-name.js', __FILE__ ), array(), '1.0.0', false );
|
||||
wp_enqueue_script( 'nmsp-plugin-dashboard-js' );
|
||||
|
||||
$payload = array(
|
||||
'wp_ajax_url' => admin_url( 'admin-ajax.php' ),
|
||||
'_nonce' => wp_create_nonce( NMSP_PLUGIN_NONCE ),
|
||||
);
|
||||
wp_localize_script( 'nmsp-plugin-dashboard-js', 'nmspDashboardApi', $payload );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
new Settings();
|
93
includes/front/class-rewrites.php
Normal file
93
includes/front/class-rewrites.php
Normal file
@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace PluginNamespace\Front;
|
||||
|
||||
class Rewrites {
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct() {
|
||||
add_action( 'init', array( $this, 'add_rewrite_rules' ), 5 );
|
||||
|
||||
add_filter( 'query_vars', array( $this, 'custom_query_vars' ) );
|
||||
add_filter( 'nmsp_plugin_name_rewrite_urls', array( $this, 'build_rewrite_urls' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate new rewrite rules and flush the cached values.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function add_rewrite_rules(): void {
|
||||
$filters = apply_filters( 'nmsp_plugin_name_rewrite_urls', array() );
|
||||
|
||||
add_rewrite_tag( 'foo', '([0-9]+)' );
|
||||
|
||||
if ( is_iterable( $filters ) && count( $filters ) > 0 ) {
|
||||
foreach ( $filters as $item ) {
|
||||
if ( $item['slug'] ) {
|
||||
$slug = str_replace( home_url() . '/', '', get_permalink( $item['id'] ) );
|
||||
add_rewrite_rule( '^' . $slug . '([^/]*' . $item['var'] . ')/([^/]*)/?', 'index.php?page_id=' . $item['id'] . '&$matches[1]=$matches[2]', 'top' );
|
||||
}
|
||||
}
|
||||
|
||||
flush_rewrite_rules();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the array of post IDs that have a slug for the URL
|
||||
* along with what custom query variable to use.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function build_rewrite_urls(): array {
|
||||
$pages = array();
|
||||
|
||||
$page_list = array(
|
||||
'NMSP_PLUGIN_SLUG_PAGE_NAME' => array( 'foo' ),
|
||||
);
|
||||
|
||||
foreach ( $page_list as $key => $params ) {
|
||||
$page_id = get_option( constant( $key ) );
|
||||
$page = ( $page_id ) ? get_post( $page_id ) : false;
|
||||
if ( ! empty( $page ) ) {
|
||||
foreach ( $params as $param ) {
|
||||
$pages[] = array(
|
||||
'id' => $page_id,
|
||||
'slug' => $page->post_name,
|
||||
'var' => $param,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $pages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append custom query variables to the list WordPress maintinas
|
||||
* with each request.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param array $variable
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function custom_query_vars( array $vars ): array {
|
||||
$vars[] = 'foo';
|
||||
return $vars;
|
||||
}
|
||||
}
|
||||
|
||||
new Rewrites();
|
Reference in New Issue
Block a user