YIT_Metabox

YIT Metabox.

Defined (1)

The class is defined in the following location(s).

/plugin-fw/lib/yit-metabox.php  
  1. class YIT_Metabox 
  2.  
  3. /** 
  4. * @var string the id of metabox 
  5. * @since 1.0 
  6. */ 
  7.  
  8. public $id; 
  9.  
  10. /** 
  11. * @var array An array where are saved all metabox settings options 
  12. * @since 1.0 
  13. */ 
  14. private $options = array(); 
  15.  
  16. /** 
  17. * @var array An array where are saved all tabs of metabox 
  18. * @since 1.0 
  19. */ 
  20. private $tabs = array(); 
  21.  
  22. /** 
  23. * @var object The single instance of the class 
  24. * @since 1.0 
  25. */ 
  26. protected static $_instance = array(); 
  27.  
  28. /** 
  29. * Main Instance 
  30. * @static 
  31. * @param $id 
  32. * @return object Main instance 
  33. * @since 1.0 
  34. * @author Antonino Scarfi' <antonino.scarfi@yithemes.com> 
  35. */ 
  36. public static function instance($id) 
  37. if (!isset(self::$_instance[$id])) { 
  38. self::$_instance[$id] = new self($id); 
  39. return self::$_instance[$id]; 
  40.  
  41. /** 
  42. * Constructor 
  43. * @param string $id 
  44. * @return \YIT_Metabox 
  45. * @since 1.0 
  46. * @author Emanuela Castorina <emanuela.castorina@yithemes.it> 
  47. */ 
  48. function __construct($id = '') 
  49. $this->id = $id; 
  50.  
  51.  
  52.  
  53. /** 
  54. * Init 
  55. * set options and tabs, add actions to register metabox, scripts and save data 
  56. * @param array $options 
  57. * @return void 
  58. * @since 1.0 
  59. * @author Emanuela Castorina <emanuela.castorina@yithemes.it> 
  60. */ 
  61. public function init($options = array()) 
  62.  
  63. $this->set_options($options); 
  64. $this->set_tabs(); 
  65.  
  66.  
  67. add_action('add_meta_boxes', array($this, 'register_metabox'), 99); 
  68. add_action('save_post', array($this, 'save_postdata')); 
  69. add_action('admin_enqueue_scripts', array($this, 'enqueue'), 15); 
  70.  
  71.  
  72. /** 
  73. * Enqueue script and styles in admin side 
  74. * Add style and scripts to administrator 
  75. * @return void 
  76. * @since 1.0 
  77. * @author Emanuela Castorina <emanuela.castorina@yithemes.it> 
  78. */ 
  79. public function enqueue() 
  80.  
  81. wp_enqueue_media(); 
  82. wp_enqueue_style('wp-color-picker'); 
  83. wp_enqueue_style('yit-plugin-metaboxes', YIT_CORE_PLUGIN_URL . '/assets/css/metaboxes.css'); 
  84. wp_enqueue_style('jquery-chosen', YIT_CORE_PLUGIN_URL . '/assets/css/chosen/chosen.css'); 
  85. wp_enqueue_script('jquery-ui-datepicker'); 
  86. wp_enqueue_script('yit-spinner', YIT_CORE_PLUGIN_URL . '/assets/js/panel.spinner.js', array('jquery'), '0.0.1', true); 
  87. wp_enqueue_script('jquery-chosen', YIT_CORE_PLUGIN_URL . '/assets/js/chosen/chosen.jquery.js', array('jquery'), '1.1.0', true); 
  88. wp_enqueue_script('ajax-chosen', YIT_CORE_PLUGIN_URL . '/assets/js/chosen/'.yit_load_js_file('ajax-chosen.jquery.js'), array('jquery'), '1.1.0', true); 
  89. wp_enqueue_script('yit-metabox', YIT_CORE_PLUGIN_URL . '/assets/js/metabox.js', array('jquery', 'wp-color-picker'), '1.0.0', true); 
  90. wp_enqueue_style('jquery-ui-overcast', YIT_CORE_PLUGIN_URL . '/assets/css/overcast/jquery-ui-custom/jquery-ui-1.8.9.custom.css', false, '1.8.9', 'all'); 
  91.  
  92. /** 
  93. * Set Options 
  94. * Set the variable options 
  95. * @param array $options 
  96. * @return void 
  97. * @since 1.0 
  98. * @author Emanuela Castorina <emanuela.castorina@yithemes.it> 
  99. */ 
  100. public function set_options($options = array()) 
  101. $this->options = $options; 
  102.  
  103.  
  104. /** 
  105. * Set Tabs 
  106. * Set the variable tabs 
  107. * @internal param array $tabs 
  108. * @return void 
  109. * @since 1.0 
  110. * @author Emanuela Castorina <emanuela.castorina@yithemes.it> 
  111. */ 
  112. public function set_tabs() 
  113. if (!isset($this->options['tabs'])) { 
  114. return; 
  115. $this->tabs = $this->options['tabs']; 
  116. if (isset($this->tabs['settings']['fields'])) { 
  117. $this->tabs['settings']['fields'] = array_filter($this->tabs['settings']['fields']); 
  118.  
  119.  
  120. /** 
  121. * Add Tab 
  122. * Add a tab inside the metabox 
  123. * @internal param array $tabs 
  124. * @param array $tab the new tab to add to the metabox 
  125. * @param string $where tell where insert the tab if after or before a $refer 
  126. * @param null $refer an existent tab inside metabox 
  127. * @return void 
  128. * @since 1.0 
  129. * @author Emanuela Castorina <emanuela.castorina@yithemes.it> 
  130. */ 
  131. public function add_tab($tab, $where = 'after', $refer = null) 
  132. if (!is_null($refer)) { 
  133. $ref_pos = array_search($refer, array_keys($this->tabs)); 
  134. if ($ref_pos !== false) { 
  135. if ($where == 'after') { 
  136. $this->tabs = array_slice($this->tabs, 0, $ref_pos + 1, true) + 
  137. $tab + 
  138. array_slice($this->tabs, $ref_pos + 1, count($this->tabs) - 1, true); 
  139. } else { 
  140. $this->tabs = array_slice($this->tabs, 0, $ref_pos, true) + 
  141. $tab + 
  142. array_slice($this->tabs, $ref_pos, count($this->tabs), true); 
  143. } else { 
  144. $this->tabs = array_merge($tab, $this->tabs); 
  145.  
  146.  
  147. /** 
  148. * Remove Tab 
  149. * Remove a tab from the tabs of metabox 
  150. * @internal param array $tabs 
  151. * @param $id_tab 
  152. * @return void 
  153. * @since 1.0 
  154. * @author Emanuela Castorina <emanuela.castorina@yithemes.it> 
  155. */ 
  156. public function remove_tab($id_tab) 
  157. if (isset($this->tabs[$id_tab])) { 
  158. unset ($this->tabs[$id_tab]); 
  159.  
  160.  
  161. /** 
  162. * Add Field 
  163. * Add a field inside a tab of metabox 
  164. * @internal param array $tabs 
  165. * @param string $tab_id the id of the tabs where add the field 
  166. * @param array $args the field to add 
  167. * @param string $where tell where insert the field if after or before a $refer 
  168. * @param null $refer an existent field inside tab 
  169. * @return void 
  170. * @since 1.0 
  171. * @author Emanuela Castorina <emanuela.castorina@yithemes.it> 
  172. */ 
  173. public function add_field($tab_id, $args, $where = 'after', $refer = null) 
  174. if (isset($this->tabs[$tab_id])) { 
  175.  
  176. $cf = $this->tabs[$tab_id]['fields']; 
  177. if (!is_null($refer)) { 
  178. $ref_pos = array_search($refer, array_keys($cf)); 
  179. if ($ref_pos !== false) { 
  180. if ($where == 'after') { 
  181. $this->tabs[$tab_id]['fields'] = array_slice($cf, 0, $ref_pos + 1, true) + 
  182. $args + 
  183. array_slice($cf, $ref_pos, count($cf) - 1, true); 
  184.  
  185. } elseif ($where == 'before') { 
  186. $this->tabs[$tab_id]['fields'] = array_slice($cf, 0, $ref_pos, true) + 
  187. $args + 
  188. array_slice($cf, $ref_pos, count($cf), true); 
  189.  
  190. } else { 
  191. if ($where == 'first') { 
  192. $this->tabs[$tab_id]['fields'] = $args + $cf; 
  193.  
  194. } else { 
  195. $this->tabs[$tab_id]['fields'] = array_merge($this->tabs[$tab_id]['fields'], $args); 
  196.  
  197.  
  198.  
  199.  
  200. /** 
  201. * Remove Field 
  202. * Remove a field from the metabox, search inside the tabs and remove it if exists 
  203. * @param $id_field 
  204. * @return void 
  205. * @since 1.0 
  206. * @author Emanuela Castorina <emanuela.castorina@yithemes.it> 
  207. */ 
  208. public function remove_field($id_field) 
  209. foreach ($this->tabs as $tab_name => $tab) { 
  210. if (isset($tab['fields'][$id_field])) { 
  211. unset ($this->tabs[$tab_name]['fields'][$id_field]); 
  212.  
  213. /** 
  214. * Reorder tabs 
  215. * Order the tabs and fields and set id and name to each field 
  216. * @internal param $id_field 
  217. * @return void 
  218. * @since 1.0 
  219. * @author Emanuela Castorina <emanuela.castorina@yithemes.it> 
  220. */ 
  221. public function reorder_tabs() 
  222. foreach ($this->tabs as $tab_name => $tab) { 
  223. foreach ($tab['fields'] as $id_field => $field) { 
  224. $this->tabs[$tab_name]['fields'][$id_field]['private'] = (isset($field['private'])) ? $field['private'] : true; 
  225. $this->tabs[$tab_name]['fields'][$id_field]['id'] = $this->get_option_metabox_id($id_field, $this->tabs[$tab_name]['fields'][$id_field]['private']); 
  226. $this->tabs[$tab_name]['fields'][$id_field]['name'] = $this->get_option_metabox_name($this->tabs[$tab_name]['fields'][$id_field]['id']); 
  227.  
  228.  
  229.  
  230. /** 
  231. * Get Option Metabox ID 
  232. * return the id of the field 
  233. * @param string $id_field 
  234. * @param bool $private if private add an _befor the id 
  235. * @return string 
  236. * @since 1.0 
  237. * @author Emanuela Castorina <emanuela.castorina@yithemes.it> 
  238. */ 
  239. public function get_option_metabox_id($id_field, $private = true) 
  240. if ($private) { 
  241. return '_' . $id_field; 
  242. } else { 
  243. return $id_field; 
  244.  
  245. /** 
  246. * Get Option Metabox Name 
  247. * return the name of the field, this name will be used as attribute name of the input field 
  248. * @param string $id_field 
  249. * @param bool $private if private add an _befor the id 
  250. * @return string 
  251. * @since 1.0 
  252. * @author Emanuela Castorina <emanuela.castorina@yithemes.it> 
  253. */ 
  254. public function get_option_metabox_name($id_field, $private = true) 
  255. $db_name = apply_filters('yit_metaboxes_option_main_name', 'yit_metaboxes'); 
  256. $return = $db_name . '['; 
  257.  
  258. if (!strpos($id_field, '[')) { 
  259. return $return . $id_field . ']'; 
  260. $return .= substr($id_field, 0, strpos($id_field, '[')); 
  261. $return .= ']'; 
  262. $return .= substr($id_field, strpos($id_field, '[')); 
  263.  
  264. return $return; 
  265.  
  266. /** 
  267. * Register the metabox 
  268. * call the wp function add_metabox to add the metabox 
  269. * @return void 
  270. * @since 1.0 
  271. * @author Emanuela Castorina <emanuela.castorina@yithemes.it> 
  272. */ 
  273. public function register_metabox($post_type) 
  274.  
  275. if (in_array($post_type, (array)$this->options['pages'])) { 
  276. add_meta_box($this->id, $this->options['label'], array($this, 'show'), $post_type, $this->options['context'], $this->options['priority']); 
  277.  
  278. /** 
  279. * Show metabox 
  280. * show the html of metabox 
  281. * @return void 
  282. * @since 1.0 
  283. * @author Emanuela Castorina <emanuela.castorina@yithemes.it> 
  284. */ 
  285. public function show() 
  286. $this->reorder_tabs(); 
  287.  
  288. yit_plugin_get_template(YIT_CORE_PLUGIN_PATH, 'metaboxes/tab.php', array('tabs' => $this->tabs)); 
  289.  
  290. /** 
  291. * Save Post Data 
  292. * Save the post data in the database when save the post 
  293. * @param $post_id 
  294. * @return int 
  295. * @since 1.0 
  296. * @author Emanuela Castorina <emanuela.castorina@yithemes.it> 
  297. */ 
  298. public function save_postdata($post_id) 
  299.  
  300.  
  301. if (!isset($_POST['yit_metaboxes_nonce']) || !wp_verify_nonce($_POST['yit_metaboxes_nonce'], 'metaboxes-fields-nonce')) { 
  302. return $post_id; 
  303.  
  304.  
  305. if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || (defined('DOING_AJAX') && DOING_AJAX)) { 
  306. return $post_id; 
  307.  
  308. if (isset($_POST['post_type'])) { 
  309. $post_type = $_POST['post_type']; 
  310. } else { 
  311. return $post_id; 
  312.  
  313. if ('page' == $post_type) { 
  314. if (!current_user_can('edit_page', $post_id)) { 
  315. return $post_id; 
  316. } else { 
  317. if (!current_user_can('edit_post', $post_id)) { 
  318. return $post_id; 
  319.  
  320. /**if (!in_array($post_type, (array)$this->options['pages'])) { 
  321. return $post_id; 
  322. }*/ 
  323.  
  324. $this->reorder_tabs(); 
  325.  
  326. if(isset($_POST['yit_metaboxes'])) { 
  327. $yit_metabox_data = $_POST['yit_metaboxes']; 
  328.  
  329. if(is_array($yit_metabox_data)) { 
  330.  
  331. foreach ($yit_metabox_data as $field_name => $field_value) { 
  332.  
  333. if(! add_post_meta($post_id, $field_name, $field_value, true) ) 
  334. update_post_meta($post_id, $field_name, $field_value); 
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341. foreach ($this->tabs as $tab) { 
  342.  
  343. foreach ($tab['fields'] as $field) { 
  344.  
  345. if (in_array($field['type'], array('title'))) { 
  346. continue; 
  347.  
  348. if (isset($_POST['yit_metaboxes'][$field['id']])) { 
  349.  
  350. add_post_meta($post_id, $field['id'], $_POST['yit_metaboxes'][$field['id']], true) || update_post_meta($post_id, $field['id'], $_POST['yit_metaboxes'][$field['id']]); 
  351. } elseif (in_array($field['type'], array('onoff', 'checkbox'))) { 
  352. update_post_meta($post_id, $field['id'], '0'); 
  353. } else { 
  354. delete_post_meta($post_id, $field['id']); 
  355.  
  356.  
  357. /** 
  358. * Remove Fields 
  359. * Remove a fields list from the metabox, search inside the tabs and remove it if exists 
  360. * @param $id_fields 
  361. * @return void 
  362. * @since 2.0.0 
  363. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  364. */ 
  365. public function remove_fields($id_fields) 
  366. foreach ($id_fields as $k => $field) { 
  367. $this->remove_field($field);