From 5ce0217861f8d1568b8e3e3d1b15a3338aaf87a9 Mon Sep 17 00:00:00 2001 From: Brian Rogers Date: Mon, 31 May 2021 15:03:52 -0600 Subject: [PATCH] Initial commit, dashboard settings page seems to work fine. --- .gitignore | 28 ++++ CHANGELOG.md | 0 LICENSE.md | 0 README.md | 0 assets/css/nmsp-plugin-name.css | 0 assets/css/vendor/.gitkeep | 0 assets/images/menu-icon.svg | 0 assets/js/nmsp-plugin-name.js | 0 assets/js/vendor/.gitkeep | 0 bootstrap.php | 24 ++++ composer.json | 47 +++++++ composer.lock | 187 +++++++++++++++++++++++++ constants/base.php | 15 ++ constants/http-codes.php | 65 +++++++++ constants/rewrites.php | 23 +++ includes/class-app.php | 166 ++++++++++++++++++++++ includes/class-nmspcli.php | 44 ++++++ includes/dashboard/class-settings.php | 73 ++++++++++ includes/front/class-rewrites.php | 93 ++++++++++++ nmsp-plugin-name.php | 36 +++++ resources/css/nmsp-plugin-name.css | 0 resources/js/nmsp-plugin-name.js | 0 templates/dashboard/settings.blade.php | 5 + templates/layouts/dashboard.blade.php | 5 + templates/partials/.gitkeep | 0 tests/Functional/.gitkeep | 0 tests/Integration/.gitkeep | 0 tests/Unit/.gitkeep | 0 tests/bootstrap.php | 3 + uninstall.php | 11 ++ 30 files changed, 825 insertions(+) create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 assets/css/nmsp-plugin-name.css create mode 100644 assets/css/vendor/.gitkeep create mode 100644 assets/images/menu-icon.svg create mode 100644 assets/js/nmsp-plugin-name.js create mode 100644 assets/js/vendor/.gitkeep create mode 100644 bootstrap.php create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 constants/base.php create mode 100644 constants/http-codes.php create mode 100644 constants/rewrites.php create mode 100644 includes/class-app.php create mode 100644 includes/class-nmspcli.php create mode 100644 includes/dashboard/class-settings.php create mode 100644 includes/front/class-rewrites.php create mode 100644 nmsp-plugin-name.php create mode 100644 resources/css/nmsp-plugin-name.css create mode 100644 resources/js/nmsp-plugin-name.js create mode 100644 templates/dashboard/settings.blade.php create mode 100644 templates/layouts/dashboard.blade.php create mode 100644 templates/partials/.gitkeep create mode 100644 tests/Functional/.gitkeep create mode 100644 tests/Integration/.gitkeep create mode 100644 tests/Unit/.gitkeep create mode 100644 tests/bootstrap.php create mode 100644 uninstall.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..01f217b --- /dev/null +++ b/.gitignore @@ -0,0 +1,28 @@ +### Project Specific +# Files +.env +phpcs.xml +phpunit.xml +.phpunit.result.cache + +# Directories +/vendor/ +node_modules/ +cache/ + +### Editor Specific +# Files +nmsp-plugin-name.sublime-project +nmsp-plugin-name.sublime-workspace + +# Directories +.idea +.vscode + +### System Specific +# Files +.DS_Store +Thumbs.db + +# Directories +__MACOSX diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..e69de29 diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/assets/css/nmsp-plugin-name.css b/assets/css/nmsp-plugin-name.css new file mode 100644 index 0000000..e69de29 diff --git a/assets/css/vendor/.gitkeep b/assets/css/vendor/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/assets/images/menu-icon.svg b/assets/images/menu-icon.svg new file mode 100644 index 0000000..e69de29 diff --git a/assets/js/nmsp-plugin-name.js b/assets/js/nmsp-plugin-name.js new file mode 100644 index 0000000..e69de29 diff --git a/assets/js/vendor/.gitkeep b/assets/js/vendor/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/bootstrap.php b/bootstrap.php new file mode 100644 index 0000000..e6fc719 --- /dev/null +++ b/bootstrap.php @@ -0,0 +1,24 @@ +=7.3.0", + "eftec/bladeone": "^3.52" + }, + "require-dev": { + "wp-coding-standards/wpcs": "^2.3" + }, + "config": { + "optimize-autoloader": true, + "preferred-install": "dist", + "sort-packages": true + }, + "autoload": { + "psr-4": { + "PluginNamespace\\": "includes/" + }, + "classmap": [ + "includes/" + ] + }, + "autoload-dev": { + "psr-4": { + "PluginNamespaceTests\\": "tests/" + }, + "classmap": [ + "tests/" + ] + }, + "minimum-stability": "dev", + "prefer-stable": true, + "scripts": { + "post-autoload-dump": [], + "post-root-package-install": [] + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..688e8a8 --- /dev/null +++ b/composer.lock @@ -0,0 +1,187 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "bd67390e5b29dd8e1a56e6e73d50d2c7", + "packages": [ + { + "name": "eftec/bladeone", + "version": "3.52", + "source": { + "type": "git", + "url": "https://github.com/EFTEC/BladeOne.git", + "reference": "a19bf66917de0b29836983db87a455a4f6e32148" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/EFTEC/BladeOne/zipball/a19bf66917de0b29836983db87a455a4f6e32148", + "reference": "a19bf66917de0b29836983db87a455a4f6e32148", + "shasum": "" + }, + "require": { + "ext-json": "*", + "php": ">=5.6" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^2.16.1", + "phpunit/phpunit": "^5.7", + "squizlabs/php_codesniffer": "^3.5.4" + }, + "suggest": { + "eftec/bladeonehtml": "Extension to create forms", + "ext-mbstring": "This extension is used if it's active" + }, + "type": "library", + "autoload": { + "psr-4": { + "eftec\\bladeone\\": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jorge Patricio Castro Castillo", + "email": "jcastro@eftec.cl" + } + ], + "description": "The standalone version Blade Template Engine from Laravel in a single php file", + "homepage": "https://github.com/EFTEC/BladeOne", + "keywords": [ + "blade", + "php", + "template", + "templating", + "view" + ], + "support": { + "issues": "https://github.com/EFTEC/BladeOne/issues", + "source": "https://github.com/EFTEC/BladeOne/tree/3.52" + }, + "time": "2021-04-17T13:49:01+00:00" + } + ], + "packages-dev": [ + { + "name": "squizlabs/php_codesniffer", + "version": "3.6.0", + "source": { + "type": "git", + "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", + "reference": "ffced0d2c8fa8e6cdc4d695a743271fab6c38625" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/ffced0d2c8fa8e6cdc4d695a743271fab6c38625", + "reference": "ffced0d2c8fa8e6cdc4d695a743271fab6c38625", + "shasum": "" + }, + "require": { + "ext-simplexml": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + }, + "bin": [ + "bin/phpcs", + "bin/phpcbf" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Greg Sherwood", + "role": "lead" + } + ], + "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", + "homepage": "https://github.com/squizlabs/PHP_CodeSniffer", + "keywords": [ + "phpcs", + "standards" + ], + "support": { + "issues": "https://github.com/squizlabs/PHP_CodeSniffer/issues", + "source": "https://github.com/squizlabs/PHP_CodeSniffer", + "wiki": "https://github.com/squizlabs/PHP_CodeSniffer/wiki" + }, + "time": "2021-04-09T00:54:41+00:00" + }, + { + "name": "wp-coding-standards/wpcs", + "version": "2.3.0", + "source": { + "type": "git", + "url": "https://github.com/WordPress/WordPress-Coding-Standards.git", + "reference": "7da1894633f168fe244afc6de00d141f27517b62" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/WordPress/WordPress-Coding-Standards/zipball/7da1894633f168fe244afc6de00d141f27517b62", + "reference": "7da1894633f168fe244afc6de00d141f27517b62", + "shasum": "" + }, + "require": { + "php": ">=5.4", + "squizlabs/php_codesniffer": "^3.3.1" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.5 || ^0.6", + "phpcompatibility/php-compatibility": "^9.0", + "phpcsstandards/phpcsdevtools": "^1.0", + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + }, + "suggest": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.6 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically." + }, + "type": "phpcodesniffer-standard", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Contributors", + "homepage": "https://github.com/WordPress/WordPress-Coding-Standards/graphs/contributors" + } + ], + "description": "PHP_CodeSniffer rules (sniffs) to enforce WordPress coding conventions", + "keywords": [ + "phpcs", + "standards", + "wordpress" + ], + "support": { + "issues": "https://github.com/WordPress/WordPress-Coding-Standards/issues", + "source": "https://github.com/WordPress/WordPress-Coding-Standards", + "wiki": "https://github.com/WordPress/WordPress-Coding-Standards/wiki" + }, + "time": "2020-05-13T23:57:56+00:00" + } + ], + "aliases": [], + "minimum-stability": "dev", + "stability-flags": [], + "prefer-stable": true, + "prefer-lowest": false, + "platform": { + "php": ">=7.3.0" + }, + "platform-dev": [], + "plugin-api-version": "2.0.0" +} diff --git a/constants/base.php b/constants/base.php new file mode 100644 index 0000000..3a792d0 --- /dev/null +++ b/constants/base.php @@ -0,0 +1,15 @@ + 'HTTP_SUCCESS', + 201 => 'HTTP_CREATED', + 202 => 'HTTP_ACCEPTED', + 204 => 'HTTP_NO_CONTENT', + 205 => 'HTTP_RESET_CONTENT', + 206 => 'HTTP_PARTIAL_CONTENT', + 208 => 'HTTP_ALREADY_REPORTED', + + // 300's (redirections) + 301 => 'HTTP_MOVED_PERMANENTLY', + 304 => 'HTTP_NOT_MODIFIED', + 307 => 'HTTP_TEMP_REDIRECT', + 308 => 'HTTP_PERMANENT_REDIRECT', + + // 400's (app got the request but couldn't process it successfully, coding error) + 400 => 'HTTP_BAD_REQUEST', + 401 => 'HTTP_UNAUTHORIZED', + 402 => 'HTTP_PAYMENT_REQUIRED', + 403 => 'HTTP_FORBIDDEN', + 404 => 'HTTP_NOT_FOUND', + 405 => 'HTTP_METHOD_NOT_ALLOWED', + 406 => 'HTTP_NOT_ACCEPTABLE', + 408 => 'HTTP_TIMEOUT', + 409 => 'HTTP_CONFLICT', + 410 => 'HTTP_GONE', + 412 => 'HTTP_PRECONDITION_FAILED', + 413 => 'HTTP_PAYLOAD_TOO_LARGE', + 415 => 'HTTP_UNSUPPORTED_MEDIA', + 413 => 'HTTP_TOO_LARGE', + 417 => 'HTTP_EXPECTATION_FAIL', + 418 => 'HTTP_TEAPOT', + 422 => 'HTTP_UNPROCESSABLE_ENTITY', + 423 => 'HTTP_LOCKED', + 424 => 'HTTP_FAILED_DEPENDENCY', + 236 => 'HTTP_UPGRADE_REQUIRED', + 428 => 'HTTP_PRECONDITION_REQUIRED', + 429 => 'HTTP_TOO_MANY_REQUESTS', + 451 => 'HTTP_GAG_ORDER', + + // 500's (server-level problem, process died or configuration is incorrect) + 500 => 'HTTP_SERVER_ERROR', + 501 => 'HTTP_NOT_IMPLEMENTED', + 503 => 'HTTP_UNAVAILABLE', + 530 => 'HTTP_SUSPENDED', +); + +foreach ( $codes as $code => $slug ) { + if ( ! defined( $slug ) ) { + define( $slug, $code ); + } +} diff --git a/constants/rewrites.php b/constants/rewrites.php new file mode 100644 index 0000000..19099b6 --- /dev/null +++ b/constants/rewrites.php @@ -0,0 +1,23 @@ + 'nmsp_plugin_slug_page_name', +); + +foreach ( NMSP_PLUGIN_SLUG_REWRITE_NAMES as $page_slug => $page_name ) { + if ( ! defined( $page_slug ) ) { + define( $page_slug, $page_name ); + } +} diff --git a/includes/class-app.php b/includes/class-app.php new file mode 100644 index 0000000..35a930a --- /dev/null +++ b/includes/class-app.php @@ -0,0 +1,166 @@ + '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; + } + +} diff --git a/includes/class-nmspcli.php b/includes/class-nmspcli.php new file mode 100644 index 0000000..ccc75d3 --- /dev/null +++ b/includes/class-nmspcli.php @@ -0,0 +1,44 @@ + '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(); diff --git a/includes/dashboard/class-settings.php b/includes/dashboard/class-settings.php new file mode 100644 index 0000000..0b5370b --- /dev/null +++ b/includes/dashboard/class-settings.php @@ -0,0 +1,73 @@ +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(); diff --git a/includes/front/class-rewrites.php b/includes/front/class-rewrites.php new file mode 100644 index 0000000..4025dce --- /dev/null +++ b/includes/front/class-rewrites.php @@ -0,0 +1,93 @@ + 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(); diff --git a/nmsp-plugin-name.php b/nmsp-plugin-name.php new file mode 100644 index 0000000..b8151a0 --- /dev/null +++ b/nmsp-plugin-name.php @@ -0,0 +1,36 @@ +load_blade( $blade ); diff --git a/resources/css/nmsp-plugin-name.css b/resources/css/nmsp-plugin-name.css new file mode 100644 index 0000000..e69de29 diff --git a/resources/js/nmsp-plugin-name.js b/resources/js/nmsp-plugin-name.js new file mode 100644 index 0000000..e69de29 diff --git a/templates/dashboard/settings.blade.php b/templates/dashboard/settings.blade.php new file mode 100644 index 0000000..9d306cb --- /dev/null +++ b/templates/dashboard/settings.blade.php @@ -0,0 +1,5 @@ +@extends('layouts.dashboard') + +@section('content') +

PluginNamespace Plugin Settings Page

+@endsection diff --git a/templates/layouts/dashboard.blade.php b/templates/layouts/dashboard.blade.php new file mode 100644 index 0000000..04a0761 --- /dev/null +++ b/templates/layouts/dashboard.blade.php @@ -0,0 +1,5 @@ +
+ + @yield('content') + +
diff --git a/templates/partials/.gitkeep b/templates/partials/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/Functional/.gitkeep b/tests/Functional/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/Integration/.gitkeep b/tests/Integration/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/Unit/.gitkeep b/tests/Unit/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..3d9949b --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,3 @@ +