Reusable Admin Panel Example Plugin

by Poly Plugins in Uncategorized on January 21, 2023

NEED A DEVELOPER?

We’re available to take on new clients. If you’re impressed by our code, our agency would love to work with you or your company for your next project. If you’re a developer looking for a community that helps others when they get stuck in the mud, we do have a Rubber Ducky Discord meant to help you think out your problems.

HOW TO USE

Here’s an example of how your plugin should be used to take advantage of Reusable Admin Panel. You can also download the full versions with css and everything you need to get started. We have a basic example, a PSR-4 compliant version, and a plugin we’ve migrated to use Reusable Admin Panel called Loginator. You can download these below.

<?php

/**
 * Plugin Name: Test Plugin
 * Description: Test
 * Version: 1.0.0
 * Author: Poly Plugins
 * Author URI: https://www.polyplugins.com
 * Plugin URI: https://www.polyplugins.com
 */

namespace Company;

use PolyPlugins\Settings;

if (!defined('ABSPATH')) exit;

class Test_Plugin
{
  /**
   * Full path and filename of plugin.
   *
   * @since    1.0.0
   * @access   private
   * @var      string    $plugin    Full path and filename of plugin.
   */
  private $plugin;
  
  /**
   * Namespace of plugin.
   *
   * @since    1.0.0
   * @access   private
   * @var      string    $plugin    Full path and filename of plugin.
   */
  private $namespace;

  /**
   * The ID of this plugin.
   *
   * @since    1.0.0
   * @access   private
   * @var      string    $plugin_slug    The ID of this plugin.
   */
  private $plugin_slug;

  /**
   * The plugin name
   *
   * @since    1.0.0
   * @access   private
   * @var      string    $plugin_name    Name of the plugin
   */
  private $plugin_name;

  /**
   * The settings class configuration
   *
   * @since    1.0.0
   * @access   private
   * @var      array    $config    The settings class configuration
   */
  private $config;

  /**
   * The plugin's options fields
   *
   * @since    1.0.0
   * @access   private
   * @var      array    $fields    The plugin's options fields
   */
  private $fields;

  /**
   * The Settings class
   *
   * @since    1.0.0
   * @access   private
   * @var      object    $settings    The Settings class
   */
  private $settings;

  /**
   * Store admin notices
   *
   * @since    1.0.0
   * @access   private
   * @var      array    $admin_notice    Store admin notices
   */
  private $admin_notice;

  public function __construct()
  {
    $this->plugin      = __FILE__;
    $this->namespace   = __NAMESPACE__;
    $this->plugin_slug = dirname(plugin_basename($this->plugin));
    $this->plugin_name = __(mb_convert_case(str_replace('-', ' ', $this->plugin_slug), MB_CASE_TITLE), $this->plugin_slug);
    
    $this->config  = array(
      'page'       => 'options-general.php', // You can use non php pages such as woocommerce here to display a submenu under Woocommerce
      'position'   => 1, // Lower number moves the link position up in the submenu
      'capability' => 'manage_options', // What permission is required to see and edit settings
      'logo'       => '/img/logo.png', // Your custom logo
      'css'        => '/css/style.css', // Your custom colors and styles
      'support'    => 'https://www.polyplugins.com/support/', // Your support link
    );

    $this->fields = array(
      'general' => array(
        array(
          'name'     => __('Enabled', $this->plugin_slug),
          'type'     => 'switch',
          'default'  => false,
        ),
        array(
          'name'     => __('Username', $this->plugin_slug),
          'type'     => 'text',
          'default'  => false,
          'help'     => __('Enter a username.', $this->plugin_slug),
        ),
        array(
          'name'     => __('Password', $this->plugin_slug),
          'type'     => 'password',
          'default'  => 'test',
          'help'     => __('Enter a password. Note: This is stored in the DB as plain text as most other plugins do, we will change this if requested.', $this->plugin_slug),
        ),
        array(
          'name'     => __('Number', $this->plugin_slug),
          'type'     => 'number',
          'default'  => false,
          'help'     => __('Enter a number.', $this->plugin_slug),
          'required' => true,
        ),
        array(
          'name'     => __('Time', $this->plugin_slug),
          'type'     => 'time',
          'default'  => false,
          'help'     => __('Select a time.', $this->plugin_slug),
        ),
        array(
          'name'     => __('Date', $this->plugin_slug),
          'type'     => 'date',
          'default'  => false,
          'help'     => __('Select a date.', $this->plugin_slug),
        ),
        array(
          'name'     => __('Color Picker', $this->plugin_slug),
          'type'     => 'color',
          'default'  => '#00ff00',
          'help'     => __('Select a color.', $this->plugin_slug),
        ),
        array(
          'name'     => __('Dropdown', $this->plugin_slug),
          'type'     => 'dropdown',
          'options'  => array('Red', 'Blue'),
          'default'  => false,
          'help'     => __('Select an option from the dropdown.', $this->plugin_slug),
        ),
      ),
    );
  }
  
  /**
   * Initialize the plugin
   *
   * @return void
   */
  public function init()
  {
    if (class_exists('PolyPlugins\Settings')) {
      $this->settings = new Settings($this->plugin, $this->namespace, $this->config, $this->fields);
      $this->settings->init();

      add_action('wp_loaded', array($this, 'display_option'));
    } else {
      $this->add_notice('"' . $this->plugin_name . '"' . " requires <a href='/wp-admin/plugin-install.php?tab=plugin-information&amp;plugin=reusable-admin-panel&amp;TB_iframe=true&amp;width=772&amp;height=608' class='thickbox open-plugin-details-modal' aria-label='More information about Reusable Admin Panel' data-title='Reusable Admin Panel'>Reusable Admin Panel</a> to be installed.");
    }
  }
  
  /**
   * Display an option
   *
   * @return void
   */
  public function display_option() {
    ?>
    <script>
      console.log("This is how you get a setting",
        '<?php echo $this->settings->get_option('general', 'color-picker'); ?>'
      );
    </script>
    <?php
  }

  /**
   * Display the notice on the admin backend
   *
   * @return void
   */
  public function display_notice() {
    ?>
    <div class="notice notice-<?php echo $this->admin_notice['type']; ?>">
      <p><?php echo $this->admin_notice['message']; ?></p>
    </div>
    <?php
  }
  
  /**
   * Enqueue the admin notice
   *
   * @param  string $message The message being displayed in admin
   * @param  string $type Optional. The type of message displayed. Default error.
   * @return void
   */
  private function add_notice($message, $type = 'error') {
    $this->admin_notice = array(
      'message' => $message,
      'type'   => $type
    );

    add_action('admin_notices', array($this, 'display_notice'));
  }
}

$test_plugin = new Test_Plugin;
$test_plugin->init();

Categories: Uncategorized