|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Custom Featured Image Metabox. |
| 4 | + * |
| 5 | + * @package Custom_Featured_Image_Metabox_Admin |
| 6 | + * @author 1fixdotio <1fixdotio@gmail.com> |
| 7 | + * @license GPL-2.0+ |
| 8 | + * @link http://1fix.io |
| 9 | + * @copyright 2014 1Fix.io |
| 10 | + */ |
| 11 | + |
| 12 | +/** |
| 13 | + * Plugin class. This class should ideally be used to work with the |
| 14 | + * administrative side of the WordPress site. |
| 15 | + * |
| 16 | + * If you're interested in introducing public-facing |
| 17 | + * functionality, then refer to `class-custom-featured-image-metabox.php` |
| 18 | + * |
| 19 | + * @package Custom_Featured_Image_Metabox_Admin |
| 20 | + * @author 1fixdotio <1fixdotio> |
| 21 | + */ |
| 22 | +class Custom_Featured_Image_Metabox_Admin { |
| 23 | + |
| 24 | + /** |
| 25 | + * Instance of this class. |
| 26 | + * |
| 27 | + * @since 0.1.0 |
| 28 | + * |
| 29 | + * @var object |
| 30 | + */ |
| 31 | + protected static $instance = null; |
| 32 | + |
| 33 | + /** |
| 34 | + * Slug of the plugin screen. |
| 35 | + * |
| 36 | + * @since 0.1.0 |
| 37 | + * |
| 38 | + * @var string |
| 39 | + */ |
| 40 | + protected $plugin_screen_hook_suffix = null; |
| 41 | + |
| 42 | + /** |
| 43 | + * Initialize the plugin by loading admin scripts & styles and adding a |
| 44 | + * settings page and menu. |
| 45 | + * |
| 46 | + * @since 0.1.0 |
| 47 | + */ |
| 48 | + private function __construct() { |
| 49 | + |
| 50 | + /* |
| 51 | + * @TODO : |
| 52 | + * |
| 53 | + * - Uncomment following lines if the admin class should only be available for super admins |
| 54 | + */ |
| 55 | + /* if( ! is_super_admin() ) { |
| 56 | + return; |
| 57 | + } */ |
| 58 | + |
| 59 | + /* |
| 60 | + * Call $plugin_slug from public plugin class. |
| 61 | + * |
| 62 | + */ |
| 63 | + $plugin = Custom_Featured_Image_Metabox::get_instance(); |
| 64 | + $this->plugin_slug = $plugin->get_plugin_slug(); |
| 65 | + |
| 66 | + // Load admin style sheet and JavaScript. |
| 67 | + // add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) ); |
| 68 | + // add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); |
| 69 | + |
| 70 | + // Add the options page and menu item. |
| 71 | + add_action( 'admin_menu', array( $this, 'add_plugin_admin_menu' ) ); |
| 72 | + |
| 73 | + // Add an action link pointing to the options page. |
| 74 | + $plugin_basename = plugin_basename( plugin_dir_path( realpath( dirname( __FILE__ ) ) ) . $this->plugin_slug . '.php' ); |
| 75 | + add_filter( 'plugin_action_links_' . $plugin_basename, array( $this, 'add_action_links' ) ); |
| 76 | + |
| 77 | + /* |
| 78 | + * Define custom functionality. |
| 79 | + * |
| 80 | + * Read more about actions and filters: |
| 81 | + * http://codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters |
| 82 | + */ |
| 83 | + // add_action( '@TODO', array( $this, 'action_method_name' ) ); |
| 84 | + // add_filter( '@TODO', array( $this, 'filter_method_name' ) ); |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Return an instance of this class. |
| 90 | + * |
| 91 | + * @since 0.1.0 |
| 92 | + * |
| 93 | + * @return object A single instance of this class. |
| 94 | + */ |
| 95 | + public static function get_instance() { |
| 96 | + |
| 97 | + // If the single instance hasn't been set, set it now. |
| 98 | + if ( null == self::$instance ) { |
| 99 | + self::$instance = new self; |
| 100 | + } |
| 101 | + |
| 102 | + return self::$instance; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Register and enqueue admin-specific style sheet. |
| 107 | + * |
| 108 | + * @since 0.1.0 |
| 109 | + * |
| 110 | + * @return null Return early if no settings page is registered. |
| 111 | + */ |
| 112 | + public function enqueue_admin_styles() { |
| 113 | + |
| 114 | + if ( ! isset( $this->plugin_screen_hook_suffix ) ) { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + $screen = get_current_screen(); |
| 119 | + if ( $this->plugin_screen_hook_suffix == $screen->id ) { |
| 120 | + wp_enqueue_style( $this->plugin_slug .'-admin-styles', plugins_url( 'assets/css/admin.css', __FILE__ ), array(), Custom_Featured_Image_Metabox::VERSION ); |
| 121 | + } |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Register and enqueue admin-specific JavaScript. |
| 127 | + * |
| 128 | + * @since 0.1.0 |
| 129 | + * |
| 130 | + * @return null Return early if no settings page is registered. |
| 131 | + */ |
| 132 | + public function enqueue_admin_scripts() { |
| 133 | + |
| 134 | + if ( ! isset( $this->plugin_screen_hook_suffix ) ) { |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + $screen = get_current_screen(); |
| 139 | + if ( $this->plugin_screen_hook_suffix == $screen->id ) { |
| 140 | + wp_enqueue_script( $this->plugin_slug . '-admin-script', plugins_url( 'assets/js/admin.js', __FILE__ ), array( 'jquery' ), Custom_Featured_Image_Metabox::VERSION ); |
| 141 | + } |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Register the administration menu for this plugin into the WordPress Dashboard menu. |
| 147 | + * |
| 148 | + * @since 0.1.0 |
| 149 | + */ |
| 150 | + public function add_plugin_admin_menu() { |
| 151 | + |
| 152 | + /* |
| 153 | + * Add a settings page for this plugin to the Settings menu. |
| 154 | + * |
| 155 | + * NOTE: Alternative menu locations are available via WordPress administration menu functions. |
| 156 | + * |
| 157 | + * Administration Menus: http://codex.wordpress.org/Administration_Menus |
| 158 | + * |
| 159 | + * @TODO: |
| 160 | + * |
| 161 | + * - Change 'Custom Featured Image Metabox' to the title of your plugin admin page |
| 162 | + * - Change 'Custom Featured Image Metabox' to the text for menu item for the plugin settings page |
| 163 | + * - Change 'manage_options' to the capability you see fit |
| 164 | + * For reference: http://codex.wordpress.org/Roles_and_Capabilities |
| 165 | + */ |
| 166 | + $this->plugin_screen_hook_suffix = add_options_page( |
| 167 | + __( 'Custom Featured Image Metabox', $this->plugin_slug ), |
| 168 | + __( 'Custom Featured Image Metabox', $this->plugin_slug ), |
| 169 | + 'manage_options', |
| 170 | + $this->plugin_slug, |
| 171 | + array( $this, 'display_plugin_admin_page' ) |
| 172 | + ); |
| 173 | + |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Render the settings page for this plugin. |
| 178 | + * |
| 179 | + * @since 0.1.0 |
| 180 | + */ |
| 181 | + public function display_plugin_admin_page() { |
| 182 | + include_once( 'views/admin.php' ); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Add settings action link to the plugins page. |
| 187 | + * |
| 188 | + * @since 0.1.0 |
| 189 | + */ |
| 190 | + public function add_action_links( $links ) { |
| 191 | + |
| 192 | + return array_merge( |
| 193 | + array( |
| 194 | + 'settings' => '<a href="' . admin_url( 'options-general.php?page=' . $this->plugin_slug ) . '">' . __( 'Settings', $this->plugin_slug ) . '</a>' |
| 195 | + ), |
| 196 | + $links |
| 197 | + ); |
| 198 | + |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * NOTE: Actions are points in the execution of a page or process |
| 203 | + * lifecycle that WordPress fires. |
| 204 | + * |
| 205 | + * Actions: http://codex.wordpress.org/Plugin_API#Actions |
| 206 | + * Reference: http://codex.wordpress.org/Plugin_API/Action_Reference |
| 207 | + * |
| 208 | + * @since 0.1.0 |
| 209 | + */ |
| 210 | + public function action_method_name() { |
| 211 | + // @TODO: Define your action hook callback here |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * NOTE: Filters are points of execution in which WordPress modifies data |
| 216 | + * before saving it or sending it to the browser. |
| 217 | + * |
| 218 | + * Filters: http://codex.wordpress.org/Plugin_API#Filters |
| 219 | + * Reference: http://codex.wordpress.org/Plugin_API/Filter_Reference |
| 220 | + * |
| 221 | + * @since 0.1.0 |
| 222 | + */ |
| 223 | + public function filter_method_name() { |
| 224 | + // @TODO: Define your filter hook callback here |
| 225 | + } |
| 226 | + |
| 227 | +} |
0 commit comments