PageRenderTime 22ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/cmb2/includes/CMB2_hookup.php

https://gitlab.com/mrinal013/book_review
PHP | 410 lines | 193 code | 67 blank | 150 comment | 41 complexity | e99923674dc35a5316a9fc1ae1debc33 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. class CMB2_hookup {
  15. /**
  16. * Array of all hooks done (to be run once)
  17. * @var array
  18. * @since 2.0.0
  19. */
  20. protected static $hooks_completed = array();
  21. /**
  22. * Only allow JS registration once
  23. * @var bool
  24. * @since 2.0.7
  25. */
  26. protected static $js_registration_done = false;
  27. /**
  28. * Only allow CSS registration once
  29. * @var bool
  30. * @since 2.0.7
  31. */
  32. protected static $css_registration_done = false;
  33. /**
  34. * Metabox Form ID
  35. * @var CMB2 object
  36. * @since 2.0.2
  37. */
  38. protected $cmb;
  39. /**
  40. * The object type we are performing the hookup for
  41. * @var string
  42. * @since 2.0.9
  43. */
  44. protected $object_type = 'post';
  45. public function __construct( CMB2 $cmb ) {
  46. $this->cmb = $cmb;
  47. $this->hooks();
  48. if ( is_admin() ) {
  49. $this->admin_hooks();
  50. }
  51. }
  52. public function hooks() {
  53. // Handle oembed Ajax
  54. $this->once( 'wp_ajax_cmb2_oembed_handler', array( cmb2_ajax(), 'oembed_handler' ) );
  55. $this->once( 'wp_ajax_nopriv_cmb2_oembed_handler', array( cmb2_ajax(), 'oembed_handler' ) );
  56. foreach ( get_class_methods( 'CMB2_Show_Filters' ) as $filter ) {
  57. add_filter( 'cmb2_show_on', array( 'CMB2_Show_Filters', $filter ), 10, 3 );
  58. }
  59. }
  60. public function admin_hooks() {
  61. global $pagenow;
  62. // register our scripts and styles for cmb
  63. $this->once( 'admin_enqueue_scripts', array( __CLASS__, 'register_scripts' ), 8 );
  64. $this->object_type = $this->cmb->mb_object_type();
  65. if ( 'post' == $this->object_type ) {
  66. add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) );
  67. add_action( 'add_attachment', array( $this, 'save_post' ) );
  68. add_action( 'edit_attachment', array( $this, 'save_post' ) );
  69. add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
  70. $this->once( 'admin_enqueue_scripts', array( $this, 'do_scripts' ) );
  71. } elseif ( 'comment' == $this->object_type ) {
  72. add_action( 'add_meta_boxes_comment', array( $this, 'add_metaboxes' ) );
  73. add_action( 'edit_comment', array( $this, 'save_comment' ) );
  74. $this->once( 'admin_enqueue_scripts', array( $this, 'do_scripts' ) );
  75. } elseif ( 'user' == $this->object_type ) {
  76. $priority = $this->cmb->prop( 'priority' );
  77. if ( ! is_numeric( $priority ) ) {
  78. switch ( $priority ) {
  79. case 'high':
  80. $priority = 5;
  81. break;
  82. case 'low':
  83. $priority = 20;
  84. break;
  85. default:
  86. $priority = 10;
  87. break;
  88. }
  89. }
  90. add_action( 'show_user_profile', array( $this, 'user_metabox' ), $priority );
  91. add_action( 'edit_user_profile', array( $this, 'user_metabox' ), $priority );
  92. add_action( 'user_new_form', array( $this, 'user_new_metabox' ), $priority );
  93. add_action( 'personal_options_update', array( $this, 'save_user' ) );
  94. add_action( 'edit_user_profile_update', array( $this, 'save_user' ) );
  95. add_action( 'user_register', array( $this, 'save_user' ) );
  96. }
  97. }
  98. /**
  99. * Registers styles for CMB2
  100. * @since 2.0.7
  101. */
  102. protected static function register_styles() {
  103. if ( self::$css_registration_done ) {
  104. return;
  105. }
  106. // Only use minified files if SCRIPT_DEBUG is off
  107. $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
  108. $front = is_admin() ? '' : '-front';
  109. // Filter required styles and register stylesheet
  110. $styles = apply_filters( 'cmb2_style_dependencies', array() );
  111. wp_register_style( 'cmb2-styles', cmb2_utils()->url( "css/cmb2{$front}{$min}.css" ), $styles );
  112. self::$css_registration_done = true;
  113. }
  114. /**
  115. * Registers scripts for CMB2
  116. * @since 2.0.7
  117. */
  118. protected static function register_js() {
  119. if ( self::$js_registration_done ) {
  120. return;
  121. }
  122. $hook = is_admin() ? 'admin_footer' : 'wp_footer';
  123. add_action( $hook, array( 'CMB2_JS', 'enqueue' ), 8 );
  124. self::$js_registration_done = true;
  125. }
  126. /**
  127. * Registers scripts and styles for CMB2
  128. * @since 1.0.0
  129. */
  130. public static function register_scripts() {
  131. self::register_styles();
  132. self::register_js();
  133. }
  134. /**
  135. * Enqueues scripts and styles for CMB2
  136. * @since 1.0.0
  137. */
  138. public function do_scripts( $hook ) {
  139. // only enqueue our scripts/styles on the proper pages
  140. if ( in_array( $hook, array( 'post.php', 'post-new.php', 'page-new.php', 'page.php', 'comment.php' ), true ) ) {
  141. if ( $this->cmb->prop( 'cmb_styles' ) ) {
  142. self::enqueue_cmb_css();
  143. }
  144. if ( $this->cmb->prop( 'enqueue_js' ) ) {
  145. self::enqueue_cmb_js();
  146. }
  147. }
  148. }
  149. /**
  150. * Add metaboxes (to 'post' or 'comment' object types)
  151. * @since 1.0.0
  152. */
  153. public function add_metaboxes() {
  154. if ( ! $this->show_on() ) {
  155. return;
  156. }
  157. foreach ( $this->cmb->prop( 'object_types' ) as $post_type ) {
  158. /**
  159. * To keep from registering an actual post-screen metabox,
  160. * omit the 'title' attribute from the metabox registration array.
  161. *
  162. * (WordPress will not display metaboxes without titles anyway)
  163. *
  164. * This is a good solution if you want to output your metaboxes
  165. * Somewhere else in the post-screen
  166. */
  167. if ( $this->cmb->prop( 'title' ) ) {
  168. if ( $this->cmb->prop( 'closed' ) ) {
  169. add_filter( "postbox_classes_{$post_type}_{$this->cmb->cmb_id}", array( $this, 'close_metabox_class' ) );
  170. }
  171. add_meta_box( $this->cmb->cmb_id, $this->cmb->prop( 'title' ), array( $this, 'metabox_callback' ), $post_type, $this->cmb->prop( 'context' ), $this->cmb->prop( 'priority' ) );
  172. }
  173. }
  174. }
  175. /**
  176. * Add 'closed' class to metabox
  177. * @since 2.0.0
  178. * @param array $classes Array of classes
  179. * @return array Modified array of classes
  180. */
  181. public function close_metabox_class( $classes ) {
  182. $classes[] = 'closed';
  183. return $classes;
  184. }
  185. /**
  186. * Display metaboxes for a post or comment object
  187. * @since 1.0.0
  188. */
  189. public function metabox_callback() {
  190. $object_id = 'comment' == $this->object_type ? get_comment_ID() : get_the_ID();
  191. $this->cmb->show_form( $object_id, $this->object_type );
  192. }
  193. /**
  194. * Display metaboxes for new user page
  195. * @since 1.0.0
  196. */
  197. public function user_new_metabox( $section ) {
  198. if ( $section == $this->cmb->prop( 'new_user_section' ) ) {
  199. $object_id = $this->cmb->object_id();
  200. $this->cmb->object_id( isset( $_REQUEST['user_id'] ) ? $_REQUEST['user_id'] : $object_id );
  201. $this->user_metabox();
  202. }
  203. }
  204. /**
  205. * Display metaboxes for a user object
  206. * @since 1.0.0
  207. */
  208. public function user_metabox() {
  209. if ( 'user' != $this->cmb->mb_object_type() ) {
  210. return;
  211. }
  212. if ( ! $this->show_on() ) {
  213. return;
  214. }
  215. if ( $this->cmb->prop( 'cmb_styles' ) ) {
  216. self::enqueue_cmb_css();
  217. }
  218. if ( $this->cmb->prop( 'enqueue_js' ) ) {
  219. self::enqueue_cmb_js();
  220. }
  221. $this->cmb->show_form( 0, 'user' );
  222. }
  223. /**
  224. * Determines if metabox should be shown in current context
  225. * @since 2.0.0
  226. * @return bool Whether metabox should be added/shown
  227. */
  228. public function show_on() {
  229. // If metabox is requesting to be conditionally shown
  230. $show = $this->cmb->should_show();
  231. /**
  232. * Filter to determine if metabox should show. Default is true
  233. *
  234. * @param array $show Default is true, show the metabox
  235. * @param mixed $meta_box_args Array of the metabox arguments
  236. * @param mixed $cmb The CMB2 instance
  237. */
  238. $show = (bool) apply_filters( 'cmb2_show_on', $show, $this->cmb->meta_box, $this->cmb );
  239. return $show;
  240. }
  241. /**
  242. * Save data from post metabox
  243. * @since 1.0.0
  244. * @param int $post_id Post ID
  245. * @param mixed $post Post object
  246. * @return null
  247. */
  248. public function save_post( $post_id, $post = false ) {
  249. $post_type = $post ? $post->post_type : get_post_type( $post_id );
  250. $do_not_pass_go = (
  251. ! $this->can_save( $post_type )
  252. // check user editing permissions
  253. || ( 'page' == $post_type && ! current_user_can( 'edit_page', $post_id ) )
  254. || ! current_user_can( 'edit_post', $post_id )
  255. );
  256. if ( $do_not_pass_go ) {
  257. // do not collect $200
  258. return;
  259. }
  260. // take a trip to reading railroad – if you pass go collect $200
  261. $this->cmb->save_fields( $post_id, 'post', $_POST );
  262. }
  263. /**
  264. * Save data from comment metabox
  265. * @since 2.0.9
  266. * @param int $comment_id Comment ID
  267. * @return null
  268. */
  269. public function save_comment( $comment_id ) {
  270. $can_edit = current_user_can( 'moderate_comments', $comment_id );
  271. if ( $this->can_save( get_comment_type( $comment_id ) ) && $can_edit ) {
  272. $this->cmb->save_fields( $comment_id, 'comment', $_POST );
  273. }
  274. }
  275. /**
  276. * Save data from user fields
  277. * @since 1.0.x
  278. * @param int $user_id User ID
  279. * @return null
  280. */
  281. public function save_user( $user_id ) {
  282. // check permissions
  283. if ( $this->can_save() ) {
  284. $this->cmb->save_fields( $user_id, 'user', $_POST );
  285. }
  286. }
  287. /**
  288. * Determines if the current object is able to be saved
  289. * @since 2.0.9
  290. * @param string $type Current post_type or comment_type
  291. * @return bool Whether object can be saved
  292. */
  293. public function can_save( $type = '' ) {
  294. return (
  295. $this->cmb->prop( 'save_fields' )
  296. // check nonce
  297. && isset( $_POST[ $this->cmb->nonce() ] )
  298. && wp_verify_nonce( $_POST[ $this->cmb->nonce() ], $this->cmb->nonce() )
  299. // check if autosave
  300. && ! ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  301. // get the metabox types & compare it to this type
  302. && ( $type && in_array( $type, $this->cmb->prop( 'object_types' ) ) )
  303. );
  304. }
  305. /**
  306. * Ensures WordPress hook only gets fired once
  307. * @since 2.0.0
  308. * @param string $action The name of the filter to hook the $hook callback to.
  309. * @param callback $hook The callback to be run when the filter is applied.
  310. * @param integer $priority Order the functions are executed
  311. * @param int $accepted_args The number of arguments the function accepts.
  312. */
  313. public function once( $action, $hook, $priority = 10, $accepted_args = 1 ) {
  314. $key = md5( serialize( func_get_args() ) );
  315. if ( in_array( $key, self::$hooks_completed ) ) {
  316. return;
  317. }
  318. self::$hooks_completed[] = $key;
  319. add_filter( $action, $hook, $priority, $accepted_args );
  320. }
  321. /**
  322. * Includes CMB2 styles
  323. * @since 2.0.0
  324. */
  325. public static function enqueue_cmb_css() {
  326. if ( ! apply_filters( 'cmb2_enqueue_css', true ) ) {
  327. return false;
  328. }
  329. self::register_styles();
  330. return wp_enqueue_style( 'cmb2-styles' );
  331. }
  332. /**
  333. * Includes CMB2 JS
  334. * @since 2.0.0
  335. */
  336. public static function enqueue_cmb_js() {
  337. if ( ! apply_filters( 'cmb2_enqueue_js', true ) ) {
  338. return false;
  339. }
  340. self::register_js();
  341. return true;
  342. }
  343. }