This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
media-modal-demo.php
286 lines (252 loc) · 6.76 KB
/
media-modal-demo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/**
* Plugin Name: Media Modal Demo
* Version: 1.0-beta
* Description: Adds an options page, where the new Media Modal can be used to get attachment details
* Author: Dominik Schilling
* Author URI: http://wphelper.de/
* Plugin URI: http://wpgrafie.de/
*
* License: GPLv2 or later
*
* Copyright (C) 2013 Dominik Schilling
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* Don't call this file directly.
*/
if ( ! class_exists( 'WP' ) ) {
die();
}
/**
* The base class.
*/
class Media_Modal_Demo {
/**
* Stores the class instance.
*
* @var Media_Modal_Demo
*/
private static $instance = null;
/**
* Stores the current demo page number.
*
* @var integer
*/
private $current_demo_page = 1;
/**
* Stores the class instance of the current demo
*
* @var Media_Modal_Demo_Page_Abstract
*/
private $demo_page_instance = null;
/**
* Stores the hook suffix of the screen.
*
* @var string
*/
private $screen_id = '';
/**
* Stores the classes which will be autoloaded.
*
* Class name => path to class, relative to __FILE__
*
* @var array
*/
public static $demo_classes = array(
'Media_Modal_Demo_Page_Abstract' => 'demos',
'Media_Modal_Demo_Page_1' => 'demos/demo-1',
'Media_Modal_Demo_Page_2' => 'demos/demo-2',
'Media_Modal_Demo_Page_3' => 'demos/demo-3',
'Media_Modal_Demo_Page_4' => 'demos/demo-4',
'Media_Modal_Demo_Page_5' => 'demos/demo-5',
);
/**
* Returns the instance of this class.
*
* It's a singleton class.
*
* @return Media_Modal_Demo The instance
*/
public static function get_instance() {
if ( ! self::$instance )
self::$instance = new self;
return self::$instance;
}
/**
* Initialises the plugin.
*
* Returns false if not in admin. Sets the spl_autoload_register
* callback.
*/
public function init_plugin() {
if ( ! is_admin() )
return false;
spl_autoload_register( 'Media_Modal_Demo::autoloader' );
$this->set_current_demo_page_number();
$this->set_demo_page_instance();
$this->init_hooks();
}
/**
* Sets the current demo page number either by argument
* or by the value of $_GET[ 'demo' ].
*
* @param integer $page The demo page number
*/
public function set_current_demo_page_number( $page = null ) {
if ( ! empty( $page ) )
$this->current_demo_page_number = $page;
else
$this->current_demo_page_number = ! empty( $_GET[ 'demo' ] ) ? (int) $_GET[ 'demo' ] : 1;
}
/**
* Returns the current demo page number.
*
* @return integer The demo page number
*/
public function get_current_demo_page_number() {
return $this->current_demo_page_number;
}
/**
* Sets the instance of the current demo page class. Either
* by argument or $current_demo_page.
* Dies if class doesn't exist.
*
* @param string $class A custom class name
*/
public function set_demo_page_instance( $class = null ) {
if ( empty( $class ) ) {
$class = sprintf(
'Media_Modal_Demo_Page_%d',
$this->current_demo_page_number
);
}
if ( class_exists( $class ) )
$this->demo_page_instance = new $class;
else
wp_die( "Sorry, but this demo doesn't exist!" );
}
/**
* Returns the instance of the current demo page class.
*
* @return Media_Modal_Demo_Page_Abstract The instance of the class
*/
public function get_demo_page_instance() {
return $this->demo_page_instance;
}
/**
* Initialises the WP actions.
* - admin_menu
* - admin_print_scripts
* - admin_print_styles
*
* Initialises also the hooks of the demo page class.
*/
private function init_hooks() {
add_action( 'admin_menu', array( $this, 'add_plugins_page' ) );
add_action( 'admin_print_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'admin_print_styles', array( $this, 'enqueue_styles' ) );
$this->demo_page_instance->init_hooks();
}
/**
* Adds a menu entry for this plugin.
*/
public function add_plugins_page() {
$this->screen_id = add_plugins_page(
'Media Modal Demo',
'Media Modal Demo',
'manage_options',
'media-modal-demo',
array( $this, 'render_page' )
);
}
/**
* Enqueues the scripts and calls wp_enqueue_media().
*
* Calls also the enqueue method of the demo page class.
*/
public function enqueue_scripts() {
if ( ! isset( get_current_screen()->id ) || get_current_screen()->id != $this->screen_id )
return;
wp_enqueue_media();
$this->demo_page_instance->enqueue_scripts();
}
/**
* Enqueues the styles.
*
* Calls also the enqueue method of the demo page class.
*/
public function enqueue_styles() {
if ( ! isset( get_current_screen()->id ) || get_current_screen()->id != $this->screen_id )
return;
wp_enqueue_style(
'media-modal-demo',
plugins_url( 'css/media-modal-demo.css', __FILE__ ),
array( 'media-views' )
);
$this->demo_page_instance->enqueue_styles();
}
/**
* Renders the page.
*
* Calls also the methods of the demo page class.
*/
public function render_page() {
?>
<div id="media-modal-demo" class="wrap">
<h2>Media Modal Demo</h2>
<h3 class="nav-tab-wrapper">
<?php
$max = count( self::$demo_classes ) - 1; // Exclude the abstract class
for( $i = 1; $i <= $max; $i++ ) {
printf(
'<a href="%s" class="nav-tab%s">%s</a>',
esc_url( add_query_arg( 'demo', $i ) ),
$this->current_demo_page_number == $i ? ' nav-tab-active' : '',
"Demo $i"
);
}
?>
</h3>
<div id="media-modal-demo-description">
<?php $this->demo_page_instance->print_description(); ?>
</div>
<div id="media-modal-demo-content">
<?php $this->demo_page_instance->render_content(); ?>
</div>
</div>
<?php
}
/**
* The callback function for the autoloader.
*
* @param string $class The class name
*/
public static function autoloader( $class ) {
if ( in_array( $class, array_keys( self::$demo_classes ) ) ) {
require_once(
sprintf(
'%s/%s/class-%s.php',
dirname( __FILE__ ),
self::$demo_classes[ $class ],
strtolower( str_replace( '_', '-', $class ) )
)
);
}
}
}
// We are now ready for take off
add_action( 'init', array( Media_Modal_Demo::get_instance(), 'init_plugin' ), 20 );