PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/wordpress/wp-content/plugins/wp-pagenavi/scb/AdminPage.php

https://bitbucket.org/gfelizola/pacaembu-institucional
PHP | 439 lines | 267 code | 108 blank | 64 comment | 42 complexity | a273c5c474796c874ab1ee0cb79bb86c MD5 | raw file
  1. <?php
  2. // Administration page base class
  3. abstract class scbAdminPage {
  4. /** Page args
  5. * $page_title string (mandatory)
  6. * $parent (string) (default: options-general.php)
  7. * $capability (string) (default: 'manage_options')
  8. * $menu_title (string) (default: $page_title)
  9. * $page_slug (string) (default: sanitized $page_title)
  10. * $toplevel (string) If not empty, will create a new top level menu (for expected values see http://codex.wordpress.org/Administration_Menus#Using_add_submenu_page)
  11. * - $icon_url (string) URL to an icon for the top level menu
  12. * - $position (int) Position of the toplevel menu (caution!)
  13. * $screen_icon (string) The icon type to use in the screen header
  14. * $nonce string (default: $page_slug)
  15. * $action_link (string|bool) Text of the action link on the Plugins page (default: 'Settings')
  16. * $admin_action_priority int The priority that the admin_menu action should be executed at (default: 10)
  17. */
  18. protected $args;
  19. // URL to the current plugin directory.
  20. // Useful for adding css and js files
  21. protected $plugin_url;
  22. // Created at page init
  23. protected $pagehook;
  24. // scbOptions object holder
  25. // Normally, it's used for storing formdata
  26. protected $options;
  27. protected $option_name;
  28. // l10n
  29. protected $textdomain;
  30. // ____________REGISTRATION COMPONENT____________
  31. private static $registered = array();
  32. static function register( $class, $file, $options = null ) {
  33. if ( isset( self::$registered[$class] ) )
  34. return false;
  35. self::$registered[$class] = array( $file, $options );
  36. add_action( '_admin_menu', array( __CLASS__, '_pages_init' ) );
  37. return true;
  38. }
  39. static function replace( $old_class, $new_class ) {
  40. if ( ! isset( self::$registered[$old_class] ) )
  41. return false;
  42. self::$registered[$new_class] = self::$registered[$old_class];
  43. unset( self::$registered[$old_class] );
  44. return true;
  45. }
  46. static function remove( $class ) {
  47. if ( ! isset( self::$registered[$class] ) )
  48. return false;
  49. unset( self::$registered[$class] );
  50. return true;
  51. }
  52. static function _pages_init() {
  53. foreach ( self::$registered as $class => $args )
  54. new $class( $args[0], $args[1] );
  55. }
  56. // ____________MAIN METHODS____________
  57. // Constructor
  58. function __construct( $file = false, $options = null ) {
  59. if ( is_a( $options, 'scbOptions' ) )
  60. $this->options = $options;
  61. $this->setup();
  62. $this->check_args();
  63. if ( isset( $this->option_name ) ) {
  64. add_action( 'admin_init', array( $this, 'option_init' ) );
  65. if ( function_exists( 'settings_errors' ) )
  66. add_action( 'admin_notices', 'settings_errors' );
  67. }
  68. add_action( 'admin_menu', array( $this, 'page_init' ), $this->args['admin_action_priority'] );
  69. add_filter( 'contextual_help', array( $this, '_contextual_help' ), 10, 2 );
  70. if ( $file ) {
  71. $this->file = $file;
  72. $this->plugin_url = plugin_dir_url( $file );
  73. if ( $this->args['action_link'] )
  74. add_filter( 'plugin_action_links_' . plugin_basename( $file ), array( $this, '_action_link' ) );
  75. }
  76. }
  77. // This is where all the page args can be set
  78. function setup(){}
  79. /**
  80. * Called when the page is loaded, but before any rendering.
  81. *
  82. * Useful for calling $screen->add_help_tab() etc.
  83. */
  84. function page_loaded() {}
  85. // This is where the css and js go
  86. // Both wp_enqueue_*() and inline code can be added
  87. function page_head(){}
  88. // This is where the contextual help goes
  89. // @return string
  90. function page_help(){}
  91. // A generic page header
  92. function page_header() {
  93. echo "<div class='wrap'>\n";
  94. screen_icon( $this->args['screen_icon'] );
  95. echo html( "h2", $this->args['page_title'] );
  96. }
  97. // This is where the page content goes
  98. abstract function page_content();
  99. // A generic page footer
  100. function page_footer() {
  101. echo "</div>\n";
  102. }
  103. // This is where the form data should be validated
  104. function validate( $new_data, $old_data ) {
  105. return $new_data;
  106. }
  107. // Manually handle option saving ( use Settings API instead )
  108. function form_handler() {
  109. if ( empty( $_POST['action'] ) )
  110. return false;
  111. check_admin_referer( $this->nonce );
  112. if ( !isset($this->options) ) {
  113. trigger_error('options handler not set', E_USER_WARNING);
  114. return false;
  115. }
  116. $new_data = wp_array_slice_assoc( $_POST, array_keys( $this->options->get_defaults() ) );
  117. $new_data = stripslashes_deep( $new_data );
  118. $new_data = $this->validate( $new_data, $this->options->get() );
  119. $this->options->set( $new_data );
  120. $this->admin_msg();
  121. }
  122. // Manually generate a standard admin notice ( use Settings API instead )
  123. function admin_msg( $msg = '', $class = "updated" ) {
  124. if ( empty( $msg ) )
  125. $msg = __( 'Settings <strong>saved</strong>.', $this->textdomain );
  126. echo scb_admin_notice( $msg, $class );
  127. }
  128. // ____________UTILITIES____________
  129. // Generates a form submit button
  130. function submit_button( $value = '', $action = 'action', $class = "button" ) {
  131. if ( is_array( $value ) ) {
  132. extract( wp_parse_args( $value, array(
  133. 'value' => __( 'Save Changes', $this->textdomain ),
  134. 'action' => 'action',
  135. 'class' => 'button',
  136. 'ajax' => true
  137. ) ) );
  138. if ( ! $ajax )
  139. $class .= ' no-ajax';
  140. }
  141. else {
  142. if ( empty( $value ) )
  143. $value = __( 'Save Changes', $this->textdomain );
  144. }
  145. $input_args = array(
  146. 'type' => 'submit',
  147. 'name' => $action,
  148. 'value' => $value,
  149. 'extra' => '',
  150. 'desc' => false,
  151. 'wrap' => html( 'p class="submit"', scbForms::TOKEN )
  152. );
  153. if ( ! empty( $class ) )
  154. $input_args['extra'] = compact( 'class' );
  155. return scbForms::input( $input_args );
  156. }
  157. /*
  158. Mimics scbForms::form_wrap()
  159. $this->form_wrap( $content ); // generates a form with a default submit button
  160. $this->form_wrap( $content, false ); // generates a form with no submit button
  161. // the second argument is sent to submit_button()
  162. $this->form_wrap( $content, array( 'text' => 'Save changes',
  163. 'name' => 'action',
  164. 'ajax' => true,
  165. ) );
  166. */
  167. function form_wrap( $content, $submit_button = true ) {
  168. if ( is_array( $submit_button ) ) {
  169. $content .= $this->submit_button( $submit_button );
  170. } elseif ( true === $submit_button ) {
  171. $content .= $this->submit_button();
  172. } elseif ( false !== strpos( $submit_button, '<input' ) ) {
  173. $content .= $submit_button;
  174. } elseif ( false !== $submit_button ) {
  175. $button_args = array_slice( func_get_args(), 1 );
  176. $content .= call_user_func_array( array( $this, 'submit_button' ), $button_args );
  177. }
  178. return scbForms::form_wrap( $content, $this->nonce );
  179. }
  180. // Generates a table wrapped in a form
  181. function form_table( $rows, $formdata = false ) {
  182. $output = '';
  183. foreach ( $rows as $row )
  184. $output .= $this->table_row( $row, $formdata );
  185. $output = $this->form_table_wrap( $output );
  186. return $output;
  187. }
  188. // Wraps the given content in a <form><table>
  189. function form_table_wrap( $content ) {
  190. $output = $this->table_wrap( $content );
  191. $output = $this->form_wrap( $output );
  192. return $output;
  193. }
  194. // Generates a form table
  195. function table( $rows, $formdata = false ) {
  196. $output = '';
  197. foreach ( $rows as $row )
  198. $output .= $this->table_row( $row, $formdata );
  199. $output = $this->table_wrap( $output );
  200. return $output;
  201. }
  202. // Generates a table row
  203. function table_row( $args, $formdata = false ) {
  204. return $this->row_wrap( $args['title'], $this->input( $args, $formdata ) );
  205. }
  206. // Mimic scbForms inheritance
  207. function __call( $method, $args ) {
  208. if ( in_array( $method, array( 'input', 'form' ) ) ) {
  209. if ( empty( $args[1] ) && isset( $this->options ) )
  210. $args[1] = $this->options->get();
  211. if ( 'form' == $method )
  212. $args[2] = $this->nonce;
  213. }
  214. return call_user_func_array( array( 'scbForms', $method ), $args );
  215. }
  216. // Wraps a string in a <script> tag
  217. function js_wrap( $string ) {
  218. return html( "script type='text/javascript'", $string );
  219. }
  220. // Wraps a string in a <style> tag
  221. function css_wrap( $string ) {
  222. return html( "style type='text/css'", $string );
  223. }
  224. // ____________INTERNAL METHODS____________
  225. // Registers a page
  226. function page_init() {
  227. extract( $this->args );
  228. if ( ! $toplevel ) {
  229. $this->pagehook = add_submenu_page( $parent, $page_title, $menu_title, $capability, $page_slug, array( $this, '_page_content_hook' ) );
  230. } else {
  231. $func = 'add_' . $toplevel . '_page';
  232. $this->pagehook = $func( $page_title, $menu_title, $capability, $page_slug, array( $this, '_page_content_hook' ), $icon_url, $position );
  233. }
  234. if ( ! $this->pagehook )
  235. return;
  236. add_action( 'load-' . $this->pagehook, array( $this, 'page_loaded' ) );
  237. if ( $ajax_submit ) {
  238. $this->ajax_response();
  239. add_action( 'admin_footer', array( $this, 'ajax_submit' ), 20 );
  240. }
  241. add_action( 'admin_print_styles-' . $this->pagehook, array( $this, 'page_head' ) );
  242. }
  243. function option_init() {
  244. register_setting( $this->option_name, $this->option_name, array( $this, 'validate' ) );
  245. }
  246. private function check_args() {
  247. if ( empty( $this->args['page_title'] ) )
  248. trigger_error( 'Page title cannot be empty', E_USER_WARNING );
  249. $this->args = wp_parse_args( $this->args, array(
  250. 'toplevel' => '',
  251. 'position' => null,
  252. 'icon_url' => '',
  253. 'screen_icon' => '',
  254. 'parent' => 'options-general.php',
  255. 'capability' => 'manage_options',
  256. 'menu_title' => $this->args['page_title'],
  257. 'page_slug' => '',
  258. 'nonce' => '',
  259. 'action_link' => __( 'Settings', $this->textdomain ),
  260. 'ajax_submit' => false,
  261. 'admin_action_priority' => 10,
  262. ) );
  263. if ( empty( $this->args['page_slug'] ) )
  264. $this->args['page_slug'] = sanitize_title_with_dashes( $this->args['menu_title'] );
  265. if ( empty( $this->args['nonce'] ) )
  266. $this->nonce = $this->args['page_slug'];
  267. }
  268. function _contextual_help( $help, $screen ) {
  269. if ( is_object( $screen ) )
  270. $screen = $screen->id;
  271. $actual_help = $this->page_help();
  272. if ( $screen == $this->pagehook && $actual_help )
  273. return $actual_help;
  274. return $help;
  275. }
  276. function ajax_response() {
  277. if ( ! isset( $_POST['_ajax_submit'] ) || $_POST['_ajax_submit'] != $this->pagehook )
  278. return;
  279. $this->form_handler();
  280. die;
  281. }
  282. function ajax_submit() {
  283. global $page_hook;
  284. if ( $page_hook != $this->pagehook )
  285. return;
  286. ?>
  287. <script type="text/javascript">
  288. jQuery( document ).ready( function( $ ){
  289. var $spinner = $( new Image() ).attr( 'src', '<?php echo admin_url( "images/wpspin_light.gif" ); ?>' );
  290. $( ':submit' ).click( function( ev ){
  291. var $submit = $( this );
  292. var $form = $submit.parents( 'form' );
  293. if ( $submit.hasClass( 'no-ajax' ) || $form.attr( 'method' ).toLowerCase() != 'post' )
  294. return true;
  295. var $this_spinner = $spinner.clone();
  296. $submit.before( $this_spinner ).hide();
  297. var data = $form.serializeArray();
  298. data.push( {name: $submit.attr( 'name' ), value: $submit.val()} );
  299. data.push( {name: '_ajax_submit', value: '<?php echo $this->pagehook; ?>'} );
  300. $.post( location.href, data, function( response ){
  301. var $prev = $( '.wrap > .updated, .wrap > .error' );
  302. var $msg = $( response ).hide().insertAfter( $( '.wrap h2' ) );
  303. if ( $prev.length > 0 )
  304. $prev.fadeOut( 'slow', function(){ $msg.fadeIn( 'slow' ); } );
  305. else
  306. $msg.fadeIn( 'slow' );
  307. $this_spinner.hide();
  308. $submit.show();
  309. } );
  310. ev.stopPropagation();
  311. ev.preventDefault();
  312. } );
  313. } );
  314. </script>
  315. <?php
  316. }
  317. function _page_content_hook() {
  318. $this->form_handler();
  319. $this->page_header();
  320. $this->page_content();
  321. $this->page_footer();
  322. }
  323. function _action_link( $links ) {
  324. $url = add_query_arg( 'page', $this->args['page_slug'], admin_url( $this->args['parent'] ) );
  325. $links[] = html_link( $url, $this->args['action_link'] );
  326. return $links;
  327. }
  328. }