NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

BoxesPage.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. /**
  3. * Admin screen with metaboxes base class.
  4. */
  5. abstract class scbBoxesPage extends scbAdminPage {
  6. /*
  7. A box definition looks like this:
  8. array( $slug, $title, $column );
  9. Available columns: normal, side, column3, column4
  10. */
  11. protected $boxes = array();
  12. /**
  13. * Constructor.
  14. *
  15. * @param string|bool $file (optional)
  16. * @param object $options (optional) A scbOptions object.
  17. *
  18. * @return void
  19. */
  20. public function __construct( $file = false, $options = null ) {
  21. parent::__construct( $file, $options );
  22. scbUtil::add_uninstall_hook( $this->file, array( $this, 'uninstall' ) );
  23. }
  24. /**
  25. * Registers a page.
  26. *
  27. * @return void
  28. */
  29. public function page_init() {
  30. if ( ! isset( $this->args['columns'] ) ) {
  31. $this->args['columns'] = 2;
  32. }
  33. parent::page_init();
  34. add_action( 'load-' . $this->pagehook, array( $this, 'boxes_init' ) );
  35. }
  36. /**
  37. * Prints default CSS styles.
  38. *
  39. * @return void
  40. */
  41. protected function default_css() {
  42. ?>
  43. <style type="text/css">
  44. .postbox-container + .postbox-container {
  45. margin-left: 18px;
  46. }
  47. .postbox-container {
  48. padding-right: 0;
  49. }
  50. .inside {
  51. clear: both;
  52. overflow: hidden;
  53. }
  54. .inside table {
  55. margin: 0 !important;
  56. padding: 0 !important;
  57. }
  58. .inside table td {
  59. vertical-align: middle !important;
  60. }
  61. .inside table .regular-text {
  62. width: 100% !important;
  63. }
  64. .inside .form-table th {
  65. width: 30%;
  66. max-width: 200px;
  67. padding: 10px 0 !important;
  68. }
  69. .inside .widefat .check-column {
  70. padding-bottom: 7px !important;
  71. }
  72. .inside p,
  73. .inside table {
  74. margin: 0 0 10px !important;
  75. }
  76. .inside p.submit {
  77. float: left !important;
  78. padding: 0 !important;
  79. margin-bottom: 0 !important;
  80. }
  81. .meta-box-sortables {
  82. min-height: 100px;
  83. width: 100%;
  84. }
  85. </style>
  86. <?php
  87. }
  88. /**
  89. * Displays page content.
  90. *
  91. * @return void
  92. */
  93. protected function page_content() {
  94. $this->default_css();
  95. global $screen_layout_columns;
  96. if ( isset( $screen_layout_columns ) ) {
  97. $hide2 = $hide3 = $hide4 = '';
  98. switch ( $screen_layout_columns ) {
  99. case 4:
  100. if ( ! isset( $this->args['column_widths'] ) ) {
  101. $this->args['column_widths'] = array( 24.5, 24.5, 24.5, 24.5 );
  102. }
  103. break;
  104. case 3:
  105. if ( ! isset( $this->args['column_widths'] ) ) {
  106. $this->args['column_widths'] = array( 32.67, 32.67, 32.67 );
  107. }
  108. $hide4 = 'display:none;';
  109. break;
  110. case 2:
  111. if ( ! isset( $this->args['column_widths'] ) ) {
  112. $this->args['column_widths'] = array( 49, 49 );
  113. }
  114. $hide3 = $hide4 = 'display:none;';
  115. break;
  116. default:
  117. if ( ! isset( $this->args['column_widths'] ) ) {
  118. $this->args['column_widths'] = array( 98 );
  119. }
  120. $hide2 = $hide3 = $hide4 = 'display:none;';
  121. }
  122. $this->args['column_widths'] = array_pad( $this->args['column_widths'], 4, 0 );
  123. }
  124. ?>
  125. <div id='<?php echo $this->pagehook; ?>-widgets' class='metabox-holder'>
  126. <?php
  127. echo "\t<div class='postbox-container' style='width:{$this->args['column_widths'][0]}%'>\n";
  128. do_meta_boxes( $this->pagehook, 'normal', '' );
  129. echo "\t</div><div class='postbox-container' style='width:{$hide2}{$this->args['column_widths'][1]}%'>\n";
  130. do_meta_boxes( $this->pagehook, 'side', '' );
  131. echo "\t</div><div class='postbox-container' style='width:{$hide3}{$this->args['column_widths'][2]}%'>\n";
  132. do_meta_boxes( $this->pagehook, 'column3', '' );
  133. echo "\t</div><div class='postbox-container' style='width:{$hide4}{$this->args['column_widths'][3]}%'>\n";
  134. do_meta_boxes( $this->pagehook, 'column4', '' );
  135. ?>
  136. </div></div>
  137. <?php
  138. }
  139. /**
  140. * Displays page footer.
  141. *
  142. * @return void
  143. */
  144. protected function page_footer() {
  145. parent::page_footer();
  146. $this->_boxes_js_init();
  147. }
  148. /**
  149. * Handles option saving.
  150. *
  151. * @return void
  152. */
  153. protected function form_handler() {
  154. if ( empty( $_POST ) ) {
  155. return;
  156. }
  157. check_admin_referer( $this->nonce );
  158. // Box handler
  159. foreach ( $this->boxes as $box ) {
  160. $args = isset( $box[4] ) ? $box[4] : array();
  161. $handler = $box[0] . '_handler';
  162. if ( method_exists( $this, $handler ) ) {
  163. call_user_func_array( array( $this, $handler ), $args );
  164. }
  165. }
  166. }
  167. /**
  168. * Uninstalls boxes.
  169. *
  170. * @return void
  171. */
  172. public function uninstall() {
  173. global $wpdb;
  174. $hook = str_replace( '-', '', $this->pagehook );
  175. foreach ( array( 'metaboxhidden', 'closedpostboxes', 'wp_metaboxorder', 'screen_layout' ) as $option ) {
  176. $keys[] = "'{$option}_{$hook}'";
  177. }
  178. $keys = '( ' . implode( ', ', $keys ) . ' )';
  179. $wpdb->query( "
  180. DELETE FROM {$wpdb->usermeta}
  181. WHERE meta_key IN {$keys}
  182. " );
  183. }
  184. /**
  185. * Adds boxes.
  186. *
  187. * @return void
  188. */
  189. public function boxes_init() {
  190. wp_enqueue_script( 'postbox' );
  191. add_screen_option( 'layout_columns', array(
  192. 'max' => $this->args['columns'],
  193. 'default' => $this->args['columns']
  194. ) );
  195. $registered = array();
  196. foreach ( $this->boxes as $box_args ) {
  197. $box_args = self::numeric_to_assoc( $box_args, array( 'name', 'title', 'context', 'priority', 'args' ) );
  198. $defaults = array(
  199. 'title' => ucfirst( $box_args['name'] ),
  200. 'context' => 'normal',
  201. 'priority' => 'default',
  202. 'args' => array()
  203. );
  204. $box_args = array_merge( $defaults, $box_args );
  205. $name = $box_args['name'];
  206. if ( isset( $registered[ $name ] ) ) {
  207. if ( empty( $box_args['args'] ) ) {
  208. trigger_error( "Duplicate box name: $name", E_USER_NOTICE );
  209. }
  210. $name = $this->_increment( $name );
  211. } else {
  212. $registered[ $name ] = true;
  213. }
  214. add_meta_box(
  215. $name,
  216. $box_args['title'],
  217. array( $this, '_intermediate_callback' ),
  218. $this->pagehook,
  219. $box_args['context'],
  220. $box_args['priority'],
  221. $box_args['args']
  222. );
  223. }
  224. }
  225. /**
  226. * Transforms numeric array to associative.
  227. *
  228. * @param array $argv
  229. * @param array $keys
  230. *
  231. * @return array
  232. */
  233. private static function numeric_to_assoc( $argv, $keys ) {
  234. $args = array();
  235. foreach ( $keys as $i => $key ) {
  236. if ( isset( $argv[ $i ] ) ) {
  237. $args[ $key ] = $argv[ $i ];
  238. }
  239. }
  240. return $args;
  241. }
  242. /**
  243. * Since we don't pass an object to do_meta_boxes(),
  244. * pass $box['args'] directly to each method.
  245. *
  246. * @param string $_
  247. * @param array $box
  248. *
  249. * @return void
  250. */
  251. public function _intermediate_callback( $_, $box ) {
  252. list( $name ) = explode( '-', $box['id'] );
  253. call_user_func_array( array( $this, $name . '_box' ), $box['args'] );
  254. }
  255. /**
  256. * Adds/Increments ID in box name.
  257. *
  258. * @param string $name
  259. *
  260. * @return string
  261. */
  262. private function _increment( $name ) {
  263. $parts = explode( '-', $name );
  264. if ( isset( $parts[1] ) ) {
  265. $parts[1]++;
  266. } else {
  267. $parts[1] = 2;
  268. }
  269. return implode( '-', $parts );
  270. }
  271. /**
  272. * Adds necesary code for JS to work.
  273. *
  274. * @return void
  275. */
  276. protected function _boxes_js_init() {
  277. echo $this->js_wrap( <<<EOT
  278. jQuery( document ).ready( function( $ ){
  279. // close postboxes that should be closed
  280. $( '.if-js-closed' ).removeClass( 'if-js-closed' ).addClass( 'closed' );
  281. // postboxes setup
  282. postboxes.add_postbox_toggles( '$this->pagehook' );
  283. } );
  284. EOT
  285. );
  286. ?>
  287. <form style='display: none' method='get' action=''>
  288. <p>
  289. <?php
  290. wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
  291. wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
  292. ?>
  293. </p>
  294. </form>
  295. <?php
  296. }
  297. }