PageRenderTime 28ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/robo-gallery/cmb2/includes/CMB2_hookup.php

https://gitlab.com/hunt9310/ras
PHP | 441 lines | 250 code | 67 blank | 124 comment | 44 complexity | 898100dd6a1596c8c53951efa3ef7fd3 MD5 | raw file
  1. <?php
  2. /**
  3. * Handles hooking CMB2 forms/metaboxes into the post/attachement/user screens
  4. * and handles hooking in and saving those fields.
  5. *
  6. * @since 2.0.0
  7. *
  8. * @category WordPress_Plugin
  9. * @package CMB2
  10. * @author WebDevStudios
  11. * @license GPL-2.0+
  12. * @link http://webdevstudios.com
  13. */
  14. if ( ! defined( 'ABSPATH' ) ) exit;
  15. class CMB2_hookup {
  16. /**
  17. * Metabox Form ID
  18. * @var string
  19. * @since 0.9.4
  20. */
  21. protected $form_id = 'post';
  22. /**
  23. * Array of all hooks done (to be run once)
  24. * @var array
  25. * @since 2.0.0
  26. */
  27. protected static $hooks_completed = array();
  28. /**
  29. * Only allow JS registration once
  30. * @var bool
  31. * @since 2.0.0
  32. */
  33. protected static $registration_done = false;
  34. /**
  35. * Metabox Form ID
  36. * @var CMB2 object
  37. * @since 2.0.2
  38. */
  39. protected $cmb;
  40. public function __construct( CMB2 $cmb ) {
  41. $this->cmb = $cmb;
  42. $this->hooks();
  43. if ( is_admin() ) {
  44. $this->admin_hooks();
  45. }
  46. }
  47. public function hooks() {
  48. // Handle oembed Ajax
  49. $this->once( 'wp_ajax_cmb2_oembed_handler', array( cmb2_ajax(), 'oembed_handler' ) );
  50. $this->once( 'wp_ajax_nopriv_cmb2_oembed_handler', array( cmb2_ajax(), 'oembed_handler' ) );
  51. foreach ( get_class_methods( 'CMB2_Show_Filters' ) as $filter ) {
  52. add_filter( 'cmb2_show_on', array( 'CMB2_Show_Filters', $filter ), 10, 3 );
  53. }
  54. }
  55. public function admin_hooks() {
  56. $field_types = (array) wp_list_pluck( $this->cmb->prop( 'fields', array() ), 'type' );
  57. $has_upload = in_array( 'file', $field_types ) || in_array( 'file_list', $field_types );
  58. global $pagenow;
  59. // register our scripts and styles for cmb
  60. $this->once( 'admin_enqueue_scripts', array( __CLASS__, 'register_scripts' ), 8 );
  61. $type = $this->cmb->mb_object_type();
  62. if ( 'post' == $type ) {
  63. add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) );
  64. add_action( 'add_attachment', array( $this, 'save_post' ) );
  65. add_action( 'edit_attachment', array( $this, 'save_post' ) );
  66. add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
  67. $this->once( 'admin_enqueue_scripts', array( $this, 'do_scripts' ) );
  68. if ( $has_upload && in_array( $pagenow, array( 'page.php', 'page-new.php', 'post.php', 'post-new.php' ) ) ) {
  69. $this->once( 'admin_head', array( $this, 'add_post_enctype' ) );
  70. }
  71. } elseif ( 'user' == $type ) {
  72. $priority = $this->cmb->prop( 'priority' );
  73. if ( ! is_numeric( $priority ) ) {
  74. switch ( $priority ) {
  75. case 'high':
  76. $priority = 5;
  77. break;
  78. case 'low':
  79. $priority = 20;
  80. break;
  81. default:
  82. $priority = 10;
  83. break;
  84. }
  85. }
  86. add_action( 'show_user_profile', array( $this, 'user_metabox' ), $priority );
  87. add_action( 'edit_user_profile', array( $this, 'user_metabox' ), $priority );
  88. add_action( 'user_new_form', array( $this, 'user_new_metabox' ), $priority );
  89. add_action( 'personal_options_update', array( $this, 'save_user' ) );
  90. add_action( 'edit_user_profile_update', array( $this, 'save_user' ) );
  91. add_action( 'user_register', array( $this, 'save_user' ) );
  92. if ( $has_upload && in_array( $pagenow, array( 'profile.php', 'user-edit.php', 'user-add.php' ) ) ) {
  93. $this->form_id = 'your-profile';
  94. $this->once( 'admin_head', array( $this, 'add_post_enctype' ) );
  95. }
  96. }
  97. }
  98. /**
  99. * Registers scripts and styles for CMB2
  100. * @since 1.0.0
  101. */
  102. public static function register_scripts() {
  103. if ( self::$registration_done ) {
  104. return;
  105. }
  106. // Only use minified files if SCRIPT_DEBUG is off
  107. $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
  108. if ( ! is_admin() ) {
  109. // we need to register colorpicker on the front-end
  110. wp_register_script( 'iris', admin_url( 'js/iris.min.js' ), array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), CMB2_VERSION );
  111. wp_register_script( 'wp-color-picker', admin_url( 'js/color-picker.min.js' ), array( 'iris' ), CMB2_VERSION );
  112. wp_localize_script( 'wp-color-picker', 'wpColorPickerL10n', array(
  113. 'clear' => __( 'Clear', 'cmb2' ),
  114. 'defaultString' => __( 'Default', 'cmb2' ),
  115. 'pick' => __( 'Select Color', 'cmb2' ),
  116. 'current' => __( 'Current Color', 'cmb2' ),
  117. ) );
  118. }
  119. wp_register_script( 'jquery-ui-datetimepicker', cmb2_utils()->url( 'js/jquery-ui-timepicker-addon.min.js' ), array( 'jquery-ui-slider' ), CMB2_VERSION );
  120. // scripts required for cmb
  121. $scripts = array( 'jquery', 'jquery-ui-core', 'jquery-ui-datepicker', 'jquery-ui-datetimepicker', 'wp-color-picker' );
  122. // styles required for cmb
  123. $styles = array( 'wp-color-picker' );
  124. wp_register_script( 'cmb2-scripts', cmb2_utils()->url( "js/cmb2{$min}.js" ), $scripts, CMB2_VERSION );
  125. wp_localize_script( 'cmb2-scripts', 'cmb2_l10', apply_filters( 'cmb2_localized_data', array(
  126. 'ajax_nonce' => wp_create_nonce( 'ajax_nonce' ),
  127. 'ajaxurl' => admin_url( '/admin-ajax.php' ),
  128. 'script_debug' => defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG,
  129. 'up_arrow_class' => 'dashicons dashicons-arrow-up-alt2',
  130. 'down_arrow_class' => 'dashicons dashicons-arrow-down-alt2',
  131. 'defaults' => array(
  132. 'color_picker' => false,
  133. 'date_picker' => array(
  134. 'changeMonth' => true,
  135. 'changeYear' => true,
  136. 'dateFormat' => _x( 'mm/dd/yy', 'Valid formatDate string for jquery-ui datepicker', 'cmb2' ),
  137. 'dayNames' => explode( ',', __( 'Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday', 'cmb2' ) ),
  138. 'dayNamesMin' => explode( ',', __( 'Su, Mo, Tu, We, Th, Fr, Sa', 'cmb2' ) ),
  139. 'dayNamesShort' => explode( ',', __( 'Sun, Mon, Tue, Wed, Thu, Fri, Sat', 'cmb2' ) ),
  140. 'monthNames' => explode( ',', __( 'January, February, March, April, May, June, July, August, September, October, November, December', 'cmb2' ) ),
  141. 'monthNamesShort' => explode( ',', __( 'Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec', 'cmb2' ) ),
  142. 'nextText' => __( 'Next', 'cmb2' ),
  143. 'prevText' => __( 'Prev', 'cmb2' ),
  144. 'currentText' => __( 'Today', 'cmb2' ),
  145. 'closeText' => __( 'Done', 'cmb2' ),
  146. 'clearText' => __( 'Clear', 'cmb2' ),
  147. ),
  148. 'time_picker' => array(
  149. 'timeOnlyTitle' => __( 'Choose Time', 'cmb2' ),
  150. 'timeText' => __( 'Time', 'cmb2' ),
  151. 'hourText' => __( 'Hour', 'cmb2' ),
  152. 'minuteText' => __( 'Minute', 'cmb2' ),
  153. 'secondText' => __( 'Second', 'cmb2' ),
  154. 'currentText' => __( 'Now', 'cmb2' ),
  155. 'closeText' => __( 'Done', 'cmb2' ),
  156. 'timeFormat' => _x( 'hh:mm TT', 'Valid formatting string, as per http://trentrichardson.com/examples/timepicker/', 'cmb2' ),
  157. 'controlType' => 'select',
  158. 'stepMinute' => 5,
  159. ),
  160. ),
  161. 'strings' => array(
  162. 'upload_file' => __( 'Use this file', 'cmb2' ),
  163. 'remove_image' => __( 'Remove Image', 'cmb2' ),
  164. 'remove_file' => __( 'Remove', 'cmb2' ),
  165. 'file' => __( 'File:', 'cmb2' ),
  166. 'download' => __( 'Download', 'cmb2' ),
  167. 'check_toggle' => __( 'Select / Deselect All', 'cmb2' ),
  168. ),
  169. ) ) );
  170. wp_register_style( 'cmb2-styles', cmb2_utils()->url( "css/cmb2{$min}.css" ), $styles );
  171. self::$registration_done = true;
  172. }
  173. /**
  174. * Enqueues scripts and styles for CMB2
  175. * @since 1.0.0
  176. */
  177. public function do_scripts( $hook ) {
  178. // only enqueue our scripts/styles on the proper pages
  179. if ( in_array( $hook, array( 'post.php', 'post-new.php', 'page-new.php', 'page.php' ), true ) ) {
  180. if ( $this->cmb->prop( 'cmb_styles' ) ) {
  181. self::enqueue_cmb_css();
  182. }
  183. self::enqueue_cmb_js();
  184. }
  185. }
  186. /**
  187. * Add encoding attribute
  188. */
  189. public function add_post_enctype() {
  190. echo '
  191. <script type="text/javascript">
  192. jQuery(document).ready(function(){
  193. $form = jQuery("#' . $this->form_id . '");
  194. if ( $form.length ) {
  195. $form.attr( {
  196. "enctype" : "multipart/form-data",
  197. "encoding" : "multipart/form-data"
  198. } );
  199. }
  200. });
  201. </script>';
  202. }
  203. /**
  204. * Add metaboxes (to 'post' object type)
  205. */
  206. public function add_metaboxes() {
  207. if ( ! $this->show_on() ) {
  208. return;
  209. }
  210. foreach ( $this->cmb->prop( 'object_types' ) as $post_type ) {
  211. if ( $this->cmb->prop( 'closed' ) ) {
  212. add_filter( "postbox_classes_{$post_type}_{$this->cmb->cmb_id}", array( $this, 'close_metabox_class' ) );
  213. }
  214. /**
  215. * To keep from registering an actual post-screen metabox,
  216. * omit the 'title' attribute from the metabox registration array.
  217. *
  218. * (WordPress will not display metaboxes without titles anyway)
  219. *
  220. * This is a good solution if you want to output your metaboxes
  221. * Somewhere else in the post-screen
  222. */
  223. if ( $this->cmb->prop( 'title' ) ) {
  224. add_meta_box( $this->cmb->cmb_id, $this->cmb->prop( 'title' ), array( $this, 'post_metabox' ), $post_type, $this->cmb->prop( 'context' ), $this->cmb->prop( 'priority' ) );
  225. }
  226. }
  227. }
  228. /**
  229. * Add 'closed' class to metabox
  230. * @since 2.0.0
  231. * @param array $classes Array of classes
  232. * @return array Modified array of classes
  233. */
  234. public function close_metabox_class( $classes ) {
  235. $classes[] = 'closed';
  236. return $classes;
  237. }
  238. /**
  239. * Display metaboxes for a post object
  240. * @since 1.0.0
  241. */
  242. public function post_metabox() {
  243. $this->cmb->show_form( get_the_ID(), 'post' );
  244. }
  245. /**
  246. * Display metaboxes for new user page
  247. * @since 1.0.0
  248. */
  249. public function user_new_metabox( $section ) {
  250. if ( $section == $this->cmb->prop( 'new_user_section' ) ) {
  251. $object_id = $this->cmb->object_id();
  252. $this->cmb->object_id( isset( $_REQUEST['user_id'] ) ? $_REQUEST['user_id'] : $object_id );
  253. $this->user_metabox();
  254. }
  255. }
  256. /**
  257. * Display metaboxes for a user object
  258. * @since 1.0.0
  259. */
  260. public function user_metabox() {
  261. if ( 'user' != $this->cmb->mb_object_type() ) {
  262. return;
  263. }
  264. if ( ! $this->show_on() ) {
  265. return;
  266. }
  267. if ( $this->cmb->prop( 'cmb_styles' ) ) {
  268. self::enqueue_cmb_css();
  269. }
  270. self::enqueue_cmb_js();
  271. $this->cmb->show_form( 0, 'user' );
  272. }
  273. /**
  274. * Save data from metabox
  275. */
  276. public function save_post( $post_id, $post = false ) {
  277. $post_type = $post ? $post->post_type : get_post_type( $post_id );
  278. $do_not_pass_go = (
  279. ! $this->cmb->prop( 'save_fields' )
  280. // check nonce
  281. || ! isset( $_POST[ $this->cmb->nonce() ] )
  282. || ! wp_verify_nonce( $_POST[ $this->cmb->nonce() ], $this->cmb->nonce() )
  283. // check if autosave
  284. || defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE
  285. // check user editing permissions
  286. || ( 'page' == $post_type && ! current_user_can( 'edit_page', $post_id ) )
  287. || ! current_user_can( 'edit_post', $post_id )
  288. // get the metabox post_types & compare it to this post_type
  289. || ! in_array( $post_type, $this->cmb->prop( 'object_types' ) )
  290. );
  291. if ( $do_not_pass_go ) {
  292. // do not collect $200
  293. return;
  294. }
  295. // take a trip to reading railroad – if you pass go collect $200
  296. $this->cmb->save_fields( $post_id, 'post', $_POST );
  297. }
  298. /**
  299. * Save data from metabox
  300. */
  301. public function save_user( $user_id ) {
  302. // check permissions
  303. if (
  304. ! $this->cmb->prop( 'save_fields' )
  305. // check nonce
  306. || ! isset( $_POST[ $this->cmb->nonce() ] )
  307. || ! wp_verify_nonce( $_POST[ $this->cmb->nonce() ], $this->cmb->nonce() )
  308. ) {
  309. // @todo more hardening?
  310. return;
  311. }
  312. $this->cmb->save_fields( $user_id, 'user', $_POST );
  313. }
  314. /**
  315. * Determines if metabox should be shown in current context
  316. * @since 2.0.0
  317. * @return bool Whether metabox should be added/shown
  318. */
  319. public function show_on() {
  320. $show = true;
  321. // If metabox is requesting to be conditionally shown
  322. if ( is_callable( $this->cmb->prop( 'show_on_cb' ) ) ) {
  323. $show = (bool) call_user_func( $this->cmb->prop( 'show_on_cb' ), $this->cmb );
  324. }
  325. /**
  326. * Filter to determine if metabox should show. Default is true
  327. *
  328. * @param array $show Default is true, show the metabox
  329. * @param mixed $meta_box_args Array of the metabox arguments
  330. * @param mixed $cmb The CMB2 instance
  331. */
  332. $show = (bool) apply_filters( 'cmb2_show_on', $show, $this->cmb->meta_box, $this->cmb );
  333. return $show;
  334. }
  335. /**
  336. * Ensures WordPress hook only gets fired once
  337. * @since 2.0.0
  338. * @param string $action The name of the filter to hook the $hook callback to.
  339. * @param callback $hook The callback to be run when the filter is applied.
  340. * @param integer $priority Order the functions are executed
  341. * @param int $accepted_args The number of arguments the function accepts.
  342. */
  343. public function once( $action, $hook, $priority = 10, $accepted_args = 1 ) {
  344. $key = md5( serialize( func_get_args() ) );
  345. if ( in_array( $key, self::$hooks_completed ) ) {
  346. return;
  347. }
  348. self::$hooks_completed[] = $key;
  349. add_filter( $action, $hook, $priority, $accepted_args );
  350. }
  351. /**
  352. * Includes CMB2 styles
  353. * @since 2.0.0
  354. */
  355. public static function enqueue_cmb_css() {
  356. if ( ! apply_filters( 'cmb2_enqueue_css', true ) ) {
  357. return false;
  358. }
  359. self::register_scripts();
  360. return wp_enqueue_style( 'cmb2-styles' );
  361. }
  362. /**
  363. * Includes CMB2 JS
  364. * @since 2.0.0
  365. */
  366. public static function enqueue_cmb_js() {
  367. if ( ! apply_filters( 'cmb2_enqueue_js', true ) ) {
  368. return false;
  369. }
  370. self::register_scripts();
  371. wp_enqueue_media();
  372. return wp_enqueue_script( 'cmb2-scripts' );
  373. }
  374. }