PluginProbe
FakerPress / 0.1.0
FakerPress v0.1.0
0.9.2 trunk 0.1.0 0.1.1 0.1.2 0.1.3 0.5.2 0.5.3 0.6.1 0.6.2 0.6.3 0.6.4 0.6.5 0.6.6 0.7.0 0.7.1 0.7.2 0.8.0 0.9.0 0.9.1
fakerpress / classes / admin.php

admin.php in FakerPress 0.1.0, at classes/admin.php

301 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace FakerPress;
3
4 Class Admin {
5 /**
6 * Variable holding the submenus objects
7 * @var array
8 */
9 protected static $menus = array();
10
11 /**
12 * Variable holding the submenus objects
13 * @var object
14 */
15 public static $view = null;
16
17 /**
18 * Static method to include all the Hooks for WordPress
19 * There is a safe conditional here, it can only be triggered once!
20 *
21 * @uses add_action
22 * @uses add_filter
23 *
24 * @since 0.1.0
25 *
26 * @return null Construct never returns
27 */
28 public function __construct(){
29 $this->control['posts'] = new Control\Posts;
30 $this->control['terms'] = new Control\Terms;
31 $this->control['users'] = new Control\Users;
32 $this->control['comments'] = new Control\Comments;
33
34 add_action( 'admin_init', array( $this, '_action_set_admin_view' ) );
35
36 // Add needs to come before `admin_menu`
37 add_action( 'init', array( $this, '_add_core_submenus' ) );
38
39 // When trying to add a menu, make bigger than the default to avoid conflicting index further on
40 add_action( 'admin_menu', array( $this, '_action_admin_menu' ), 11 );
41 add_action( 'fakerpress.view.start', array( $this, '_action_current_menu_js' ) );
42
43 self::$menus[] = (object) array(
44 'view' => 'settings',
45 'title' => esc_attr__( 'Settings', 'fakerpress' ),
46 'label' => esc_attr__( 'FakerPress', 'fakerpress' ),
47 'capability' => 'manage_options',
48 'priority' => 0,
49 );
50
51 // Creating information for the plugin pages footer
52 add_filter( 'admin_footer_text', array( $this, '_filter_admin_footer_text' ) );
53 add_filter( 'update_footer', array( $this, '_filter_update_footer' ), 15 );
54
55 add_filter( 'fakerpress.view', array( $this, '_filter_set_view_title' ) );
56 add_filter( 'fakerpress.view', array( $this, '_filter_set_view_action' ) );
57
58 add_action( 'admin_enqueue_scripts', array( $this, '_action_enqueue_ui' ) );
59 }
60
61 /**
62 * Creating submenus easier to be extent from outside
63 *
64 * @param string $view The slug of the View, _GET param
65 * @param string $title Translatable string for the title of the page
66 * @param string $label Translatable string that will go on the menu
67 * @param string $capability WordPress capability that will be required to access this view
68 * @param integer $priority The priority to show this submenu
69 */
70 public static function add_menu( $view, $title, $label, $capability = 'manage_options', $priority = 10 ){
71 $priority = absint( $priority );
72 self::$menus[] = (object) array(
73 'view' => sanitize_title( $view ),
74 'title' => esc_attr( $title ),
75 'label' => esc_attr( $label ),
76 'capability' => sanitize_title( $capability ),
77 'priority' => $priority === 0 ? $priority + 1 : $priority,
78 );
79
80 usort( self::$menus, '\FakerPress\Admin::_sort_priority' );
81 }
82
83 public static function _sort_priority( $a, $b ){
84 return $a->priority - $b->priority;
85 }
86
87 public function _action_current_menu_js( $view ) {
88 ?>
89 <script>
90 (function($){
91 'use strict';
92 var fp = typeof FakerPress === 'object' ? window.FakerPress : {};
93
94 fp.view = {
95 name: '<?php echo esc_attr( $view->slug ); ?>',
96 default: '<?php echo esc_attr( self::$menus[0]->view ); ?>'
97 };
98
99 fp.menu = {
100 $container: $('#toplevel_page_fakerpress')
101 };
102
103 fp.menu.$main = fp.menu.$container.children('a');
104 fp.menu.$items = fp.menu.$container.children('.wp-submenu').children('li').not('.wp-submenu-head');
105 fp.menu.$current = fp.menu.$items.filter('.current');
106
107 if ( fp.view.default !== fp.view.name ){
108 fp.menu.$current = fp.menu.$items.children('a').filter('[href="admin.php?page=fakerpress&view=' + fp.view.name + '"]');
109 fp.menu.$items.filter('.current').removeClass('current');
110 fp.menu.$current.parent().addClass('current');
111 }
112 }(jQuery));
113 </script>
114 <?php
115 }
116
117 public function _action_set_admin_view(){
118 // Default Page of the plugin
119 $view = (object) array(
120 'slug' => Filter::super( INPUT_GET, 'view', 'file', self::$menus[0]->view ),
121 'path' => null,
122 );
123
124 // First we check if the file exists in our plugin folder, otherwhise give the user an error
125 if ( ! file_exists( Plugin::path( "view/{$view->slug}.php" ) ) ){
126 $view->slug = 'error';
127 }
128
129 // Define the path for the view we
130 $view->path = Plugin::path( "view/{$view->slug}.php" );
131
132 // Set the Admin::$view
133 self::$view = apply_filters( 'fakerpress.view', $view );
134 }
135
136 /**
137 * Method triggered to add the menu to WordPress administration
138 *
139 * @uses add_menu_page
140 * @uses __
141 * @uses \FakerPress\Plugin::$slug
142 *
143 * @since 0.1.0
144 * @return null Actions do not return
145 */
146 public function _action_admin_menu() {
147 foreach ( self::$menus as &$menu ) {
148 if ( $menu->priority === 0 ) {
149 $menu->hook = add_menu_page( $menu->title, $menu->label, $menu->capability, Plugin::$slug, array( &$this, '_include_settings_page' ), 'none' );
150 } else {
151 $menu->hook = add_submenu_page( Plugin::$slug, $menu->title, $menu->label, $menu->capability, Plugin::$slug . '&view=' . $menu->view, array( &$this, '_include_settings_page' ) );
152 }
153 }
154
155 // Change the Default Submenu for FakerPress menus
156 $GLOBALS['submenu'][ Plugin::$slug ][0][0] = esc_attr__( 'Settings', 'FakerPress' );
157 }
158
159 /**
160 *
161 */
162 public function _add_core_submenus(){
163 self::add_menu( 'users', __( 'Users', 'fakerpress' ), __( 'Users', 'fakerpress' ), 'manage_options', 10 );
164 self::add_menu( 'terms', __( 'Terms', 'fakerpress' ), __( 'Terms', 'fakerpress' ), 'manage_options', 10 );
165 self::add_menu( 'posts', __( 'Posts', 'fakerpress' ), __( 'Posts', 'fakerpress' ), 'manage_options', 10 );
166 self::add_menu( 'comments', __( 'Comments', 'fakerpress' ), __( 'Comments', 'fakerpress' ), 'manage_options', 10 );
167 }
168
169 /**
170 * Register and enqueue the WordPress admin UI elements like JavaScript and CSS
171 *
172 * @uses wp_register_style
173 * @uses wp_enqueue_style
174 * @uses \FakerPress\Plugin::url
175 * @uses \FakerPress\Plugin::version
176 *
177 * @since 0.1.0
178 *
179 * @return null Actions do not return
180 */
181 public function _action_enqueue_ui() {
182 wp_register_style( 'fakerpress.icon', Plugin::url( 'ui/font.css' ), array(), Plugin::version, 'screen' );
183
184 wp_enqueue_style( 'fakerpress.icon' );
185 }
186
187 /**
188 * Method to include the settings page, from views folders
189 *
190 * @uses \FakerPress\Filter::super
191 * @uses \FakerPress\Plugin::path
192 * @uses do_action
193 *
194 * @since 0.1.0
195 * @return null
196 */
197 public function _include_settings_page() {
198 $view = self::$view;
199
200 // Execute some actions before including the view, to allow others to hook in here
201 // Use these to do stuff related to the view you are working with
202 do_action( 'fakerpress.view.start', self::$view );
203 do_action( "fakerpress.view.start.{$view->slug}", self::$view );
204
205 // PHP include the view
206 include_once self::$view->path;
207
208 // Execute some actions before including the view, to allow others to hook in here
209 // Use these to do stuff related to the view you are working with
210 do_action( 'fakerpress.view.end', self::$view );
211 do_action( "fakerpress.view.end.{$view->slug}", self::$view );
212 }
213
214
215 public function _filter_set_view_action( $view ){
216 $view->action = Filter::super( INPUT_GET, 'action', FILTER_SANITIZE_STRING );
217
218 return $view;
219 }
220
221 public function _filter_set_view_title( $view ){
222 foreach ( self::$menus as $menu ){
223 if ( $view->slug !== $menu->view ){
224 continue;
225 }
226 $view->title = $menu->title;
227 }
228
229 switch ( $view->slug ) {
230 case 'changelog':
231 $view->title = esc_attr__( 'Changelog', 'fakerpress' );
232 break;
233
234 case 'error':
235 $view->title = esc_attr__( 'Not Found (404)', 'fakerpress' );
236 break;
237 }
238
239 add_filter( 'admin_title', array( $this, '_filter_set_admin_page_title' ), 15, 2 );
240
241 return $view;
242 }
243
244 public function _filter_set_admin_page_title( $admin_title, $title ){
245 $pos = strpos( $admin_title, $title );
246 if ( $pos !== false ) {
247 $admin_title = substr_replace( $admin_title, sprintf( apply_filters( 'fakerpress.admin_title_base', __( '%s on FakerPress', 'fakerpress' ) ), self::$view->title ), $pos, strlen( $title ) );
248 }
249 return $admin_title;
250 }
251
252 /**
253 * Filter the WordPress Version on plugins pages to display plugin version
254 *
255 * @uses \FakerPress\Filter::super
256 * @uses \FakerPress\Plugin::$slug
257 * @uses __
258 *
259 * @since 0.1.0
260 * @return string
261 */
262 public function _filter_admin_footer_text( $text ){
263 $page = Filter::super( INPUT_GET, 'page', FILTER_SANITIZE_STRING );
264 if ( is_null( $page ) || strtolower( $page ) !== Plugin::$slug ){
265 return $text;
266 }
267
268 /**
269 * @todo Review the links to the Official repository before release
270 */
271 return
272 '<a target="_blank" href="http://wordpress.org/support/plugin/fakerpress#postform">' . __( 'Contact Support', 'fakerpress' ) . '</a>' .
273 ' | ' .
274 str_replace(
275 array( '[stars]', '[wp.org]' ),
276 array( '<a target="_blank" href="http://wordpress.org/support/view/plugin-reviews/fakerpress#postform" >&#9733;&#9733;&#9733;&#9733;&#9733;</a>', '<a target="_blank" href="http://wordpress.org/plugins/fakerpress/" >wordpress.org</a>' ),
277 __( 'Add your [stars] on [wp.org] to spread the love.', 'fakerpress' )
278 );
279 }
280
281 /**
282 * Filter the WordPress Version on plugins pages to display the plugin version
283 *
284 * @uses \FakerPress\Filter::super
285 * @uses \FakerPress\Plugin::$slug
286 * @uses \FakerPress\Plugin::admin_url
287 * @uses \FakerPress\Plugin::version
288 * @uses __
289 *
290 * @since 0.1.0
291 * @return string
292 */
293 public function _filter_update_footer( $text ){
294 $page = Filter::super( INPUT_GET, 'page', FILTER_SANITIZE_STRING );
295 if ( is_null( $page ) || strtolower( $page ) !== Plugin::$slug ){
296 return $text;
297 }
298
299 return __( 'Version' ) . ': ' . '<a title="' . __( 'View what changed in this version', 'fakerpress' ) . '" href="' . Plugin::admin_url( 'view=changelog&version=' . Plugin::version ) . '">' . Plugin::version . '</a>';
300 }
301 }