Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 21a6fcb

Browse files
committed
Create controller class
1 parent 1359372 commit 21a6fcb

3 files changed

Lines changed: 119 additions & 75 deletions

File tree

functionality.php

Lines changed: 25 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -30,96 +30,46 @@
3030
*/
3131

3232
/**
33-
* Create an instance of the class
34-
*
35-
* @since 1.0
36-
* @uses apply_filters() to allow changing of the filename without hacking
37-
* @return Functionality_Plugin
33+
* Enable autoloading of plugin classes
34+
* @param $class_name
3835
*/
39-
function get_functionality_plugin() {
40-
static $plugin;
36+
function functionality_autoload( $class_name ) {
4137

42-
if ( ! isset( $plugin ) ) {
43-
require_once dirname( __FILE__ ) . '/class-functionality-plugin.php';
44-
$filename = apply_filters( 'functionality_plugin_filename', 'functions.php' );
45-
$plugin = new Functionality_Plugin( $filename );
38+
/* Only autoload classes from this plugin */
39+
if ( 'Functionality' !== substr( $class_name, 0, 13 ) ) {
40+
return;
4641
}
4742

48-
return $plugin;
49-
}
50-
51-
/**
52-
* Add a link to edit the functionality plugin
53-
* to the Plugins admin menu for easy access
54-
*
55-
* @since 1.0
56-
* @uses add_plugins_page() To register the new submenu page
57-
*/
58-
function functionality_plugin_admin_menu() {
59-
$functionality = get_functionality_plugin();
60-
$plugin_file = $functionality->get_plugin_filename();
43+
/* Remove namespace from class name */
44+
$class_file = str_replace( 'Functionality_', '', $class_name );
6145

62-
add_plugins_page(
63-
__( 'Edit Functions', 'functionality' ),
64-
__( 'Edit Functions', 'functionality' ),
65-
'edit_plugins',
66-
add_query_arg( 'file', $plugin_file, 'plugin-editor.php' )
67-
);
68-
}
46+
/* Convert class name format to file name format */
47+
$class_file = strtolower( $class_file );
48+
$class_file = str_replace( '_', '-', $class_file );
6949

70-
add_action( 'admin_menu', 'functionality_plugin_admin_menu' );
7150

72-
/**
73-
* Callback runs when this plugin is activated
74-
*
75-
* @since 1.1
76-
*/
77-
function functionality_plugin_activate() {
78-
add_option( 'functionality_plugin_activated', true );
51+
/* Load the class */
52+
require_once dirname( __FILE__ ) . "/php/class-{$class_file}.php";
7953
}
8054

81-
register_activation_hook( __FILE__, 'functionality_plugin_activate' );
55+
spl_autoload_register( 'functionality_autoload' );
56+
8257

8358
/**
84-
* Create and activate the functionality plugin
85-
* after this plugin is activated
59+
* Create an instance of the class
8660
*
87-
* @since 1.1
88-
* @uses activate_plugin() to activate the functionality plugin
61+
* @since 1.0
62+
* @uses apply_filters() to allow changing of the filename without hacking
63+
* @return Functionality_Controller
8964
*/
90-
function create_functionality_plugin() {
91-
92-
/* Check if this plugin has just been activated */
93-
if ( ! get_option( 'functionality_plugin_activated', false ) ) {
94-
return;
95-
}
96-
97-
delete_option( 'functionality_plugin_activated' );
98-
99-
$functionality = get_functionality_plugin();
65+
function functionality() {
66+
static $controller;
10067

101-
/* Create the plugin */
102-
$functionality->create_plugin();
103-
104-
/* Activate the plugin */
105-
$plugin = $functionality->get_plugin_filename();
106-
107-
if ( ! function_exists( 'activate_plugin' ) ) {
108-
require_once ABSPATH . '/wp-admin/includes/plugin.php';
68+
if ( ! isset( $controller ) ) {
69+
$controller = new Functionality_Controller();
10970
}
11071

111-
activate_plugin( $plugin );
112-
}
113-
114-
add_action( 'init', 'create_functionality_plugin' );
115-
116-
/**
117-
* Load the plugin textdomain
118-
*
119-
* @since 1.1
120-
*/
121-
function load_functionality_textdomain() {
122-
load_plugin_textdomain( 'functionality', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
72+
return $controller;
12373
}
12474

125-
add_action( 'plugins_loaded', 'load_functionality_textdomain' );
75+
functionality()->run();

php/class-controller.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
4+
/**
5+
* Class Functionality_Controller
6+
*/
7+
class Functionality_Controller {
8+
9+
/**
10+
* @var Functionality_Plugin
11+
*/
12+
public $plugin;
13+
14+
/**
15+
* Class constructor
16+
*/
17+
public function __construct() {
18+
$filename = apply_filters( 'functionality_plugin_filename', 'functions.php' );
19+
20+
$this->plugin = new Functionality_Plugin( $filename );
21+
}
22+
23+
public function run() {
24+
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
25+
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
26+
add_action( 'init', array( $this, 'create_plugin' ) );
27+
register_activation_hook( __FILE__, array( $this, 'activation_hook') );
28+
}
29+
30+
/**
31+
* Add a link to edit the functionality plugin
32+
* to the Plugins admin menu for easy access
33+
*
34+
* @since 1.0
35+
* @uses add_plugins_page() To register the new submenu page
36+
*/
37+
public function admin_menu() {
38+
$plugin_file = $this->plugin->get_plugin_filename();
39+
40+
add_plugins_page(
41+
__( 'Edit Functions', 'functionality' ),
42+
__( 'Edit Functions', 'functionality' ),
43+
'edit_plugins',
44+
add_query_arg( 'file', $plugin_file, 'plugin-editor.php' )
45+
);
46+
}
47+
48+
/**
49+
* Callback runs when this plugin is activated
50+
*
51+
* @since 1.1
52+
*/
53+
function activation_hook() {
54+
add_option( 'functionality_plugin_activated', true );
55+
}
56+
57+
/**
58+
* Create and activate the functionality plugin
59+
* after this plugin is activated
60+
*
61+
* @since 1.1
62+
* @uses activate_plugin() to activate the functionality plugin
63+
*/
64+
function create_plugin() {
65+
66+
/* Check if this plugin has just been activated */
67+
if ( ! get_option( 'functionality_plugin_activated', false ) ) {
68+
return;
69+
}
70+
71+
/* Create the plugin */
72+
$this->plugin->create_plugin();
73+
74+
/* Activate the plugin */
75+
if ( ! function_exists( 'activate_plugin' ) ) {
76+
require_once ABSPATH . '/wp-admin/includes/plugin.php';
77+
}
78+
79+
$filename = $this->plugin->get_plugin_filename();
80+
activate_plugin( $filename );
81+
82+
delete_option( 'functionality_plugin_activated' );
83+
}
84+
85+
/**
86+
* Load the plugin textdomain
87+
*
88+
* @since 1.1
89+
*/
90+
function load_textdomain() {
91+
load_plugin_textdomain( 'functionality', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
92+
}
93+
}
94+

0 commit comments

Comments
 (0)