AT_Meta_Box

All Types Meta Box class.

Defined (1)

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

/libraries/class-meta-box.php  
  1. class AT_Meta_Box { 
  2.  
  3. /** 
  4. * Holds meta box object 
  5. * @var object 
  6. * @access protected 
  7. */ 
  8. protected $_meta_box; 
  9.  
  10. /** 
  11. * Holds meta box fields. 
  12. * @var array 
  13. * @access protected 
  14. */ 
  15. protected $_prefix; 
  16.  
  17. /** 
  18. * Holds Prefix for meta box fields. 
  19. * @var array 
  20. * @access protected 
  21. */ 
  22. protected $_fields; 
  23.  
  24. /** 
  25. * Use local images. 
  26. * @var bool 
  27. * @access protected 
  28. */ 
  29. protected $_Local_images; 
  30.  
  31. /** 
  32. * SelfPath to allow themes as well as plugins. 
  33. * @var string 
  34. * @access protected 
  35. * @since 1.6 
  36. */ 
  37. protected $SelfPath; 
  38.  
  39. /** 
  40. * $field_types holds used field types 
  41. * @var array 
  42. * @access public 
  43. * @since 2.9.7 
  44. */ 
  45. public $field_types = array(); 
  46.  
  47. /** 
  48. * $inGroup holds groupping boolean 
  49. * @var boolean 
  50. * @access public 
  51. * @since 2.9.8 
  52. */ 
  53. public $inGroup = false; 
  54.  
  55. /** 
  56. * Constructor 
  57. * @since 1.0 
  58. * @access public 
  59. * @param array $meta_box 
  60. */ 
  61. public function __construct( $meta_box ) { 
  62.  
  63. // If we are not in admin area exit. 
  64. if( !is_admin() ) { 
  65. return; 
  66.  
  67. //load translation 
  68. add_filter( 'init', array( $this, 'load_textdomain' ) ); 
  69.  
  70. // Assign meta box values to local variables and add it's missed values. 
  71. $this->_meta_box = $meta_box; 
  72. $this->_prefix = ( isset( $meta_box['prefix'] ) ) ? $meta_box['prefix'] : ''; 
  73. $this->_fields = $this->_meta_box['fields']; 
  74. $this->_Local_images = ( isset( $meta_box['local_images'] ) ) ? true : false; 
  75. $this->add_missed_values(); 
  76.  
  77. if( isset( $meta_box['use_with_theme'] ) ) { 
  78. if( $meta_box['use_with_theme'] === true ) { 
  79. $this->SelfPath = get_stylesheet_directory_uri() . '/libraries'; 
  80. elseif( $meta_box['use_with_theme'] === false ) { 
  81. $this->SelfPath = plugins_url( 'meta-box-class', plugin_basename( dirname( __FILE__ ) ) ); 
  82. else { 
  83. $this->SelfPath = $meta_box['use_with_theme']; 
  84. else { 
  85. $this->SelfPath = plugins_url( 'meta-box-class', plugin_basename( dirname( __FILE__ ) ) ); 
  86.  
  87. // Add metaboxes 
  88. add_action( 'add_meta_boxes', array( $this, 'add' ) ); 
  89. //add_action( 'wp_insert_post', array( $this, 'save' ) ); 
  90. add_action( 'pre_post_update', array( $this, 'save' ) ); 
  91. // Load common js, css files 
  92. // Must enqueue for all pages as we need js for the media upload, too. 
  93. add_action( 'admin_print_styles', array( $this, 'load_scripts_styles' ) ); 
  94. //limit File type at upload 
  95. add_filter( 'wp_handle_upload_prefilter', array( $this, 'Validate_upload_file_type' ) ); 
  96.  
  97.  
  98. /** 
  99. * Load all Javascript and CSS 
  100. * @since 1.0 
  101. * @access public 
  102. */ 
  103. public function load_scripts_styles() { 
  104.  
  105. // Get Plugin Path 
  106. $plugin_path = $this->SelfPath; 
  107.  
  108. // Set template directory uri 
  109. $directory_uri = get_template_directory_uri(); 
  110. $metabox_uri = $directory_uri . '/libraries'; 
  111.  
  112. //only load styles and js when needed 
  113. /** 
  114. * since 1.8 
  115. */ 
  116. global $typenow; 
  117. if( in_array( $typenow, $this->_meta_box['pages'] ) && $this->is_edit_page() ) { 
  118. // Enqueue Meta Box Style 
  119. wp_enqueue_style( 'at-meta-box', $metabox_uri . '/css/meta-box.css' ); 
  120.  
  121. // Enqueue Meta Box Scripts 
  122. wp_enqueue_script( 'at-meta-box', $metabox_uri . '/js/meta-box.min.js', array( 'jquery' ), null, true ); 
  123. // Enqueue Cyberchimps Scripts 
  124. wp_enqueue_script( 'meta-boxes-js', $metabox_uri . '/js/metabox-tabs.min.js', array( 'jquery' ), null, true ); 
  125.  
  126. // Enqueue Media uploader for single images TODO look into removing this and use this classes image uploader that saves images as an array. Will need to change all elements to be 
  127. // TODO able to work with this 
  128. wp_enqueue_script( 'cc-media-uploader-js', $metabox_uri . '/js/media-uploader-new.min.js', array( 'jquery' ), null, true ); 
  129.  
  130. // Make upload feature work event when custom post type doesn't support 'editor' 
  131. if( $this->has_field( 'image' ) || $this->has_field( 'file' ) ) { 
  132. wp_enqueue_script( 'media-upload' ); 
  133. add_thickbox(); 
  134. wp_enqueue_script( 'jquery-ui-core' ); 
  135. wp_enqueue_script( 'jquery-ui-sortable' ); 
  136. // Check for special fields and add needed actions for them. 
  137.  
  138. //this replaces the ugly check fields methods calls 
  139. foreach( array( 'upload', 'color', 'date', 'time', 'select' ) as $type ) { 
  140. call_user_func( array( $this, 'check_field_' . $type ) ); 
  141.  
  142.  
  143. /** 
  144. * Check the Field select, Add needed Actions 
  145. * @since 2.9.8 
  146. * @access public 
  147. */ 
  148. public function check_field_select() { 
  149.  
  150. // Check if the field is an image or file. If not, return. 
  151. if( !$this->has_field( 'select' ) ) { 
  152. return; 
  153. $plugin_path = $this->SelfPath; 
  154. // Enqueu JQuery UI, use proper version. 
  155.  
  156. // Enqueu JQuery select2 library, use proper version. 
  157. // wp_enqueue_style( 'at-multiselect-select2-css', $plugin_path . '/js/select2/select2.css', array(), null ); 
  158. // wp_enqueue_script( 'at-multiselect-select2-js', $plugin_path . '/js/select2/select2.js', array( 'jquery' ), false, true ); 
  159.  
  160. /** 
  161. * Check the Field Upload, Add needed Actions 
  162. * @since 1.0 
  163. * @access public 
  164. */ 
  165. public function check_field_upload() { 
  166.  
  167. // Check if the field is an image or file. If not, return. 
  168. if( !$this->has_field( 'image' ) && !$this->has_field( 'file' ) ) { 
  169. return; 
  170.  
  171. // Add data encoding type for file uploading. 
  172. add_action( 'post_edit_form_tag', array( $this, 'add_enctype' ) ); 
  173.  
  174.  
  175. /** 
  176. * Add data encoding type for file uploading 
  177. * @since 1.0 
  178. * @access public 
  179. */ 
  180. public function add_enctype() { 
  181. printf( ' enctype="multipart/form-data" encoding="multipart/form-data" ' ); 
  182.  
  183. /** 
  184. * Check Field Color 
  185. * @since 1.0 
  186. * @access public 
  187. */ 
  188. public function check_field_color() { 
  189.  
  190. if( $this->has_field( 'color' ) && $this->is_edit_page() ) { 
  191. wp_enqueue_style( 'wp-color-picker' ); 
  192. wp_enqueue_script( 'wp-color-picker' ); 
  193.  
  194. /** 
  195. * Check Field Date 
  196. * @since 1.0 
  197. * @access public 
  198. */ 
  199. public function check_field_date() { 
  200.  
  201. if( $this->has_field( 'date' ) && $this->is_edit_page() ) { 
  202. // Enqueu JQuery UI, use proper version. 
  203. $plugin_path = $this->SelfPath; 
  204. wp_enqueue_style( 'at-jquery-ui-css', $plugin_path . '/js/jquery-ui/jquery-ui.css' ); 
  205. wp_enqueue_script( 'jquery-ui' ); 
  206. wp_enqueue_script( 'jquery-ui-datepicker' ); 
  207.  
  208. /** 
  209. * Check Field Time 
  210. * @since 1.0 
  211. * @access public 
  212. */ 
  213. public function check_field_time() { 
  214.  
  215. if( $this->has_field( 'time' ) && $this->is_edit_page() ) { 
  216. $plugin_path = $this->SelfPath; 
  217. // Enqueu JQuery UI, use proper version. 
  218. wp_enqueue_style( 'at-jquery-ui-css', $plugin_path . '/js/jquery-ui/jquery-ui.css' ); 
  219. wp_enqueue_script( 'jquery-ui' ); 
  220. wp_enqueue_script( 'at-timepicker', $plugin_path . '/js/jquery-ui/jquery-ui-timepicker-addon.min.js', array( 'jquery-ui-slider', 'jquery-ui-datepicker' ), false, true ); 
  221.  
  222. /** 
  223. * Add Meta Box for multiple post types. 
  224. * @since 1.0 
  225. * @access public 
  226. */ 
  227. public function add( $postType ) { 
  228. if( in_array( $postType, $this->_meta_box['pages'] ) ) { 
  229. add_meta_box( $this->_meta_box['id'], $this->_meta_box['title'], array( $this, 'show' ), $postType, $this->_meta_box['context'], $this->_meta_box['priority'] ); 
  230.  
  231. /** 
  232. * Callback function to show fields in meta box. 
  233. * @since 1.0 
  234. * @access public 
  235. */ 
  236. public function show() { 
  237. $this->inGroup = false; 
  238. global $post; 
  239.  
  240. wp_nonce_field( basename( __FILE__ ), 'at_meta_box_nonce' ); 
  241. echo '<table class="form-table">'; 
  242.  
  243. foreach( $this->_fields as $field ) { 
  244. $field['multiple'] = isset( $field['multiple'] ) ? $field['multiple'] : false; 
  245. $meta = get_post_meta( $post->ID, $field['id'], !$field['multiple'] ); 
  246. $meta = ( $meta !== '' ) ? $meta : @$field['std']; 
  247.  
  248. if( !in_array( $field['type'], array( 'image', 'repeater', 'file' ) ) ) { 
  249. $meta = is_array( $meta ) ? array_map( 'esc_attr', $meta ) : esc_attr( $meta ); 
  250.  
  251. $class = ( ( $field['class'] != '' ) ) ? ' class="' . esc_attr( $field['class'] ) . '-container"' : ''; 
  252. if( $this->inGroup !== true ) { 
  253. echo '<tr' . $class . '>'; 
  254.  
  255. if( isset( $field['group'] ) && $field['group'] == 'start' ) { 
  256. $this->inGroup = true; 
  257. echo '<td><table class="form-table"><tr>'; 
  258.  
  259. // Call Separated methods for displaying each type of field. 
  260. call_user_func( array( $this, 'show_field_' . $field['type'] ), $field, $meta ); 
  261.  
  262. if( $this->inGroup === true ) { 
  263. if( isset( $field['group'] ) && $field['group'] == 'end' ) { 
  264. echo '</tr></table></td></tr>'; 
  265. $this->inGroup = false; 
  266. else { 
  267. echo '</tr>'; 
  268. echo '</table>'; 
  269.  
  270. /** 
  271. * Begin Field. 
  272. * @param string $field 
  273. * @param string $meta 
  274. * @since 1.0 
  275. * @access public 
  276. */ 
  277. public function show_field_begin( $field, $meta ) { 
  278. echo "<td class='at-field'" . ( ( $this->inGroup === true ) ? " valign='top'" : "" ) . ">"; 
  279. if( $field['name'] != '' || $field['name'] != false ) { 
  280. echo "<div class='at-label'>"; 
  281. echo '<label for="' . esc_attr( $field['id'] ) . '">' . esc_attr( $field['name'] ) .'</label>'; 
  282. echo "</div>"; 
  283.  
  284. /** 
  285. * End Field. 
  286. * @param string $field 
  287. * @param string $meta 
  288. * @since 1.0 
  289. * @access public 
  290. */ 
  291. public function show_field_end( $field, $meta = null, $group = false ) { 
  292. //print description 
  293. if( isset( $field['desc'] ) && $field['desc'] != '' ) { 
  294. echo '<div class="desc-field">' . esc_attr( $field['desc'] ) . '</div>'; 
  295. echo "</td>"; 
  296.  
  297. /** 
  298. * Show Field Text. 
  299. * @param string $field 
  300. * @param string $meta 
  301. * @since 1.0 
  302. * @access public 
  303. */ 
  304. public function show_field_text( $field, $meta ) { 
  305. $this->show_field_begin( $field, $meta ); 
  306. echo '<input type="text" class="at-text' . ( isset( $field['class'] ) ? ' ' . esc_attr( $field['class'] ) : '' ) . '" name="' . esc_attr( $field['id'] ) . '" id="' . esc_attr( $field['id'] ) . '" value="' . $meta . '" size="30" ' . ( isset( $field['style'] ) ? 'style="' . esc_attr( $field['style'] ) . '"' : '' ) . '/>'; 
  307. $this->show_field_end( $field, $meta ); 
  308.  
  309. /** 
  310. * Show Field number. 
  311. * @param string $field 
  312. * @param string $meta 
  313. * @since 1.0 
  314. * @access public 
  315. */ 
  316. public function show_field_number( $field, $meta ) { 
  317. $this->show_field_begin( $field, $meta ); 
  318. $step = ( isset( $field['step'] ) || $field['step'] != '1' ) ? "step='" . $field['step'] . "' " : ''; 
  319. $min = isset( $field['min'] ) ? "min='" . esc_attr( $field['min'] ) . "' " : ''; 
  320. $max = isset( $field['max'] ) ? "max='" . esc_attr( $field['max'] ) . "' " : ''; 
  321. echo "<input type='number' class='at-number" . ( isset( $field['class'] ) ? ' ' . esc_attr( $field['class'] ) : '' ) . "' name='" . esc_attr( $field['id'] ) . "' id='" . esc_attr( $field['id'] ) . "' value='" . esc_attr( $meta ) . "' size='30' " . esc_attr( $step ) . esc_attr( $min ) . esc_attr( $max ) . ( isset( $field['style'] ) ? "style='" . esc_attr( $field['style'] ) . "'" : '' ) . "/>"; 
  322. $this->show_field_end( $field, $meta ); 
  323.  
  324. /** 
  325. * Show Field hidden. 
  326. * @param string $field 
  327. * @param string|mixed $meta 
  328. * @since 0.1.3 
  329. * @access public 
  330. */ 
  331. public function show_field_hidden( $field, $meta ) { 
  332. //$this->show_field_begin( $field, $meta ); 
  333. echo "<input type='hidden' " . ( isset( $field['style'] ) ? "style='" . esc_attr( $field['style'] ) . "' " : '' ) . "class='at-text" . ( isset( $field['class'] ) ? ' ' . esc_attr( $field['class'] ) : '' ) . "' name='" . esc_attr( $field['id'] ) . "' id='" . esc_attr( $field['id'] ) . "' value='" . esc_attr( $meta ) . "'/>"; 
  334. //$this->show_field_end( $field, $meta ); 
  335.  
  336. /** 
  337. * Show Field Paragraph. 
  338. * @param string $field 
  339. * @since 0.1.3 
  340. * @access public 
  341. */ 
  342. public function show_field_paragraph( $field ) { 
  343. //$this->show_field_begin( $field, $meta ); 
  344. echo '<p>' . esc_attr( $field['value'] ) . '</p>'; 
  345. //$this->show_field_end( $field, $meta ); 
  346.  
  347. /** 
  348. * Show Field Textarea. 
  349. * @param string $field 
  350. * @param string $meta 
  351. * @since 1.0 
  352. * @access public 
  353. */ 
  354. public function show_field_textarea( $field, $meta ) { 
  355. $this->show_field_begin( $field, $meta ); 
  356. echo "<textarea class='at-textarea large-text" . ( isset( $field['class'] ) ? ' ' . esc_attr( $field['class'] ) : '' ) . "' name='" . esc_attr( $field['id'] ) . "' id='" . esc_attr( $field['id'] ) . "' " . ( isset( $field['style'] ) ? "style='" . esc_attr( $field['style'] ) . "' " : '' ) . " cols='60' rows='10'>" . esc_attr( $meta ) . "</textarea>"; 
  357. $this->show_field_end( $field, $meta ); 
  358.  
  359. /** 
  360. * Show Field Unfiltered Textarea. 
  361. * @param string $field 
  362. * @param string $meta 
  363. * @since 1.0 
  364. * @access public 
  365. */ 
  366. public function show_field_unfiltered_textarea( $field, $meta ) { 
  367. $this->show_field_begin( $field, $meta ); 
  368. echo "<textarea class='at-textarea large-text" . ( isset( $field['class'] ) ? ' ' . esc_attr( $field['class'] ) : '' ) . "' name='" . esc_attr( $field['id'] ) . "' id='" . esc_attr( $field['id'] ) . "' " . ( isset( $field['style'] ) ? "style='" . esc_attr( $field['style'] ) . "' " : '' ) . " cols='60' rows='10'>" . esc_attr( $meta ) . "</textarea>"; 
  369. $this->show_field_end( $field, $meta ); 
  370.  
  371. /** 
  372. * Show Field Select. 
  373. * @param string $field 
  374. * @param string $meta 
  375. * @since 1.0 
  376. * @access public 
  377. */ 
  378. public function show_field_select( $field, $meta ) { 
  379.  
  380. if( !is_array( $meta ) ) { 
  381. $meta = (array)$meta; 
  382.  
  383. $this->show_field_begin( $field, $meta ); 
  384. echo "<select " . ( isset( $field['style'] ) ? "style='" . esc_attr( $field['style'] ) . "' " : '' ) . " class='at-select" . ( isset( $field['class'] ) ? ' ' . esc_attr( $field['class'] ) : '' ) . "' name='{$field['id']}" . ( $field['multiple'] ? "[]' id='" . esc_attr( $field['id'] ) . "' multiple='multiple'" : "'" ) . ">"; 
  385. foreach( $field['options'] as $key => $value ) { 
  386. echo "<option value='" . esc_attr( $key ) . "'" . selected( in_array( $key, $meta ), true, false ) . ">" . esc_attr( $value ) . "</option>"; 
  387. echo "</select>"; 
  388. $this->show_field_end( $field, $meta ); 
  389.  
  390.  
  391. /** 
  392. * Show Radio Field. 
  393. * @param string $field 
  394. * @param string $meta 
  395. * @since 1.0 
  396. * @access public 
  397. */ 
  398. public function show_field_radio( $field, $meta ) { 
  399.  
  400. if( !is_array( $meta ) ) { 
  401. $meta = (array)$meta; 
  402.  
  403. $this->show_field_begin( $field, $meta ); 
  404. foreach( $field['options'] as $key => $value ) { 
  405. echo "<input type='radio' " . ( isset( $field['style'] ) ? "style='" . esc_attr( $field['style'] ) . "' " : '' ) . " class='at-radio" . ( isset( $field['class'] ) ? ' ' . esc_attr( $field['class'] ) : '' ) . "' name='" . esc_attr( $field['id'] ) . "' value='" . esc_attr( $key ) . "'" . checked( in_array( $key, $meta ), true, false ) . " /> <span class='at-radio-label'>" . esc_attr( $value ) . "</span>"; 
  406. $this->show_field_end( $field, $meta ); 
  407.  
  408. /** 
  409. * Show Checkbox Field. 
  410. * @param string $field 
  411. * @param string $meta 
  412. * @since 1.0 
  413. * @access public 
  414. */ 
  415. public function show_field_checkbox( $field, $meta ) { 
  416.  
  417. $this->show_field_begin( $field, $meta ); 
  418. echo "<input type='checkbox' " . ( isset( $field['style'] ) ? "style='" . esc_attr( $field['style'] ) . "' " : '' ) . " class='rw-checkbox" . ( isset( $field['class'] ) ? ' ' . esc_attr( $field['class'] ) : '' ) . "' name='" . esc_attr( $field['id'] ) . "' id='" . esc_attr( $field['id'] ) . "'" . checked( !empty( $meta ), true, false ) . " />"; 
  419. $this->show_field_end( $field, $meta ); 
  420.  
  421.  
  422. /** 
  423. * Show editor Field. 
  424. * @param string $field 
  425. * @param string $meta 
  426. * @since 1.0 
  427. * @access public 
  428. */ 
  429. public function show_field_editor( $field, $meta, $in_repeater = false ) { 
  430. $this->show_field_begin( $field, $meta ); 
  431.  
  432. if( $in_repeater ) { 
  433. echo "<textarea class='at-editor theEditor large-text" . ( isset( $field['class'] ) ? ' ' . esc_attr( $field['class'] ) : '' ) . "' name='" . esc_attr( $field['id'] ) . "' id='" . esc_attr( $field['id'] ) . "' cols='60' rows='10'>" . esc_attr( $meta ) . "</textarea>"; 
  434. } else { 
  435. $default_settings = array( 'media_buttons' => false ); 
  436.  
  437. // Use new wp_editor() since WP 3.3 
  438. $settings = ( isset( $field['settings'] ) && is_array( $field['settings'] ) ? $field['settings'] : $default_settings ); 
  439. $settings['editor_class'] = 'at-editor' . ( isset( $field['class'] ) ? ' ' . esc_attr( $field['class'] ) : '' ); 
  440. $id = str_replace( "_", "", $this->stripNumeric( strtolower( $field['id'] ) ) ); 
  441. wp_editor( html_entity_decode( $meta ), $id, $settings ); 
  442. $this->show_field_end( $field, $meta ); 
  443.  
  444. /** 
  445. * Show File Field. 
  446. * @param string $field 
  447. * @param string $meta 
  448. * @since 1.0 
  449. * @access public 
  450. */ 
  451. public function show_field_file( $field, $meta ) { 
  452. wp_enqueue_media(); 
  453. $this->show_field_begin( $field, $meta ); 
  454.  
  455. $std = isset( $field['std'] ) ? $field['std'] : array( 'id' => '', 'url' => '' ); 
  456. $multiple = isset( $field['multiple'] ) ? $field['multiple'] : false; 
  457. $multiple = ( $multiple ) ? "multiFile '" : ""; 
  458. $name = esc_attr( $field['id'] ); 
  459. $value = isset( $meta['id'] ) ? $meta : $std; 
  460. $has_file = ( empty( $value['url'] ) ) ? false : true; 
  461. $type = isset( $field['mime_type'] ) ? $field['mime_type'] : ''; 
  462. $ext = isset( $field['ext'] ) ? $field['ext'] : ''; 
  463. $type = ( is_array( $type ) ? implode( "|", $type ) : $type ); 
  464. $ext = ( is_array( $ext ) ? implode( "|", $ext ) : $ext ); 
  465. $id = $field['id']; 
  466. $li = ( $has_file ) ? "<li><a href='{$value['url']}' target='_blank'>{$value['url']}</a></li>" : ""; 
  467.  
  468. echo "<span class='simplePanelfilePreview'><ul>{$li}</ul></span>"; 
  469. echo "<input type='hidden' name='" . esc_attr( $name ) . "[id]' value='" . esc_attr( $value['id'] ) . "'/>"; 
  470. echo "<input type='hidden' name='" . esc_url( $name ) . "[url]' value='" . esc_url( $value['url'] ) . "'/>"; 
  471. if( $has_file ) { 
  472. echo "<input type='button' class='" . esc_attr( $multiple ) . " button simplePanelfileUploadclear' id='" . esc_attr( $id ) . "' value='Remove File' data-mime_type='" . esc_attr( $type ) . "' data-ext='" . esc_attr( $ext ) . "'/>"; 
  473. else { 
  474. echo "<input type='button' class='" . esc_attr( $multiple ) . " button simplePanelfileUpload' id='" . esc_attr( $id ) . "' value='Upload File' data-mime_type='" . esc_attr( $type ) . "' data-ext='" . esc_attr( $ext ) . "'/>"; 
  475.  
  476. $this->show_field_end( $field, $meta ); 
  477.  
  478. /** 
  479. * Show Image Field. 
  480. * @param array $field 
  481. * @param array $meta 
  482. * @since 1.0 
  483. * @access public 
  484. */ 
  485. public function show_field_image( $field, $meta ) { 
  486. wp_enqueue_media(); 
  487.  
  488. $this->show_field_begin( $field, $meta ); 
  489.  
  490. $std = isset( $field['std'] ) ? $field['std'] : array( 'id' => '', 'url' => '' ); 
  491. $name = esc_attr( $field['id'] ); 
  492. $value = isset( $meta['id'] ) ? $meta : $std; 
  493.  
  494. $value['url'] = isset( $meta['src'] ) ? $meta['src'] : $value['url']; //backwords capability 
  495. $has_image = empty( $value['url'] ) ? false : true; 
  496. $w = isset( $field['width'] ) ? $field['width'] : 'auto'; 
  497. $h = isset( $field['height'] ) ? $field['height'] : 'auto'; 
  498. $PreviewStyle = "style='width: $w; height: $h;" . ( ( !$has_image ) ? "display: none;'" : "'" ); 
  499. $id = $field['id']; 
  500. $multiple = isset( $field['multiple'] ) ? $field['multiple'] : false; 
  501. $multiple = ( $multiple ) ? "multiFile " : ""; 
  502.  
  503. echo "<span class='simplePanelImagePreview'><img {$PreviewStyle} src='{$value['url']}'><br/></span>"; 
  504. echo "<input type='hidden' name='" . esc_attr( $name ) . "[id]' value='" . esc_attr( $value['id'] ) . "'/>"; 
  505. echo "<input type='hidden' name='" . esc_url( $name ) . "[url]' value='" . esc_url( $value['url'] ) . "'/>"; 
  506. if( $has_image ) { 
  507. echo "<input class='" . esc_attr( $multiple ) . " button simplePanelimageUploadclear' id='" . esc_url( $id ) . "' value='Remove Image' type='button'/>"; 
  508. else { 
  509. echo "<input class='" . esc_attr( $multiple ) . " button simplePanelimageUpload' id='" . esc_url( $id ) . "' value='Upload Image' type='button'/>"; 
  510. $this->show_field_end( $field, $meta ); 
  511.  
  512. /** 
  513. * Show Color Field. 
  514. * @param string $field 
  515. * @param string $meta 
  516. * @since 1.0 
  517. * @access public 
  518. */ 
  519. public function show_field_color( $field, $meta ) { 
  520.  
  521. if( empty( $meta ) ) { 
  522. $meta = '#'; 
  523.  
  524. $this->show_field_begin( $field, $meta ); 
  525. echo '<div class="input-prepend ' . esc_attr( $field['class'] ) . '"><input class="of-color" name="' . esc_attr( $field['id'] ) . '" id="' . esc_attr( $field['id'] ) . '" type="text" value="' . $meta . '" />'; 
  526. echo '<div id="' . esc_attr( $field['id'] ) . '_picker' . '" class="add-on colorSelector"><div style="background-color:' . esc_attr( $meta ) . '"></div></div></div>'; 
  527.  
  528. $this->show_field_end( $field, $meta ); 
  529.  
  530.  
  531. /** 
  532. * Show Checkbox List Field 
  533. * @param string $field 
  534. * @param string $meta 
  535. * @since 1.0 
  536. * @access public 
  537. */ 
  538. public function show_field_checkbox_list( $field, $meta ) { 
  539.  
  540. if( !is_array( $meta ) ) { 
  541. $meta = (array)$meta; 
  542.  
  543. $this->show_field_begin( $field, $meta ); 
  544.  
  545. $html = array(); 
  546.  
  547. foreach( $field['options'] as $key => $value ) { 
  548. $html[] = "<input type='checkbox' " . ( isset( $field['style'] ) ? "style='" . esc_attr( $field['style'] ) . "' " : '' ) . " class='at-checkbox_list" . ( isset( $field['class'] ) ? ' ' . esc_attr( $field['class'] ) : '' ) . "' name='" . esc_attr( $field['id'] ) . "[]' value='" . esc_attr( $key ) . "'" . checked( in_array( $key, $meta ), true, false ) . " /> " . esc_attr( $value ); 
  549.  
  550. echo implode( '<br />', $html ); 
  551.  
  552. $this->show_field_end( $field, $meta ); 
  553.  
  554.  
  555. /** 
  556. * Show Date Field. 
  557. * @param string $field 
  558. * @param string $meta 
  559. * @since 1.0 
  560. * @access public 
  561. */ 
  562. public function show_field_date( $field, $meta ) { 
  563. $this->show_field_begin( $field, $meta ); 
  564. echo "<input type='text' " . ( isset( $field['style'] ) ? "style='" . esc_attr( $field['style'] ) . "' " : '' ) . " class='at-date" . ( isset( $field['class'] ) ? ' ' . esc_attr( $field['class'] ) : '' ) . "' name='" . esc_attr( $field['id'] ) . "' id='" . esc_attr( $field['id'] ) . "' rel='" . esc_attr( $field['format'] ) . "' value='" . esc_attr( $meta ) . "' size='30' />"; 
  565. $this->show_field_end( $field, $meta ); 
  566.  
  567. /** 
  568. * Show time field. 
  569. * @param string $field 
  570. * @param string $meta 
  571. * @since 1.0 
  572. * @access public 
  573. */ 
  574. public function show_field_time( $field, $meta ) { 
  575. $this->show_field_begin( $field, $meta ); 
  576. $ampm = ( $field['ampm'] ) ? 'true' : 'false'; 
  577. echo "<input type='text' " . ( isset( $field['style'] ) ? "style='" . esc_attr( $field['style'] ) . "' " : '' ) . " class='at-time" . ( isset( $field['class'] ) ? ' ' . esc_attr( $field['class'] ) : '' ) . "' name='" . esc_attr( $field['id'] ) . "' id='" . esc_attr( $field['id'] ) . "' data-ampm='{$ampm}' rel='" . esc_attr( $field['format'] ) . "' value='" . esc_attr( $meta ) . "' size='30' />"; 
  578. $this->show_field_end( $field, $meta ); 
  579.  
  580. /** 
  581. * Show Posts field. 
  582. * used creating a posts/pages/custom types checkboxlist or a select dropdown 
  583. * @param string $field 
  584. * @param string $meta 
  585. * @since 1.0 
  586. * @access public 
  587. */ 
  588. public function show_field_posts( $field, $meta ) { 
  589. global $post; 
  590.  
  591. if( !is_array( $meta ) ) { 
  592. $meta = (array)$meta; 
  593. $this->show_field_begin( $field, $meta ); 
  594. $options = $field['options']; 
  595. $posts = get_posts( $options['args'] ); 
  596. // checkbox_list 
  597. if( 'checkbox_list' == $options['type'] ) { 
  598. foreach( $posts as $p ) { 
  599. echo "<input type='checkbox' " . ( isset( $field['style'] ) ? "style='" . esc_attr( $field['style'] ) . "' " : '' ) . " class='at-posts-checkbox" . ( isset( $field['class'] ) ? ' ' . esc_attr( $field['class'] ) : '' ) . "' name='" . esc_attr( $field['id'] ) . "[]' value='$p->ID'" . checked( in_array( $p->ID, $meta ), true, false ) . " /> $p->post_title<br/>"; 
  600. // select 
  601. else { 
  602. echo "<select " . ( isset( $field['style'] ) ? "style='" . esc_attr( $field['style'] ) . "' " : '' ) . " class='at-posts-select" . ( isset( $field['class'] ) ? ' ' . esc_attr( $field['class'] ) : '' ) . "' name='" . esc_attr( $field['id'] ) . ( $field['multiple'] ? "[]' multiple='multiple' style='height:auto'" : "'" ) . ">"; 
  603. foreach( $posts as $p ) { 
  604. echo "<option value='" . esc_attr( $p->ID ) . "'" . selected( in_array( $p->ID, $meta ), true, false ) . ">$p->post_title</option>"; 
  605. echo "</select>"; 
  606.  
  607. $this->show_field_end( $field, $meta ); 
  608.  
  609. /** 
  610. * Show Taxonomy field. 
  611. * used creating a category/tags/custom taxonomy checkboxlist or a select dropdown 
  612. * @param string $field 
  613. * @param string $meta 
  614. * @since 1.0 
  615. * @access public 
  616. * @uses get_terms() 
  617. */ 
  618. public function show_field_taxonomy( $field, $meta ) { 
  619. global $post; 
  620.  
  621. if( !is_array( $meta ) ) { 
  622. $meta = (array)$meta; 
  623. $this->show_field_begin( $field, $meta ); 
  624. $options = $field['options']; 
  625. $terms = get_terms( $options['taxonomy'], $options['args'] ); 
  626.  
  627. // checkbox_list 
  628. if( 'checkbox_list' == $options['type'] ) { 
  629. foreach( $terms as $term ) { 
  630. echo "<input type='checkbox' " . ( isset( $field['style'] ) ? "style='" . esc_attr( $field['style'] ) . "' " : '' ) . " class='at-tax-checkbox" . ( isset( $field['class'] ) ? ' ' . esc_attr( $field['class'] ) : '' ) . "' name='" . esc_attr( $field['id'] ) . "[]' value='$term->slug'" . checked( in_array( $term->slug, $meta ), true, false ) . " /> $term->name<br/>"; 
  631. // select 
  632. else { 
  633. echo "<select " . ( isset( $field['style'] ) ? "style='" . esc_attr( $field['style'] ) . "' " : '' ) . " class='at-tax-select" . ( isset( $field['class'] ) ? ' ' . esc_attr( $field['class'] ) : '' ) . "' name='{$field['id']}" . ( $field['multiple'] ? "[]' multiple='multiple' style='height:auto'" : "'" ) . ">"; 
  634. foreach( $terms as $term ) { 
  635. echo "<option value='$term->slug'" . selected( in_array( $term->slug, $meta ), true, false ) . ">$term->name</option>"; 
  636. echo "</select>"; 
  637.  
  638. $this->show_field_end( $field, $meta ); 
  639.  
  640. /** 
  641. * Show conditinal Checkbox Field. 
  642. * @param string $field 
  643. * @param string $meta 
  644. * @since 2.9.9 
  645. * @access public 
  646. */ 
  647. public function show_field_cond( $field, $meta ) { 
  648.  
  649. $this->show_field_begin( $field, $meta ); 
  650. $checked = false; 
  651. if( is_array( $meta ) && isset( $meta['enabled'] ) && $meta['enabled'] == 'on' ) { 
  652. $checked = true; 
  653. echo "<input type='checkbox' class='conditinal_control' name='{$field['id']}[enabled]' id='" . esc_attr( $field['id'] ) . "'" . checked( $checked, true, false ) . " />"; 
  654. //start showing the fields 
  655. $display = ( $checked ) ? '' : ' style="display: none;"'; 
  656.  
  657. echo '<div class="conditinal_container"' . $display . '><table>'; 
  658. foreach( (array)$field['fields'] as $f ) { 
  659. //reset var $id for cond 
  660. $id = ''; 
  661. $id = $field['id'] . '[' . $f['id'] . ']'; 
  662. $m = ''; 
  663. $m = ( isset( $meta[$f['id']] ) ) ? $meta[$f['id']] : ''; 
  664. $m = ( $m !== '' ) ? $m : ( isset( $f['std'] ) ? $f['std'] : '' ); 
  665. if( 'image' != $f['type'] && $f['type'] != 'repeater' ) { 
  666. $m = is_array( $m ) ? array_map( 'esc_attr', $m ) : esc_attr( $m ); 
  667. //set new id for field in array format 
  668. $f['id'] = $id; 
  669. echo '<tr>'; 
  670. call_user_func( array( $this, 'show_field_' . $f['type'] ), $f, $m ); 
  671. echo '</tr>'; 
  672. echo '</table></div>'; 
  673. $this->show_field_end( $field, $meta ); 
  674.  
  675. /** 
  676. * Show Help Field. 
  677. * @param string $field 
  678. * @param string $meta 
  679. * @since 1.0 
  680. * @access public 
  681. */ 
  682. public function show_field_help( $field, $meta ) { 
  683.  
  684. $this->show_field_begin( $field, $meta ); 
  685. echo '<span class="help-message">' . $field['help_text'] . '</span>'; 
  686. $this->show_field_end( $field, $meta ); 
  687.  
  688. /** 
  689. * Save Data from Metabox 
  690. * @param string $post_id 
  691. * @since 1.0 
  692. * @access public 
  693. */ 
  694. public function save( $post_id ) { 
  695.  
  696. global $post_type; 
  697.  
  698. $post_type_object = get_post_type_object( $post_type ); 
  699.  
  700. if( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) // Check Autosave 
  701. || ( !isset( $_POST['post_ID'] ) || $post_id != $_POST['post_ID'] ) // Check Revision 
  702. || ( !in_array( $post_type, $this->_meta_box['pages'] ) ) // Check if current post type is supported. 
  703. || ( !check_admin_referer( basename( __FILE__ ), 'at_meta_box_nonce' ) ) // Check nonce - Security 
  704. || ( !current_user_can( $post_type_object->cap->edit_post, $post_id ) ) 
  705. ) // Check permission 
  706. return $post_id; 
  707.  
  708. foreach( $this->_fields as $field ) { 
  709.  
  710. $name = $field['id']; 
  711. $type = $field['type']; 
  712. $old = get_post_meta( $post_id, $name, !$field['multiple'] ); 
  713. $new = ( isset( $_POST[$name] ) ) ? $_POST[$name] : ( ( $field['multiple'] ) ? array() : '' ); 
  714.  
  715. // Validate meta value 
  716. if( class_exists( 'at_Meta_Box_Validate' ) && method_exists( 'at_Meta_Box_Validate', $field['validate_func'] ) ) { 
  717. $new = call_user_func( array( 'at_Meta_Box_Validate', $field['validate_func'] ), $new ); 
  718.  
  719. //skip on Paragraph field 
  720. if( $type != "paragraph" ) { 
  721.  
  722. // Call defined method to sanitize meta value 
  723. $sanitize_func = 'sanitize_field_' . $type; 
  724.  
  725. if( method_exists( $this, $sanitize_func ) ) { 
  726. $new = call_user_func( array( $this, $sanitize_func ), $new, $old ); 
  727.  
  728. // Call defined method to save meta value, if there's no methods, call common one. 
  729. $save_func = 'save_field_' . $type; 
  730. if( method_exists( $this, $save_func ) ) { 
  731. call_user_func( array( $this, $save_func ), $post_id, $field, $old, $new ); 
  732. else { 
  733. $this->save_field( $post_id, $field, $old, $new ); 
  734.  
  735. } // End foreach 
  736.  
  737. /** 
  738. * Common function for saving fields. 
  739. * @param string $post_id 
  740. * @param string $field 
  741. * @param string $old 
  742. * @param string|mixed $new 
  743. * @since 1.0 
  744. * @access public 
  745. */ 
  746. public function save_field( $post_id, $field, $old, $new ) { 
  747.  
  748. $name = $field['id']; 
  749. delete_post_meta( $post_id, $name ); 
  750. if( $new === '' || $new === array() ) { 
  751. return; 
  752. if( $field['multiple'] ) { 
  753. foreach( $new as $add_new ) { 
  754. add_post_meta( $post_id, $name, $add_new, false ); 
  755. else { 
  756. update_post_meta( $post_id, $name, $new ); 
  757.  
  758. /** 
  759. * function for saving image field. 
  760. * @param string $post_id 
  761. * @param string $field 
  762. * @param string $old 
  763. * @param string|mixed $new 
  764. * @since 1.7 
  765. * @access public 
  766. */ 
  767. public function save_field_image( $post_id, $field, $old, $new ) { 
  768. $name = $field['id']; 
  769. delete_post_meta( $post_id, $name ); 
  770. if( $new === '' || $new === array() || $new['id'] == '' || $new['url'] == '' ) { 
  771. return; 
  772.  
  773. update_post_meta( $post_id, $name, $new ); 
  774.  
  775. /** 
  776. * Save Editor Field. 
  777. * @param string $post_id  
  778. * @param string $field  
  779. * @param string $old  
  780. * @param string $new  
  781. * @since 1.0 
  782. * @access public  
  783. */ 
  784. public function save_field_editor( $post_id, $field, $old, $new ) { 
  785. $id = str_replace( "_", "", $this->stripNumeric( strtolower( $field['id'] ) ) ); 
  786. $new = ( isset( $_POST[$id] ) ) ? $_POST[$id] : ( ( $field['multiple'] ) ? array() : '' ); 
  787. $this->save_field( $post_id, $field, $old, $new ); 
  788.  
  789. /** 
  790. * Save repeater Fields. 
  791. * @param string $post_id 
  792. * @param string $field 
  793. * @param string|mixed $old 
  794. * @param string|mixed $new 
  795. * @since 1.0 
  796. * @access public 
  797. */ 
  798. public function save_field_repeater( $post_id, $field, $old, $new ) { 
  799. if( is_array( $new ) && count( $new ) > 0 ) { 
  800. foreach( $new as $n ) { 
  801. foreach( $field['fields'] as $f ) { 
  802. $type = $f['type']; 
  803. switch( $type ) { 
  804. case 'editor': 
  805. $n[$f['id']] = wpautop( $n[$f['id']] ); 
  806. break; 
  807. default: 
  808. break; 
  809. if( !$this->is_array_empty( $n ) ) { 
  810. $temp[] = $n; 
  811. if( isset( $temp ) && count( $temp ) > 0 && !$this->is_array_empty( $temp ) ) { 
  812. update_post_meta( $post_id, $field['id'], $temp ); 
  813. else { 
  814. // remove old meta if exists 
  815. delete_post_meta( $post_id, $field['id'] ); 
  816. else { 
  817. // remove old meta if exists 
  818. delete_post_meta( $post_id, $field['id'] ); 
  819.  
  820. /** 
  821. * Save File Field. 
  822. * @param string $post_id 
  823. * @param string $field 
  824. * @param string $old 
  825. * @param string $new 
  826. * @since 1.0 
  827. * @access public 
  828. */ 
  829. public function save_field_file( $post_id, $field, $old, $new ) { 
  830.  
  831. $name = $field['id']; 
  832. delete_post_meta( $post_id, $name ); 
  833. if( $new === '' || $new === array() || $new['id'] == '' || $new['url'] == '' ) { 
  834. return; 
  835.  
  836. update_post_meta( $post_id, $name, $new ); 
  837.  
  838. /** 
  839. * Save repeater File Field. 
  840. * @param string $post_id 
  841. * @param string $field 
  842. * @param string $old 
  843. * @param string $new 
  844. * @since 1.0 
  845. * @access public 
  846. * @deprecated 3.0.7 
  847. */ 
  848. public function save_field_file_repeater( $post_id, $field, $old, $new ) { 
  849.  
  850. /** 
  851. * Save Checkbox Field. 
  852. * @param string $post_id 
  853. * @param string $field 
  854. * @param string $old 
  855. * @param string $new 
  856. * @since 1.0 
  857. * @access public 
  858. */ 
  859. function save_field_checkbox( $post_id, $field, $old, $new ) { 
  860. $new = $new ? "1" : "0"; 
  861. update_post_meta( $post_id, $field['id'], $new ); 
  862.  
  863. /** 
  864. * Add missed values for meta box. 
  865. * @since 1.0 
  866. * @access public 
  867. */ 
  868. public function add_missed_values() { 
  869.  
  870. // Default values for meta box 
  871. $this->_meta_box = array_merge( array( 'context' => 'normal', 'priority' => 'high', 'pages' => array( 'post' ) ), (array)$this->_meta_box ); 
  872.  
  873. // Default values for fields 
  874. foreach( $this->_fields as &$field ) { 
  875.  
  876. $multiple = in_array( $field['type'], array( 'checkbox_list', 'file', 'image' ) ); 
  877. $std = $multiple ? array() : ''; 
  878. $format = 'date' == $field['type'] ? 'yy-mm-dd' : ( 'time' == $field['type'] ? 'hh:mm' : '' ); 
  879.  
  880. $field = array_merge( array( 'multiple' => $multiple, 'std' => $std, 'desc' => '', 'format' => $format, 'validate_func' => '' ), $field ); 
  881.  
  882. } // End foreach 
  883.  
  884.  
  885. /** 
  886. * Check if field with $type exists. 
  887. * @param string $type 
  888. * @since 1.0 
  889. * @access public 
  890. */ 
  891. public function has_field( $type ) { 
  892. //faster search in single dimention array. 
  893. if( count( $this->field_types ) > 0 ) { 
  894. return in_array( $type, $this->field_types ); 
  895.  
  896. //run once over all fields and store the types in a local array 
  897. $temp = array(); 
  898. foreach( $this->_fields as $field ) { 
  899. $temp[] = $field['type']; 
  900. if( 'repeater' == $field['type'] || 'cond' == $field['type'] ) { 
  901. foreach( (array)$field["fields"] as $repeater_field ) { 
  902. $temp[] = $repeater_field["type"]; 
  903.  
  904. //remove duplicates 
  905. $this->field_types = array_unique( $temp ); 
  906.  
  907. //call this function one more time now that we have an array of field types 
  908. return $this->has_field( $type ); 
  909.  
  910. /** 
  911. * Check if current page is edit page. 
  912. * @since 1.0 
  913. * @access public 
  914. */ 
  915. public function is_edit_page() { 
  916. global $pagenow; 
  917.  
  918. return in_array( $pagenow, array( 'post.php', 'post-new.php' ) ); 
  919.  
  920. /** 
  921. * Fixes the odd indexing of multiple file uploads. 
  922. * Goes from the format: 
  923. * $_FILES['field']['key']['index'] 
  924. * to 
  925. * The More standard and appropriate: 
  926. * $_FILES['field']['index']['key'] 
  927. * @param string $files 
  928. * @since 1.0 
  929. * @access public 
  930. */ 
  931. public function fix_file_array( &$files ) { 
  932.  
  933. $output = array(); 
  934.  
  935. foreach( $files as $key => $list ) { 
  936. foreach( $list as $index => $value ) { 
  937. $output[$index][$key] = $value; 
  938.  
  939. return $output; 
  940.  
  941.  
  942. /** 
  943. * Get proper JQuery UI version. 
  944. * Used in order to not conflict with WP Admin Scripts. 
  945. * @since 1.0 
  946. * @access public 
  947. */ 
  948. public function get_jqueryui_ver() { 
  949.  
  950. global $wp_version; 
  951.  
  952. if( version_compare( $wp_version, '3.1', '>=' ) ) { 
  953. return '1.8.10'; 
  954.  
  955. return '1.7.3'; 
  956.  
  957.  
  958. /** 
  959. * Add Field to meta box (generic function) 
  960. * @author Ohad Raz 
  961. * @since 1.2 
  962. * @access public 
  963. * @param $id string field id, i.e. the meta key 
  964. * @param $args mixed|array 
  965. */ 
  966. public function addField( $id, $args ) { 
  967. $new_field = array( 'id' => $id, 'std' => '', 'desc' => '', 'style' => '' ); 
  968. $new_field = array_merge( $new_field, $args ); 
  969. $this->_fields[] = $new_field; 
  970.  
  971. /** 
  972. * Add Text Field to meta box 
  973. * @author Ohad Raz 
  974. * @since 1.0 
  975. * @access public 
  976. * @param $id string field id, i.e. the meta key 
  977. * @param $args mixed|array 
  978. * 'name' => // field name/label string optional 
  979. * 'desc' => // field description, string optional 
  980. * 'std' => // default value, string optional 
  981. * 'style' => // custom style for field, string optional 
  982. * 'validate_func' => // validate function, string optional 
  983. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  984. */ 
  985. public function addText( $id, $args, $repeater = false ) { 
  986. $new_field = array( 'type' => 'text', 'id' => $id, 'std' => '', 'desc' => '', 'style' => '', 'name' => 'Text Field' ); 
  987. $new_field = array_merge( $new_field, $args ); 
  988. if( false === $repeater ) { 
  989. $this->_fields[] = $new_field; 
  990. else { 
  991. return $new_field; 
  992.  
  993. /** 
  994. * Add Number Field to meta box 
  995. * @author Ohad Raz 
  996. * @since 1.0 
  997. * @access public 
  998. * @param $id string field id, i.e. the meta key 
  999. * @param $args mixed|array 
  1000. * 'name' => // field name/label string optional 
  1001. * 'desc' => // field description, string optional 
  1002. * 'std' => // default value, string optional 
  1003. * 'style' => // custom style for field, string optional 
  1004. * 'validate_func' => // validate function, string optional 
  1005. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1006. */ 
  1007. public function addNumber( $id, $args, $repeater = false ) { 
  1008. $new_field = array( 'type' => 'number', 'id' => $id, 'std' => '0', 'desc' => '', 'style' => '', 'name' => 'Number Field', 'step' => '1', 'min' => '0' ); 
  1009. $new_field = array_merge( $new_field, $args ); 
  1010. if( false === $repeater ) { 
  1011. $this->_fields[] = $new_field; 
  1012. else { 
  1013. return $new_field; 
  1014.  
  1015. /** 
  1016. * Add Hidden Field to meta box 
  1017. * @author Ohad Raz 
  1018. * @since 0.1.3 
  1019. * @access public 
  1020. * @param $id string field id, i.e. the meta key 
  1021. * @param $args mixed|array 
  1022. * 'name' => // field name/label string optional 
  1023. * 'desc' => // field description, string optional 
  1024. * 'std' => // default value, string optional 
  1025. * 'style' => // custom style for field, string optional 
  1026. * 'validate_func' => // validate function, string optional 
  1027. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1028. */ 
  1029. public function addHidden( $id, $args, $repeater = false ) { 
  1030. $new_field = array( 'type' => 'hidden', 'id' => $id, 'std' => '', 'desc' => '', 'style' => '', 'name' => 'Text Field' ); 
  1031. $new_field = array_merge( $new_field, $args ); 
  1032. if( false === $repeater ) { 
  1033. $this->_fields[] = $new_field; 
  1034. else { 
  1035. return $new_field; 
  1036.  
  1037. /** 
  1038. * Add Paragraph to meta box 
  1039. * @author Ohad Raz 
  1040. * @since 0.1.3 
  1041. * @access public 
  1042. * @param $id string field id, i.e. the meta key 
  1043. * @param $value paragraph html 
  1044. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1045. */ 
  1046. public function addParagraph( $id, $args, $repeater = false ) { 
  1047. $new_field = array( 'type' => 'paragraph', 'id' => $id, 'value' => '' ); 
  1048. $new_field = array_merge( $new_field, $args ); 
  1049. if( false === $repeater ) { 
  1050. $this->_fields[] = $new_field; 
  1051. else { 
  1052. return $new_field; 
  1053.  
  1054. /** 
  1055. * Add Checkbox Field to meta box 
  1056. * @author Ohad Raz 
  1057. * @since 1.0 
  1058. * @access public 
  1059. * @param $id string field id, i.e. the meta key 
  1060. * @param $args mixed|array 
  1061. * 'name' => // field name/label string optional 
  1062. * 'desc' => // field description, string optional 
  1063. * 'std' => // default value, string optional 
  1064. * 'validate_func' => // validate function, string optional 
  1065. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1066. */ 
  1067. public function addCheckbox( $id, $args, $repeater = false ) { 
  1068. $new_field = array( 'type' => 'checkbox', 'id' => $id, 'std' => '', 'desc' => '', 'style' => '', 'name' => 'Checkbox Field' ); 
  1069. $new_field = array_merge( $new_field, $args ); 
  1070. if( false === $repeater ) { 
  1071. $this->_fields[] = $new_field; 
  1072. else { 
  1073. return $new_field; 
  1074.  
  1075. /** 
  1076. * Add CheckboxList Field to meta box 
  1077. * @author Ohad Raz 
  1078. * @since 1.0 
  1079. * @access public 
  1080. * @param $id string field id, i.e. the meta key 
  1081. * @param $options (array) array of key => value pairs for select options 
  1082. * @param $args mixed|array 
  1083. * 'name' => // field name/label string optional 
  1084. * 'desc' => // field description, string optional 
  1085. * 'std' => // default value, string optional 
  1086. * 'validate_func' => // validate function, string optional 
  1087. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1088. * @return : remember to call: $checkbox_list = get_post_meta(get_the_ID(), 'meta_name', false); 
  1089. * which means the last param as false to get the values in an array 
  1090. */ 
  1091. public function addCheckboxList( $id, $options, $args, $repeater = false ) { 
  1092. $new_field = array( 'type' => 'checkbox_list', 'id' => $id, 'std' => '', 'desc' => '', 'style' => '', 'name' => 'Checkbox List Field', 'options' => $options, 'multiple' => true, ); 
  1093. $new_field = array_merge( $new_field, $args ); 
  1094. if( false === $repeater ) { 
  1095. $this->_fields[] = $new_field; 
  1096. else { 
  1097. return $new_field; 
  1098.  
  1099. /** 
  1100. * Add Textarea Field to meta box 
  1101. * @author Ohad Raz 
  1102. * @since 1.0 
  1103. * @access public 
  1104. * @param $id string field id, i.e. the meta key 
  1105. * @param $args mixed|array 
  1106. * 'name' => // field name/label string optional 
  1107. * 'desc' => // field description, string optional 
  1108. * 'std' => // default value, string optional 
  1109. * 'style' => // custom style for field, string optional 
  1110. * 'validate_func' => // validate function, string optional 
  1111. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1112. */ 
  1113. public function addTextarea( $id, $args, $repeater = false ) { 
  1114. $new_field = array( 'type' => 'textarea', 'id' => $id, 'std' => '', 'desc' => '', 'style' => '', 'name' => 'Textarea Field' ); 
  1115. $new_field = array_merge( $new_field, $args ); 
  1116. if( false === $repeater ) { 
  1117. $this->_fields[] = $new_field; 
  1118. else { 
  1119. return $new_field; 
  1120.  
  1121. /** 
  1122. * Add Select Field to meta box 
  1123. * @author Ohad Raz 
  1124. * @since 1.0 
  1125. * @access public 
  1126. * @param $id string field id, i.e. the meta key 
  1127. * @param $options (array) array of key => value pairs for select options 
  1128. * @param $args mixed|array 
  1129. * 'name' => // field name/label string optional 
  1130. * 'desc' => // field description, string optional 
  1131. * 'std' => // default value, (array) optional 
  1132. * 'multiple' => // select multiple values, optional. Default is false. 
  1133. * 'validate_func' => // validate function, string optional 
  1134. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1135. */ 
  1136. public function addSelect( $id, $options, $args, $repeater = false ) { 
  1137. $new_field = array( 'type' => 'select', 'id' => $id, 'std' => array(), 'desc' => '', 'style' => '', 'name' => 'Select Field', 'multiple' => false, 'options' => $options ); 
  1138. $new_field = array_merge( $new_field, $args ); 
  1139. if( false === $repeater ) { 
  1140. $this->_fields[] = $new_field; 
  1141. else { 
  1142. return $new_field; 
  1143.  
  1144. /** 
  1145. * Add Radio Field to meta box 
  1146. * @author Ohad Raz 
  1147. * @since 1.0 
  1148. * @access public 
  1149. * @param $id string field id, i.e. the meta key 
  1150. * @param $options (array) array of key => value pairs for radio options 
  1151. * @param $args mixed|array 
  1152. * 'name' => // field name/label string optional 
  1153. * 'desc' => // field description, string optional 
  1154. * 'std' => // default value, string optional 
  1155. * 'validate_func' => // validate function, string optional 
  1156. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1157. */ 
  1158. public function addRadio( $id, $options, $args, $repeater = false ) { 
  1159. $new_field = array( 'type' => 'radio', 'id' => $id, 'std' => array(), 'desc' => '', 'style' => '', 'name' => 'Radio Field', 'options' => $options ); 
  1160. $new_field = array_merge( $new_field, $args ); 
  1161. if( false === $repeater ) { 
  1162. $this->_fields[] = $new_field; 
  1163. else { 
  1164. return $new_field; 
  1165.  
  1166. /** 
  1167. * Add Date Field to meta box 
  1168. * @author Ohad Raz 
  1169. * @since 1.0 
  1170. * @access public 
  1171. * @param $id string field id, i.e. the meta key 
  1172. * @param $args mixed|array 
  1173. * 'name' => // field name/label string optional 
  1174. * 'desc' => // field description, string optional 
  1175. * 'std' => // default value, string optional 
  1176. * 'validate_func' => // validate function, string optional 
  1177. * 'format' => // date format, default yy-mm-dd. Optional. Default "'d MM, yy'" See more formats here: http://goo.gl/Wcwxn 
  1178. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1179. */ 
  1180. public function addDate( $id, $args, $repeater = false ) { 
  1181. $new_field = array( 'type' => 'date', 'id' => $id, 'std' => '', 'desc' => '', 'format' => 'd MM, yy', 'name' => 'Date Field' ); 
  1182. $new_field = array_merge( $new_field, $args ); 
  1183. if( false === $repeater ) { 
  1184. $this->_fields[] = $new_field; 
  1185. else { 
  1186. return $new_field; 
  1187.  
  1188. /** 
  1189. * Add Time Field to meta box 
  1190. * @author Ohad Raz 
  1191. * @since 1.0 
  1192. * @access public 
  1193. * @param $id string- field id, i.e. the meta key 
  1194. * @param $args mixed|array 
  1195. * 'name' => // field name/label string optional 
  1196. * 'desc' => // field description, string optional 
  1197. * 'std' => // default value, string optional 
  1198. * 'validate_func' => // validate function, string optional 
  1199. * 'format' => // time format, default hh:mm. Optional. See more formats here: http://goo.gl/83woX 
  1200. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1201. */ 
  1202. public function addTime( $id, $args, $repeater = false ) { 
  1203. $new_field = array( 'type' => 'time', 'id' => $id, 'std' => '', 'desc' => '', 'format' => 'hh:mm', 'name' => 'Time Field', 'ampm' => false ); 
  1204. $new_field = array_merge( $new_field, $args ); 
  1205. if( false === $repeater ) { 
  1206. $this->_fields[] = $new_field; 
  1207. else { 
  1208. return $new_field; 
  1209.  
  1210. /** 
  1211. * Add Color Field to meta box 
  1212. * @author Ohad Raz 
  1213. * @since 1.0 
  1214. * @access public 
  1215. * @param $id string field id, i.e. the meta key 
  1216. * @param $args mixed|array 
  1217. * 'name' => // field name/label string optional 
  1218. * 'desc' => // field description, string optional 
  1219. * 'std' => // default value, string optional 
  1220. * 'validate_func' => // validate function, string optional 
  1221. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1222. */ 
  1223. public function addColor( $id, $args, $repeater = false ) { 
  1224. $new_field = array( 'type' => 'color', 'id' => $id, 'std' => '', 'desc' => '', 'name' => 'ColorPicker Field' ); 
  1225. $new_field = array_merge( $new_field, $args ); 
  1226. if( false === $repeater ) { 
  1227. $this->_fields[] = $new_field; 
  1228. else { 
  1229. return $new_field; 
  1230.  
  1231. /** 
  1232. * Add Image Field to meta box 
  1233. * @author Ohad Raz 
  1234. * @since 1.0 
  1235. * @access public 
  1236. * @param $id string field id, i.e. the meta key 
  1237. * @param $args mixed|array 
  1238. * 'name' => // field name/label string optional 
  1239. * 'desc' => // field description, string optional 
  1240. * 'validate_func' => // validate function, string optional 
  1241. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1242. */ 
  1243. public function addImage( $id, $args, $repeater = false ) { 
  1244. $new_field = array( 'type' => 'image', 'id' => $id, 'desc' => '', 'name' => 'Image Field', 'std' => array( 'id' => '', 'url' => '' ), 'multiple' => false ); 
  1245. $new_field = array_merge( $new_field, $args ); 
  1246. if( false === $repeater ) { 
  1247. $this->_fields[] = $new_field; 
  1248. else { 
  1249. return $new_field; 
  1250.  
  1251. /** 
  1252. * Add File Field to meta box 
  1253. * @author Ohad Raz 
  1254. * @since 1.0 
  1255. * @access public 
  1256. * @param $id string field id, i.e. the meta key 
  1257. * @param $args mixed|array 
  1258. * 'name' => // field name/label string optional 
  1259. * 'desc' => // field description, string optional 
  1260. * 'validate_func' => // validate function, string optional 
  1261. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1262. */ 
  1263. public function addFile( $id, $args, $repeater = false ) { 
  1264. $new_field = array( 'type' => 'file', 'id' => $id, 'desc' => '', 'name' => 'File Field', 'multiple' => false, 'std' => array( 'id' => '', 'url' => '' ) ); 
  1265. $new_field = array_merge( $new_field, $args ); 
  1266. if( false === $repeater ) { 
  1267. $this->_fields[] = $new_field; 
  1268. else { 
  1269. return $new_field; 
  1270.  
  1271. /** 
  1272. * Add Editor Field to meta box 
  1273. * @author Ohad Raz 
  1274. * @since 1.0 
  1275. * @access public 
  1276. * @param $id string field id, i.e. the meta key 
  1277. * @param $args mixed|array 
  1278. * 'name' => // field name/label string optional 
  1279. * 'desc' => // field description, string optional 
  1280. * 'std' => // default value, string optional 
  1281. * 'style' => // custom style for field, string optional Default 'width: 300px; height: 400px' 
  1282. * 'validate_func' => // validate function, string optional 
  1283. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1284. */ 
  1285. public function addEditor( $id, $args, $repeater = false ) { 
  1286. $new_field = array( 'type' => 'editor', 'id' => $id, 'std' => '', 'desc' => '', 'style' => 'width: 300px; height: 400px', 'name' => 'Editor Editor Field' ); 
  1287. $new_field = array_merge( $new_field, $args ); 
  1288. if( false === $repeater ) { 
  1289. $this->_fields[] = $new_field; 
  1290. else { 
  1291. return $new_field; 
  1292.  
  1293. /** 
  1294. * Add Taxonomy Field to meta box 
  1295. * @author Ohad Raz 
  1296. * @since 1.0 
  1297. * @access public 
  1298. * @param $id string field id, i.e. the meta key 
  1299. * @param $options mixed|array options of taxonomy field 
  1300. * 'taxonomy' => // taxonomy name can be category, post_tag or any custom taxonomy default is category 
  1301. * 'type' => // how to show taxonomy? 'select' (default) or 'checkbox_list' 
  1302. * 'args' => // arguments to query taxonomy, see http://goo.gl/uAANN default ('hide_empty' => false) 
  1303. * @param $args mixed|array 
  1304. * 'name' => // field name/label string optional 
  1305. * 'desc' => // field description, string optional 
  1306. * 'std' => // default value, string optional 
  1307. * 'validate_func' => // validate function, string optional 
  1308. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1309. */ 
  1310. public function addTaxonomy( $id, $options, $args, $repeater = false ) { 
  1311. $temp = array( 
  1312. 'args' => array( 'hide_empty' => 0 ),  
  1313. 'tax' => 'category',  
  1314. 'type' => 'select' 
  1315. ); 
  1316. $options = array_merge( $temp, $options ); 
  1317. $new_field = array( 'type' => 'taxonomy', 'id' => $id, 'desc' => '', 'name' => 'Taxonomy Field', 'options' => $options ); 
  1318. $new_field = array_merge( $new_field, $args ); 
  1319. if( false === $repeater ) { 
  1320. $this->_fields[] = $new_field; 
  1321. else { 
  1322. return $new_field; 
  1323.  
  1324. /** 
  1325. * Add posts Field to meta box 
  1326. * @author Ohad Raz 
  1327. * @since 1.0 
  1328. * @access public 
  1329. * @param $id string field id, i.e. the meta key 
  1330. * @param $options mixed|array options of taxonomy field 
  1331. * 'post_type' => // post type name, 'post' (default) 'page' or any custom post type 
  1332. * 'type' => // how to show posts? 'select' (default) or 'checkbox_list' 
  1333. * 'args' => // arguments to query posts, see http://goo.gl/is0yK default ('posts_per_page' => -1) 
  1334. * @param $args mixed|array 
  1335. * 'name' => // field name/label string optional 
  1336. * 'desc' => // field description, string optional 
  1337. * 'std' => // default value, string optional 
  1338. * 'validate_func' => // validate function, string optional 
  1339. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1340. */ 
  1341. public function addPosts( $id, $options, $args, $repeater = false ) { 
  1342. $post_type = isset( $options['post_type'] ) ? $options['post_type'] : ( isset( $args['post_type'] ) ? $args['post_type'] : 'post' ); 
  1343. $type = isset( $options['type'] ) ? $options['type'] : 'select'; 
  1344. $q = array( 'posts_per_page' => -1, 'post_type' => $post_type ); 
  1345. if( isset( $options['args'] ) ) { 
  1346. $q = array_merge( $q, (array)$options['args'] ); 
  1347. $options = array( 'post_type' => $post_type, 'type' => $type, 'args' => $q ); 
  1348. $new_field = array( 'type' => 'posts', 'id' => $id, 'desc' => '', 'name' => 'Posts Field', 'options' => $options, 'multiple' => false ); 
  1349. $new_field = array_merge( $new_field, $args ); 
  1350. if( false === $repeater ) { 
  1351. $this->_fields[] = $new_field; 
  1352. else { 
  1353. return $new_field; 
  1354.  
  1355. /** 
  1356. * Add repeater Field Block to meta box 
  1357. * @author Ohad Raz 
  1358. * @since 1.0 
  1359. * @access public 
  1360. * @param $id string field id, i.e. the meta key 
  1361. * @param $args mixed|array 
  1362. * 'name' => // field name/label string optional 
  1363. * 'desc' => // field description, string optional 
  1364. * 'std' => // default value, string optional 
  1365. * 'style' => // custom style for field, string optional 
  1366. * 'validate_func' => // validate function, string optional 
  1367. * 'fields' => //fields to repeater 
  1368. */ 
  1369. public function addRepeaterBlock( $id, $args ) { 
  1370. $new_field = array( 
  1371. 'type' => 'repeater',  
  1372. 'id' => $id,  
  1373. 'name' => 'Reapeater Field',  
  1374. 'fields' => array(),  
  1375. 'inline' => false,  
  1376. 'sortable' => false 
  1377. ); 
  1378. $new_field = array_merge( $new_field, $args ); 
  1379. $this->_fields[] = $new_field; 
  1380.  
  1381. /** 
  1382. * Add Checkbox conditional Field to Page 
  1383. * @author Ohad Raz 
  1384. * @since 2.9.9 
  1385. * @access public 
  1386. * @param $id string field id, i.e. the key 
  1387. * @param $args mixed|array 
  1388. * 'name' => // field name/label string optional 
  1389. * 'desc' => // field description, string optional 
  1390. * 'std' => // default value, string optional 
  1391. * 'validate_func' => // validate function, string optional 
  1392. * 'fields' => list of fields to show conditionally. 
  1393. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1394. */ 
  1395. public function addCondition( $id, $args, $repeater = false ) { 
  1396. $new_field = array( 
  1397. 'type' => 'cond',  
  1398. 'id' => $id,  
  1399. 'std' => '',  
  1400. 'desc' => '',  
  1401. 'style' => '',  
  1402. 'name' => 'Conditional Field',  
  1403. 'fields' => array() 
  1404. ); 
  1405. $new_field = array_merge( $new_field, $args ); 
  1406. if( false === $repeater ) { 
  1407. $this->_fields[] = $new_field; 
  1408. else { 
  1409. return $new_field; 
  1410.  
  1411. /** 
  1412. * Finish Declaration of Meta Box 
  1413. * @author Ohad Raz 
  1414. * @since 1.0 
  1415. * @access public 
  1416. */ 
  1417. public function Finish() { 
  1418. $this->add_missed_values(); 
  1419.  
  1420. /** 
  1421. * Helper function to check for empty arrays 
  1422. * @author Ohad Raz 
  1423. * @since 1.5 
  1424. * @access public 
  1425. * @param $args mixed|array 
  1426. */ 
  1427. public function is_array_empty( $array ) { 
  1428. if( !is_array( $array ) ) { 
  1429. return true; 
  1430.  
  1431. foreach( $array as $a ) { 
  1432. if( is_array( $a ) ) { 
  1433. foreach( $a as $sub_a ) { 
  1434. if( !empty( $sub_a ) && $sub_a != '' ) { 
  1435. return false; 
  1436. else { 
  1437. if( !empty( $a ) && $a != '' ) { 
  1438. return false; 
  1439.  
  1440. return true; 
  1441.  
  1442. /** 
  1443. * Validate_upload_file_type 
  1444. * Checks if the uploaded file is of the expected format 
  1445. * @author Ohad Raz <admin@bainternet.info> 
  1446. * @since 3.0.7 
  1447. * @access public 
  1448. * @uses get_allowed_mime_types() to check allowed types 
  1449. * @param array $file uploaded file 
  1450. * @return array file with error on mismatch 
  1451. */ 
  1452. function Validate_upload_file_type( $file ) { 
  1453. if( isset( $_POST['uploadeType'] ) && !empty( $_POST['uploadeType'] ) && isset( $_POST['uploadeType'] ) && $_POST['uploadeType'] == 'my_meta_box' ) { 
  1454. $allowed = explode( "|", $_POST['uploadeType'] ); 
  1455. $ext = substr( strrchr( $file['name'], '.' ), 1 ); 
  1456.  
  1457. if( !in_array( $ext, (array)$allowed ) ) { 
  1458. $file['error'] = __( 'Sorry, you cannot upload this file type for this field.', 'responsive-mobile' ); 
  1459.  
  1460. return $file; 
  1461.  
  1462. foreach( get_allowed_mime_types() as $key => $value ) { 
  1463. if( strpos( $key, $ext ) || $key == $ext ) { 
  1464. return $file; 
  1465. $file['error'] = __( 'Sorry, you cannot upload this file type for this field.', 'responsive-mobile' ); 
  1466.  
  1467. return $file; 
  1468.  
  1469. /** 
  1470. * function to sanitize field id 
  1471. * @author Ohad Raz <admin@bainternet.info> 
  1472. * @since 3.0.7 
  1473. * @access public 
  1474. * @param string $str string to sanitize 
  1475. * @return string sanitized string 
  1476. */ 
  1477. public function idfy( $str ) { 
  1478. return str_replace( " ", "_", $str ); 
  1479.  
  1480.  
  1481. /** 
  1482. * stripNumeric Strip number form string 
  1483. * @author Ohad Raz <admin@bainternet.info> 
  1484. * @since 3.0.7 
  1485. * @access public 
  1486. * @param string $str 
  1487. * @return string number less string 
  1488. */ 
  1489. public function stripNumeric( $str ) { 
  1490. return trim( str_replace( range( 0, 9 ), '', $str ) ); 
  1491.  
  1492. /** 
  1493. * load_textdomain 
  1494. * @author Ohad Raz 
  1495. * @since 2.9.4 
  1496. * @return void 
  1497. */ 
  1498. public function load_textdomain() { 
  1499. //In themes/plugins/mu-plugins directory 
  1500. load_textdomain( 'responsive-mobile', dirname( __FILE__ ) . '/lang/' . get_locale() . 'mo' ); 
  1501. } // End Class