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="' . $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. * Show Repeater Fields. 
  272. * @param string $field 
  273. * @param string $meta 
  274. * @since 1.0 
  275. * @access public 
  276. */ 
  277. public function show_field_repeater( $field, $meta ) { 
  278. global $post; 
  279. // Get Plugin Path 
  280. $plugin_path = $this->SelfPath; 
  281. $this->show_field_begin( $field, $meta ); 
  282. $class = ''; 
  283. if( $field['sortable'] ) { 
  284. $class = " repeater-sortable"; 
  285. echo "<div class='at-repeat" . $class . "' id='{$field['id']}'>"; 
  286.  
  287. $c = 0; 
  288. $meta = get_post_meta( $post->ID, $field['id'], true ); 
  289.  
  290. if( count( $meta ) > 0 && is_array( $meta ) ) { 
  291. foreach( $meta as $me ) { 
  292. //for labling toggles 
  293. $mmm = isset( $me[$field['fields'][0]['id']] ) ? $me[$field['fields'][0]['id']] : ""; 
  294. if( in_array( $field['fields'][0]['type'], array( 'image', 'file' ) ) ) { 
  295. $mmm = $c + 1; 
  296. echo '<div class="at-repater-block">' . $mmm . '<br/><table class="repeater-table" style="display: none;">'; 
  297. if( $field['inline'] ) { 
  298. echo '<tr class="at-inline" VALIGN="top">'; 
  299. foreach( $field['fields'] as $f ) { 
  300. //reset var $id for repeater 
  301. $id = ''; 
  302. $id = $field['id'] . '[' . $c . '][' . $f['id'] . ']'; 
  303. $m = isset( $me[$f['id']] ) ? $me[$f['id']] : ''; 
  304. $m = ( $m !== '' ) ? $m : $f['std']; 
  305. if( 'image' != $f['type'] && $f['type'] != 'repeater' ) { 
  306. $m = is_array( $m ) ? array_map( 'esc_attr', $m ) : esc_attr( $m ); 
  307. //set new id for field in array format 
  308. $f['id'] = $id; 
  309. if( !$field['inline'] ) { 
  310. echo '<tr>'; 
  311. call_user_func( array( $this, 'show_field_' . $f['type'] ), $f, $m ); 
  312. if( !$field['inline'] ) { 
  313. echo '</tr>'; 
  314. if( $field['inline'] ) { 
  315. echo '</tr>'; 
  316. echo '</table>'; 
  317. if( $field['sortable'] ) { 
  318. echo '<span class="re-control"><img src="' . $plugin_path . '/images/move.png" alt="sort" title="sort" class="at_re_sort_handle" /></span>'; 
  319.  
  320. echo ' 
  321. <span class="re-control at-re-toggle"><img src="' . $plugin_path . '/images/edit.png" alt="Edit" title="Edit"/></span>  
  322. <span class="re-control"><img src="' . $plugin_path . '/images/remove.png" alt="' . __( 'Remove', 'responsive-II' ) . '" title="' . __( 'Remove', 'responsive-II' ) . '" id="remove-' . $field['id'] . '"></span> 
  323. <span class="re-control-clear"></span></div>'; 
  324. $c = $c + 1; 
  325.  
  326. echo '<img src="'; 
  327. if( $this->_Local_images ) { 
  328. echo $plugin_path . '/images/add.png'; 
  329. else { 
  330. echo 'http://i.imgur.com/w5Tuc.png'; 
  331. echo '" alt="' . __( 'Add', 'responsive-II' ) . '" title="' . __( 'Add', 'responsive-II' ) . '" id="add-' . $field['id'] . '"><br/></div>'; 
  332.  
  333. //create all fields once more for js function and catch with object buffer 
  334. ob_start(); 
  335. echo '<div class="at-repater-block"><table class="repeater-table">'; 
  336. if( $field['inline'] ) { 
  337. echo '<tr class="at-inline" VALIGN="top">'; 
  338. foreach( $field['fields'] as $f ) { 
  339. //reset var $id for repeater 
  340. $id = ''; 
  341. $id = $field['id'] . '[CurrentCounter][' . $f['id'] . ']'; 
  342. $f['id'] = $id; 
  343. if( !$field['inline'] ) { 
  344. echo '<tr>'; 
  345. if( $f['type'] != 'editor' ) { 
  346. call_user_func( array( $this, 'show_field_' . $f['type'] ), $f, '' ); 
  347. else { 
  348. call_user_func( array( $this, 'show_field_' . $f['type'] ), $f, '', true ); 
  349. if( !$field['inline'] ) { 
  350. echo '</tr>'; 
  351. if( $field['inline'] ) { 
  352. echo '</tr>'; 
  353. echo '</table><img src="' . $plugin_path . '/images/remove.png" alt="' . __( 'Remove', 'responsive-II' ) . '" title="' . __( 'Remove', 'responsive-II' ) . '" id="remove-' . $field['id'] . '"></div>'; 
  354. $counter = 'countadd_' . $field['id']; 
  355. $js_code = ob_get_clean(); 
  356. $js_code = str_replace( "\n", "", $js_code ); 
  357. $js_code = str_replace( "\r", "", $js_code ); 
  358. $js_code = str_replace( "'", "\"", $js_code ); 
  359. $js_code = str_replace( "CurrentCounter", "' + " . $counter . " + '", $js_code ); 
  360. echo '<script> 
  361. jQuery(document).ready(function() { 
  362. var ' . $counter . ' = ' . $c . '; 
  363. jQuery("#add-' . $field['id'] . '").live(\'click\', function() { 
  364. ' . $counter . ' = ' . $counter . ' + 1; 
  365. jQuery(this).before(\'' . $js_code . '\');  
  366. update_repeater_fields(); 
  367. }); 
  368. jQuery("#remove-' . $field['id'] . '").live(\'click\', function() { 
  369. jQuery(this).parent().parent().remove(); 
  370. }); 
  371. }); 
  372. </script>'; 
  373. echo '<br/><style> 
  374. .at_re_sort_highlight{min-height: 55px; background-color: #EEEEEE; margin: 2px;} 
  375. .re-control-clear{clear: both; display: block;} 
  376. .at_re_sort_handle{cursor: move;} 
  377. .re-control{float: right; padding: 5px;}  
  378. .at-inline{line-height: 1 !important;} 
  379. .at-inline .at-field{border: 0px !important;} 
  380. .at-inline .at-label{margin: 0 0 1px !important;} 
  381. .at-inline .at-text{width: 70px;} 
  382. .at-inline .at-textarea{width: 100px; height: 75px;} 
  383. .at-repater-block{background-color: #FFFFFF;border: 1px solid;margin: 2px; min-height: 50px} 
  384. </style>'; 
  385. $this->show_field_end( $field, $meta ); 
  386.  
  387. /** 
  388. * Begin Field. 
  389. * @param string $field 
  390. * @param string $meta 
  391. * @since 1.0 
  392. * @access public 
  393. */ 
  394. public function show_field_begin( $field, $meta ) { 
  395. echo "<td class='at-field'" . ( ( $this->inGroup === true ) ? " valign='top'" : "" ) . ">"; 
  396. if( $field['name'] != '' || $field['name'] != false ) { 
  397. echo "<div class='at-label'>"; 
  398. echo "<label for='{$field['id']}'>{$field['name']}</label>"; 
  399. echo "</div>"; 
  400.  
  401. /** 
  402. * End Field. 
  403. * @param string $field 
  404. * @param string $meta 
  405. * @since 1.0 
  406. * @access public 
  407. */ 
  408. public function show_field_end( $field, $meta = null, $group = false ) { 
  409. //print description 
  410. if( isset( $field['desc'] ) && $field['desc'] != '' ) { 
  411. echo "<div class='desc-field'>{$field['desc']}</div>"; 
  412. echo "</td>"; 
  413.  
  414. /** 
  415. * Show Field Text. 
  416. * @param string $field 
  417. * @param string $meta 
  418. * @since 1.0 
  419. * @access public 
  420. */ 
  421. public function show_field_text( $field, $meta ) { 
  422. $this->show_field_begin( $field, $meta ); 
  423. echo "<input type='text' class='at-text" . ( isset( $field['class'] ) ? ' ' . $field['class'] : '' ) . "' name='{$field['id']}' id='{$field['id']}' value='{$meta}' size='30' " . ( isset( $field['style'] ) ? "style='{$field['style']}'" : '' ) . "/>"; 
  424. $this->show_field_end( $field, $meta ); 
  425.  
  426. /** 
  427. * Show Field number. 
  428. * @param string $field 
  429. * @param string $meta 
  430. * @since 1.0 
  431. * @access public 
  432. */ 
  433. public function show_field_number( $field, $meta ) { 
  434. $this->show_field_begin( $field, $meta ); 
  435. $step = ( isset( $field['step'] ) || $field['step'] != '1' ) ? "step='" . $field['step'] . "' " : ''; 
  436. $min = isset( $field['min'] ) ? "min='" . $field['min'] . "' " : ''; 
  437. $max = isset( $field['max'] ) ? "max='" . $field['max'] . "' " : ''; 
  438. echo "<input type='number' class='at-number" . ( isset( $field['class'] ) ? ' ' . $field['class'] : '' ) . "' name='{$field['id']}' id='{$field['id']}' value='{$meta}' size='30' " . $step . $min . $max . ( isset( $field['style'] ) ? "style='{$field['style']}'" : '' ) . "/>"; 
  439. $this->show_field_end( $field, $meta ); 
  440.  
  441. /** 
  442. * Show Field hidden. 
  443. * @param string $field 
  444. * @param string|mixed $meta 
  445. * @since 0.1.3 
  446. * @access public 
  447. */ 
  448. public function show_field_hidden( $field, $meta ) { 
  449. //$this->show_field_begin( $field, $meta ); 
  450. echo "<input type='hidden' " . ( isset( $field['style'] ) ? "style='{$field['style']}' " : '' ) . "class='at-text" . ( isset( $field['class'] ) ? ' ' . $field['class'] : '' ) . "' name='{$field['id']}' id='{$field['id']}' value='{$meta}'/>"; 
  451. //$this->show_field_end( $field, $meta ); 
  452.  
  453. /** 
  454. * Show Field Paragraph. 
  455. * @param string $field 
  456. * @since 0.1.3 
  457. * @access public 
  458. */ 
  459. public function show_field_paragraph( $field ) { 
  460. //$this->show_field_begin( $field, $meta ); 
  461. echo '<p>' . $field['value'] . '</p>'; 
  462. //$this->show_field_end( $field, $meta ); 
  463.  
  464. /** 
  465. * Show Field Textarea. 
  466. * @param string $field 
  467. * @param string $meta 
  468. * @since 1.0 
  469. * @access public 
  470. */ 
  471. public function show_field_textarea( $field, $meta ) { 
  472. $this->show_field_begin( $field, $meta ); 
  473. echo "<textarea class='at-textarea large-text" . ( isset( $field['class'] ) ? ' ' . $field['class'] : '' ) . "' name='{$field['id']}' id='{$field['id']}' " . ( isset( $field['style'] ) ? "style='{$field['style']}' " : '' ) . " cols='60' rows='10'>{$meta}</textarea>"; 
  474. $this->show_field_end( $field, $meta ); 
  475.  
  476. /** 
  477. * Show Field Unfiltered Textarea. 
  478. * @param string $field 
  479. * @param string $meta 
  480. * @since 1.0 
  481. * @access public 
  482. */ 
  483. public function show_field_unfiltered_textarea( $field, $meta ) { 
  484. $this->show_field_begin( $field, $meta ); 
  485. echo "<textarea class='at-textarea large-text" . ( isset( $field['class'] ) ? ' ' . $field['class'] : '' ) . "' name='{$field['id']}' id='{$field['id']}' " . ( isset( $field['style'] ) ? "style='{$field['style']}' " : '' ) . " cols='60' rows='10'>{$meta}</textarea>"; 
  486. $this->show_field_end( $field, $meta ); 
  487.  
  488. /** 
  489. * Show Field Select. 
  490. * @param string $field 
  491. * @param string $meta 
  492. * @since 1.0 
  493. * @access public 
  494. */ 
  495. public function show_field_select( $field, $meta ) { 
  496.  
  497. if( !is_array( $meta ) ) { 
  498. $meta = (array)$meta; 
  499.  
  500. $this->show_field_begin( $field, $meta ); 
  501. echo "<select " . ( isset( $field['style'] ) ? "style='{$field['style']}' " : '' ) . " class='at-select" . ( isset( $field['class'] ) ? ' ' . $field['class'] : '' ) . "' name='{$field['id']}" . ( $field['multiple'] ? "[]' id='{$field['id']}' multiple='multiple'" : "'" ) . ">"; 
  502. foreach( $field['options'] as $key => $value ) { 
  503. echo "<option value='{$key}'" . selected( in_array( $key, $meta ), true, false ) . ">{$value}</option>"; 
  504. echo "</select>"; 
  505. $this->show_field_end( $field, $meta ); 
  506.  
  507.  
  508. /** 
  509. * Show Radio Field. 
  510. * @param string $field 
  511. * @param string $meta 
  512. * @since 1.0 
  513. * @access public 
  514. */ 
  515. public function show_field_radio( $field, $meta ) { 
  516.  
  517. if( !is_array( $meta ) ) { 
  518. $meta = (array)$meta; 
  519.  
  520. $this->show_field_begin( $field, $meta ); 
  521. foreach( $field['options'] as $key => $value ) { 
  522. echo "<input type='radio' " . ( isset( $field['style'] ) ? "style='{$field['style']}' " : '' ) . " class='at-radio" . ( isset( $field['class'] ) ? ' ' . $field['class'] : '' ) . "' name='{$field['id']}' value='{$key}'" . checked( in_array( $key, $meta ), true, false ) . " /> <span class='at-radio-label'>{$value}</span>"; 
  523. $this->show_field_end( $field, $meta ); 
  524.  
  525. /** 
  526. * Show Checkbox Field. 
  527. * @param string $field 
  528. * @param string $meta 
  529. * @since 1.0 
  530. * @access public 
  531. */ 
  532. public function show_field_checkbox( $field, $meta ) { 
  533.  
  534. $this->show_field_begin( $field, $meta ); 
  535. echo "<input type='checkbox' " . ( isset( $field['style'] ) ? "style='{$field['style']}' " : '' ) . " class='rw-checkbox" . ( isset( $field['class'] ) ? ' ' . $field['class'] : '' ) . "' name='{$field['id']}' id='{$field['id']}'" . checked( !empty( $meta ), true, false ) . " />"; 
  536. $this->show_field_end( $field, $meta ); 
  537.  
  538.  
  539. /** 
  540. * Show editor Field. 
  541. * @param string $field 
  542. * @param string $meta 
  543. * @since 1.0 
  544. * @access public 
  545. */ 
  546. public function show_field_editor( $field, $meta, $in_repeater = false ) { 
  547. $this->show_field_begin( $field, $meta ); 
  548.  
  549. if( $in_repeater ) { 
  550. echo "<textarea class='at-editor theEditor large-text" . ( isset( $field['class'] ) ? ' ' . $field['class'] : '' ) . "' name='{$field['id']}' id='{$field['id']}' cols='60' rows='10'>{$meta}</textarea>"; 
  551. else { 
  552. $default_settings = array( 'media_buttons' => false ); 
  553.  
  554. // Use new wp_editor() since WP 3.3 
  555. $settings = ( isset( $field['settings'] ) && is_array( $field['settings'] ) ? $field['settings'] : $default_settings ); 
  556. $settings['editor_class'] = 'at-editor' . ( isset( $field['class'] ) ? ' ' . $field['class'] : '' ); 
  557. $id = str_replace( "_", "", $this->stripNumeric( strtolower( $field['id'] ) ) ); 
  558. wp_editor( html_entity_decode( $meta ), $id, $settings ); 
  559. $this->show_field_end( $field, $meta ); 
  560.  
  561. /** 
  562. * Show File Field. 
  563. * @param string $field 
  564. * @param string $meta 
  565. * @since 1.0 
  566. * @access public 
  567. */ 
  568. public function show_field_file( $field, $meta ) { 
  569. wp_enqueue_media(); 
  570. $this->show_field_begin( $field, $meta ); 
  571.  
  572. $std = isset( $field['std'] ) ? $field['std'] : array( 'id' => '', 'url' => '' ); 
  573. $multiple = isset( $field['multiple'] ) ? $field['multiple'] : false; 
  574. $multiple = ( $multiple ) ? "multiFile '" : ""; 
  575. $name = esc_attr( $field['id'] ); 
  576. $value = isset( $meta['id'] ) ? $meta : $std; 
  577. $has_file = ( empty( $value['url'] ) ) ? false : true; 
  578. $type = isset( $field['mime_type'] ) ? $field['mime_type'] : ''; 
  579. $ext = isset( $field['ext'] ) ? $field['ext'] : ''; 
  580. $type = ( is_array( $type ) ? implode( "|", $type ) : $type ); 
  581. $ext = ( is_array( $ext ) ? implode( "|", $ext ) : $ext ); 
  582. $id = $field['id']; 
  583. $li = ( $has_file ) ? "<li><a href='{$value['url']}' target='_blank'>{$value['url']}</a></li>" : ""; 
  584.  
  585. echo "<span class='simplePanelfilePreview'><ul>{$li}</ul></span>"; 
  586. echo "<input type='hidden' name='{$name}[id]' value='{$value['id']}'/>"; 
  587. echo "<input type='hidden' name='{$name}[url]' value='{$value['url']}'/>"; 
  588. if( $has_file ) { 
  589. echo "<input type='button' class='{$multiple} button simplePanelfileUploadclear' id='{$id}' value='Remove File' data-mime_type='{$type}' data-ext='{$ext}'/>"; 
  590. else { 
  591. echo "<input type='button' class='{$multiple} button simplePanelfileUpload' id='{$id}' value='Upload File' data-mime_type='{$type}' data-ext='{$ext}'/>"; 
  592.  
  593. $this->show_field_end( $field, $meta ); 
  594.  
  595. /** 
  596. * Show Image Field. 
  597. * @param array $field 
  598. * @param array $meta 
  599. * @since 1.0 
  600. * @access public 
  601. */ 
  602. public function show_field_image( $field, $meta ) { 
  603. wp_enqueue_media(); 
  604.  
  605. $this->show_field_begin( $field, $meta ); 
  606.  
  607. $std = isset( $field['std'] ) ? $field['std'] : array( 'id' => '', 'url' => '' ); 
  608. $name = esc_attr( $field['id'] ); 
  609. $value = isset( $meta['id'] ) ? $meta : $std; 
  610.  
  611. $value['url'] = isset( $meta['src'] ) ? $meta['src'] : $value['url']; //backwords capability 
  612. $has_image = empty( $value['url'] ) ? false : true; 
  613. $w = isset( $field['width'] ) ? $field['width'] : 'auto'; 
  614. $h = isset( $field['height'] ) ? $field['height'] : 'auto'; 
  615. $PreviewStyle = "style='width: $w; height: $h;" . ( ( !$has_image ) ? "display: none;'" : "'" ); 
  616. $id = $field['id']; 
  617. $multiple = isset( $field['multiple'] ) ? $field['multiple'] : false; 
  618. $multiple = ( $multiple ) ? "multiFile " : ""; 
  619.  
  620. echo "<span class='simplePanelImagePreview'><img {$PreviewStyle} src='{$value['url']}'><br/></span>"; 
  621. echo "<input type='hidden' name='{$name}[id]' value='{$value['id']}'/>"; 
  622. echo "<input type='hidden' name='{$name}[url]' value='{$value['url']}'/>"; 
  623. if( $has_image ) { 
  624. echo "<input class='{$multiple} button simplePanelimageUploadclear' id='{$id}' value='Remove Image' type='button'/>"; 
  625. else { 
  626. echo "<input class='{$multiple} button simplePanelimageUpload' id='{$id}' value='Upload Image' type='button'/>"; 
  627. $this->show_field_end( $field, $meta ); 
  628.  
  629. /** 
  630. * Show Color Field. 
  631. * @param string $field 
  632. * @param string $meta 
  633. * @since 1.0 
  634. * @access public 
  635. */ 
  636. public function show_field_color( $field, $meta ) { 
  637.  
  638. if( empty( $meta ) ) { 
  639. $meta = '#'; 
  640.  
  641. $this->show_field_begin( $field, $meta ); 
  642. echo '<div class="input-prepend ' . $field['class'] . '"><input class="of-color" name="' . $field['id'] . '" id="' . $field['id'] . '" type="text" value="' . $meta . '" />'; 
  643. echo '<div id="' . $field['id'] . '_picker' . '" class="add-on colorSelector"><div style="' . esc_attr( 'background-color:' . $meta ) . '"></div></div></div>'; 
  644.  
  645. $this->show_field_end( $field, $meta ); 
  646.  
  647.  
  648. /** 
  649. * Show Checkbox List Field 
  650. * @param string $field 
  651. * @param string $meta 
  652. * @since 1.0 
  653. * @access public 
  654. */ 
  655. public function show_field_checkbox_list( $field, $meta ) { 
  656.  
  657. if( !is_array( $meta ) ) { 
  658. $meta = (array)$meta; 
  659.  
  660. $this->show_field_begin( $field, $meta ); 
  661.  
  662. $html = array(); 
  663.  
  664. foreach( $field['options'] as $key => $value ) { 
  665. $html[] = "<input type='checkbox' " . ( isset( $field['style'] ) ? "style='{$field['style']}' " : '' ) . " class='at-checkbox_list" . ( isset( $field['class'] ) ? ' ' . $field['class'] : '' ) . "' name='{$field['id']}[]' value='{$key}'" . checked( in_array( $key, $meta ), true, false ) . " /> {$value}"; 
  666.  
  667. echo implode( '<br />', $html ); 
  668.  
  669. $this->show_field_end( $field, $meta ); 
  670.  
  671.  
  672. /** 
  673. * Show Date Field. 
  674. * @param string $field 
  675. * @param string $meta 
  676. * @since 1.0 
  677. * @access public 
  678. */ 
  679. public function show_field_date( $field, $meta ) { 
  680. $this->show_field_begin( $field, $meta ); 
  681. echo "<input type='text' " . ( isset( $field['style'] ) ? "style='{$field['style']}' " : '' ) . " class='at-date" . ( isset( $field['class'] ) ? ' ' . $field['class'] : '' ) . "' name='{$field['id']}' id='{$field['id']}' rel='{$field['format']}' value='{$meta}' size='30' />"; 
  682. $this->show_field_end( $field, $meta ); 
  683.  
  684. /** 
  685. * Show time field. 
  686. * @param string $field 
  687. * @param string $meta 
  688. * @since 1.0 
  689. * @access public 
  690. */ 
  691. public function show_field_time( $field, $meta ) { 
  692. $this->show_field_begin( $field, $meta ); 
  693. $ampm = ( $field['ampm'] ) ? 'true' : 'false'; 
  694. echo "<input type='text' " . ( isset( $field['style'] ) ? "style='{$field['style']}' " : '' ) . " class='at-time" . ( isset( $field['class'] ) ? ' ' . $field['class'] : '' ) . "' name='{$field['id']}' id='{$field['id']}' data-ampm='{$ampm}' rel='{$field['format']}' value='{$meta}' size='30' />"; 
  695. $this->show_field_end( $field, $meta ); 
  696.  
  697. /** 
  698. * Show Posts field. 
  699. * used creating a posts/pages/custom types checkboxlist or a select dropdown 
  700. * @param string $field 
  701. * @param string $meta 
  702. * @since 1.0 
  703. * @access public 
  704. */ 
  705. public function show_field_posts( $field, $meta ) { 
  706. global $post; 
  707.  
  708. if( !is_array( $meta ) ) { 
  709. $meta = (array)$meta; 
  710. $this->show_field_begin( $field, $meta ); 
  711. $options = $field['options']; 
  712. $posts = get_posts( $options['args'] ); 
  713. // checkbox_list 
  714. if( 'checkbox_list' == $options['type'] ) { 
  715. foreach( $posts as $p ) { 
  716. echo "<input type='checkbox' " . ( isset( $field['style'] ) ? "style='{$field['style']}' " : '' ) . " class='at-posts-checkbox" . ( isset( $field['class'] ) ? ' ' . $field['class'] : '' ) . "' name='{$field['id']}[]' value='$p->ID'" . checked( in_array( $p->ID, $meta ), true, false ) . " /> $p->post_title<br/>"; 
  717. // select 
  718. else { 
  719. echo "<select " . ( isset( $field['style'] ) ? "style='{$field['style']}' " : '' ) . " class='at-posts-select" . ( isset( $field['class'] ) ? ' ' . $field['class'] : '' ) . "' name='{$field['id']}" . ( $field['multiple'] ? "[]' multiple='multiple' style='height:auto'" : "'" ) . ">"; 
  720. foreach( $posts as $p ) { 
  721. echo "<option value='$p->ID'" . selected( in_array( $p->ID, $meta ), true, false ) . ">$p->post_title</option>"; 
  722. echo "</select>"; 
  723.  
  724. $this->show_field_end( $field, $meta ); 
  725.  
  726. /** 
  727. * Show Taxonomy field. 
  728. * used creating a category/tags/custom taxonomy checkboxlist or a select dropdown 
  729. * @param string $field 
  730. * @param string $meta 
  731. * @since 1.0 
  732. * @access public 
  733. * @uses get_terms() 
  734. */ 
  735. public function show_field_taxonomy( $field, $meta ) { 
  736. global $post; 
  737.  
  738. if( !is_array( $meta ) ) { 
  739. $meta = (array)$meta; 
  740. $this->show_field_begin( $field, $meta ); 
  741. $options = $field['options']; 
  742. $terms = get_terms( $options['taxonomy'], $options['args'] ); 
  743.  
  744. // checkbox_list 
  745. if( 'checkbox_list' == $options['type'] ) { 
  746. foreach( $terms as $term ) { 
  747. echo "<input type='checkbox' " . ( isset( $field['style'] ) ? "style='{$field['style']}' " : '' ) . " class='at-tax-checkbox" . ( isset( $field['class'] ) ? ' ' . $field['class'] : '' ) . "' name='{$field['id']}[]' value='$term->slug'" . checked( in_array( $term->slug, $meta ), true, false ) . " /> $term->name<br/>"; 
  748. // select 
  749. else { 
  750. echo "<select " . ( isset( $field['style'] ) ? "style='{$field['style']}' " : '' ) . " class='at-tax-select" . ( isset( $field['class'] ) ? ' ' . $field['class'] : '' ) . "' name='{$field['id']}" . ( $field['multiple'] ? "[]' multiple='multiple' style='height:auto'" : "'" ) . ">"; 
  751. foreach( $terms as $term ) { 
  752. echo "<option value='$term->slug'" . selected( in_array( $term->slug, $meta ), true, false ) . ">$term->name</option>"; 
  753. echo "</select>"; 
  754.  
  755. $this->show_field_end( $field, $meta ); 
  756.  
  757. /** 
  758. * Show conditinal Checkbox Field. 
  759. * @param string $field 
  760. * @param string $meta 
  761. * @since 2.9.9 
  762. * @access public 
  763. */ 
  764. public function show_field_cond( $field, $meta ) { 
  765.  
  766. $this->show_field_begin( $field, $meta ); 
  767. $checked = false; 
  768. if( is_array( $meta ) && isset( $meta['enabled'] ) && $meta['enabled'] == 'on' ) { 
  769. $checked = true; 
  770. echo "<input type='checkbox' class='conditinal_control' name='{$field['id']}[enabled]' id='{$field['id']}'" . checked( $checked, true, false ) . " />"; 
  771. //start showing the fields 
  772. $display = ( $checked ) ? '' : ' style="display: none;"'; 
  773.  
  774. echo '<div class="conditinal_container"' . $display . '><table>'; 
  775. foreach( (array)$field['fields'] as $f ) { 
  776. //reset var $id for cond 
  777. $id = ''; 
  778. $id = $field['id'] . '[' . $f['id'] . ']'; 
  779. $m = ''; 
  780. $m = ( isset( $meta[$f['id']] ) ) ? $meta[$f['id']] : ''; 
  781. $m = ( $m !== '' ) ? $m : ( isset( $f['std'] ) ? $f['std'] : '' ); 
  782. if( 'image' != $f['type'] && $f['type'] != 'repeater' ) { 
  783. $m = is_array( $m ) ? array_map( 'esc_attr', $m ) : esc_attr( $m ); 
  784. //set new id for field in array format 
  785. $f['id'] = $id; 
  786. echo '<tr>'; 
  787. call_user_func( array( $this, 'show_field_' . $f['type'] ), $f, $m ); 
  788. echo '</tr>'; 
  789. echo '</table></div>'; 
  790. $this->show_field_end( $field, $meta ); 
  791.  
  792. /** 
  793. * Show Help Field. 
  794. * @param string $field 
  795. * @param string $meta 
  796. * @since 1.0 
  797. * @access public 
  798. */ 
  799. public function show_field_help( $field, $meta ) { 
  800.  
  801. $this->show_field_begin( $field, $meta ); 
  802. echo '<span class="help-message">' . $field['help_text'] . '</span>'; 
  803. $this->show_field_end( $field, $meta ); 
  804.  
  805. /** 
  806. * Save Data from Metabox 
  807. * @param string $post_id 
  808. * @since 1.0 
  809. * @access public 
  810. */ 
  811. public function save( $post_id ) { 
  812.  
  813. global $post_type; 
  814.  
  815. $post_type_object = get_post_type_object( $post_type ); 
  816.  
  817. if( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) // Check Autosave 
  818. || ( !isset( $_POST['post_ID'] ) || $post_id != $_POST['post_ID'] ) // Check Revision 
  819. || ( !in_array( $post_type, $this->_meta_box['pages'] ) ) // Check if current post type is supported. 
  820. || ( !check_admin_referer( basename( __FILE__ ), 'at_meta_box_nonce' ) ) // Check nonce - Security 
  821. || ( !current_user_can( $post_type_object->cap->edit_post, $post_id ) ) 
  822. ) // Check permission 
  823. return $post_id; 
  824.  
  825. foreach( $this->_fields as $field ) { 
  826.  
  827. $name = $field['id']; 
  828. $type = $field['type']; 
  829. $old = get_post_meta( $post_id, $name, !$field['multiple'] ); 
  830. $new = ( isset( $_POST[$name] ) ) ? $_POST[$name] : ( ( $field['multiple'] ) ? array() : '' ); 
  831.  
  832. // Validate meta value 
  833. if( class_exists( 'at_Meta_Box_Validate' ) && method_exists( 'at_Meta_Box_Validate', $field['validate_func'] ) ) { 
  834. $new = call_user_func( array( 'at_Meta_Box_Validate', $field['validate_func'] ), $new ); 
  835.  
  836. //skip on Paragraph field 
  837. if( $type != "paragraph" ) { 
  838.  
  839. // Call defined method to sanitize meta value 
  840. $sanitize_func = 'sanitize_field_' . $type; 
  841.  
  842. if( method_exists( $this, $sanitize_func ) ) { 
  843. $new = call_user_func( array( $this, $sanitize_func ), $new, $old ); 
  844.  
  845. // Call defined method to save meta value, if there's no methods, call common one. 
  846. $save_func = 'save_field_' . $type; 
  847. if( method_exists( $this, $save_func ) ) { 
  848. call_user_func( array( $this, $save_func ), $post_id, $field, $old, $new ); 
  849. else { 
  850. $this->save_field( $post_id, $field, $old, $new ); 
  851.  
  852. } // End foreach 
  853.  
  854. /** 
  855. * Common function for saving fields. 
  856. * @param string $post_id 
  857. * @param string $field 
  858. * @param string $old 
  859. * @param string|mixed $new 
  860. * @since 1.0 
  861. * @access public 
  862. */ 
  863. public function save_field( $post_id, $field, $old, $new ) { 
  864.  
  865. $name = $field['id']; 
  866. delete_post_meta( $post_id, $name ); 
  867. if( $new === '' || $new === array() ) { 
  868. return; 
  869. if( $field['multiple'] ) { 
  870. foreach( $new as $add_new ) { 
  871. add_post_meta( $post_id, $name, $add_new, false ); 
  872. else { 
  873. update_post_meta( $post_id, $name, $new ); 
  874.  
  875. /** 
  876. * function for saving image field. 
  877. * @param string $post_id 
  878. * @param string $field 
  879. * @param string $old 
  880. * @param string|mixed $new 
  881. * @since 1.7 
  882. * @access public 
  883. */ 
  884. public function save_field_image( $post_id, $field, $old, $new ) { 
  885. $name = $field['id']; 
  886. delete_post_meta( $post_id, $name ); 
  887. if( $new === '' || $new === array() || $new['id'] == '' || $new['url'] == '' ) { 
  888. return; 
  889.  
  890. update_post_meta( $post_id, $name, $new ); 
  891.  
  892. /** 
  893. * Save Editor Field. 
  894. * @param string $post_id  
  895. * @param string $field  
  896. * @param string $old  
  897. * @param string $new  
  898. * @since 1.0 
  899. * @access public  
  900. */ 
  901. public function save_field_editor( $post_id, $field, $old, $new ) { 
  902. $id = str_replace( "_", "", $this->stripNumeric( strtolower( $field['id'] ) ) ); 
  903. $new = ( isset( $_POST[$id] ) ) ? $_POST[$id] : ( ( $field['multiple'] ) ? array() : '' ); 
  904. $this->save_field( $post_id, $field, $old, $new ); 
  905.  
  906. /** 
  907. * Save repeater Fields. 
  908. * @param string $post_id 
  909. * @param string $field 
  910. * @param string|mixed $old 
  911. * @param string|mixed $new 
  912. * @since 1.0 
  913. * @access public 
  914. */ 
  915. public function save_field_repeater( $post_id, $field, $old, $new ) { 
  916. if( is_array( $new ) && count( $new ) > 0 ) { 
  917. foreach( $new as $n ) { 
  918. foreach( $field['fields'] as $f ) { 
  919. $type = $f['type']; 
  920. switch( $type ) { 
  921. case 'editor': 
  922. $n[$f['id']] = wpautop( $n[$f['id']] ); 
  923. break; 
  924. default: 
  925. break; 
  926. if( !$this->is_array_empty( $n ) ) { 
  927. $temp[] = $n; 
  928. if( isset( $temp ) && count( $temp ) > 0 && !$this->is_array_empty( $temp ) ) { 
  929. update_post_meta( $post_id, $field['id'], $temp ); 
  930. else { 
  931. // remove old meta if exists 
  932. delete_post_meta( $post_id, $field['id'] ); 
  933. else { 
  934. // remove old meta if exists 
  935. delete_post_meta( $post_id, $field['id'] ); 
  936.  
  937. /** 
  938. * Save File Field. 
  939. * @param string $post_id 
  940. * @param string $field 
  941. * @param string $old 
  942. * @param string $new 
  943. * @since 1.0 
  944. * @access public 
  945. */ 
  946. public function save_field_file( $post_id, $field, $old, $new ) { 
  947.  
  948. $name = $field['id']; 
  949. delete_post_meta( $post_id, $name ); 
  950. if( $new === '' || $new === array() || $new['id'] == '' || $new['url'] == '' ) { 
  951. return; 
  952.  
  953. update_post_meta( $post_id, $name, $new ); 
  954.  
  955. /** 
  956. * Save repeater File Field. 
  957. * @param string $post_id 
  958. * @param string $field 
  959. * @param string $old 
  960. * @param string $new 
  961. * @since 1.0 
  962. * @access public 
  963. * @deprecated 3.0.7 
  964. */ 
  965. public function save_field_file_repeater( $post_id, $field, $old, $new ) { 
  966.  
  967. /** 
  968. * Save Checkbox Field. 
  969. * @param string $post_id 
  970. * @param string $field 
  971. * @param string $old 
  972. * @param string $new 
  973. * @since 1.0 
  974. * @access public 
  975. */ 
  976. function save_field_checkbox( $post_id, $field, $old, $new ) { 
  977. $new = $new ? "1" : "0"; 
  978. update_post_meta( $post_id, $field['id'], $new ); 
  979.  
  980. /** 
  981. * Add missed values for meta box. 
  982. * @since 1.0 
  983. * @access public 
  984. */ 
  985. public function add_missed_values() { 
  986.  
  987. // Default values for meta box 
  988. $this->_meta_box = array_merge( array( 'context' => 'normal', 'priority' => 'high', 'pages' => array( 'post' ) ), (array)$this->_meta_box ); 
  989.  
  990. // Default values for fields 
  991. foreach( $this->_fields as &$field ) { 
  992.  
  993. $multiple = in_array( $field['type'], array( 'checkbox_list', 'file', 'image' ) ); 
  994. $std = $multiple ? array() : ''; 
  995. $format = 'date' == $field['type'] ? 'yy-mm-dd' : ( 'time' == $field['type'] ? 'hh:mm' : '' ); 
  996.  
  997. $field = array_merge( array( 'multiple' => $multiple, 'std' => $std, 'desc' => '', 'format' => $format, 'validate_func' => '' ), $field ); 
  998.  
  999. } // End foreach 
  1000.  
  1001.  
  1002. /** 
  1003. * Check if field with $type exists. 
  1004. * @param string $type 
  1005. * @since 1.0 
  1006. * @access public 
  1007. */ 
  1008. public function has_field( $type ) { 
  1009. //faster search in single dimention array. 
  1010. if( count( $this->field_types ) > 0 ) { 
  1011. return in_array( $type, $this->field_types ); 
  1012.  
  1013. //run once over all fields and store the types in a local array 
  1014. $temp = array(); 
  1015. foreach( $this->_fields as $field ) { 
  1016. $temp[] = $field['type']; 
  1017. if( 'repeater' == $field['type'] || 'cond' == $field['type'] ) { 
  1018. foreach( (array)$field["fields"] as $repeater_field ) { 
  1019. $temp[] = $repeater_field["type"]; 
  1020.  
  1021. //remove duplicates 
  1022. $this->field_types = array_unique( $temp ); 
  1023.  
  1024. //call this function one more time now that we have an array of field types 
  1025. return $this->has_field( $type ); 
  1026.  
  1027. /** 
  1028. * Check if current page is edit page. 
  1029. * @since 1.0 
  1030. * @access public 
  1031. */ 
  1032. public function is_edit_page() { 
  1033. global $pagenow; 
  1034.  
  1035. return in_array( $pagenow, array( 'post.php', 'post-new.php' ) ); 
  1036.  
  1037. /** 
  1038. * Fixes the odd indexing of multiple file uploads. 
  1039. * Goes from the format: 
  1040. * $_FILES['field']['key']['index'] 
  1041. * to 
  1042. * The More standard and appropriate: 
  1043. * $_FILES['field']['index']['key'] 
  1044. * @param string $files 
  1045. * @since 1.0 
  1046. * @access public 
  1047. */ 
  1048. public function fix_file_array( &$files ) { 
  1049.  
  1050. $output = array(); 
  1051.  
  1052. foreach( $files as $key => $list ) { 
  1053. foreach( $list as $index => $value ) { 
  1054. $output[$index][$key] = $value; 
  1055.  
  1056. return $output; 
  1057.  
  1058.  
  1059. /** 
  1060. * Get proper JQuery UI version. 
  1061. * Used in order to not conflict with WP Admin Scripts. 
  1062. * @since 1.0 
  1063. * @access public 
  1064. */ 
  1065. public function get_jqueryui_ver() { 
  1066.  
  1067. global $wp_version; 
  1068.  
  1069. if( version_compare( $wp_version, '3.1', '>=' ) ) { 
  1070. return '1.8.10'; 
  1071.  
  1072. return '1.7.3'; 
  1073.  
  1074.  
  1075. /** 
  1076. * Add Field to meta box (generic function) 
  1077. * @author Ohad Raz 
  1078. * @since 1.2 
  1079. * @access public 
  1080. * @param $id string field id, i.e. the meta key 
  1081. * @param $args mixed|array 
  1082. */ 
  1083. public function addField( $id, $args ) { 
  1084. $new_field = array( 'id' => $id, 'std' => '', 'desc' => '', 'style' => '' ); 
  1085. $new_field = array_merge( $new_field, $args ); 
  1086. $this->_fields[] = $new_field; 
  1087.  
  1088. /** 
  1089. * Add Text Field to meta box 
  1090. * @author Ohad Raz 
  1091. * @since 1.0 
  1092. * @access public 
  1093. * @param $id string field id, i.e. the meta key 
  1094. * @param $args mixed|array 
  1095. * 'name' => // field name/label string optional 
  1096. * 'desc' => // field description, string optional 
  1097. * 'std' => // default value, string optional 
  1098. * 'style' => // custom style for field, string optional 
  1099. * 'validate_func' => // validate function, string optional 
  1100. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1101. */ 
  1102. public function addText( $id, $args, $repeater = false ) { 
  1103. $new_field = array( 'type' => 'text', 'id' => $id, 'std' => '', 'desc' => '', 'style' => '', 'name' => 'Text Field' ); 
  1104. $new_field = array_merge( $new_field, $args ); 
  1105. if( false === $repeater ) { 
  1106. $this->_fields[] = $new_field; 
  1107. else { 
  1108. return $new_field; 
  1109.  
  1110. /** 
  1111. * Add Number Field to meta box 
  1112. * @author Ohad Raz 
  1113. * @since 1.0 
  1114. * @access public 
  1115. * @param $id string field id, i.e. the meta key 
  1116. * @param $args mixed|array 
  1117. * 'name' => // field name/label string optional 
  1118. * 'desc' => // field description, string optional 
  1119. * 'std' => // default value, string optional 
  1120. * 'style' => // custom style for field, string optional 
  1121. * 'validate_func' => // validate function, string optional 
  1122. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1123. */ 
  1124. public function addNumber( $id, $args, $repeater = false ) { 
  1125. $new_field = array( 'type' => 'number', 'id' => $id, 'std' => '0', 'desc' => '', 'style' => '', 'name' => 'Number Field', 'step' => '1', 'min' => '0' ); 
  1126. $new_field = array_merge( $new_field, $args ); 
  1127. if( false === $repeater ) { 
  1128. $this->_fields[] = $new_field; 
  1129. else { 
  1130. return $new_field; 
  1131.  
  1132. /** 
  1133. * Add Hidden Field to meta box 
  1134. * @author Ohad Raz 
  1135. * @since 0.1.3 
  1136. * @access public 
  1137. * @param $id string field id, i.e. the meta key 
  1138. * @param $args mixed|array 
  1139. * 'name' => // field name/label string optional 
  1140. * 'desc' => // field description, string optional 
  1141. * 'std' => // default value, string optional 
  1142. * 'style' => // custom style for field, string optional 
  1143. * 'validate_func' => // validate function, string optional 
  1144. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1145. */ 
  1146. public function addHidden( $id, $args, $repeater = false ) { 
  1147. $new_field = array( 'type' => 'hidden', 'id' => $id, 'std' => '', 'desc' => '', 'style' => '', 'name' => 'Text Field' ); 
  1148. $new_field = array_merge( $new_field, $args ); 
  1149. if( false === $repeater ) { 
  1150. $this->_fields[] = $new_field; 
  1151. else { 
  1152. return $new_field; 
  1153.  
  1154. /** 
  1155. * Add Paragraph to meta box 
  1156. * @author Ohad Raz 
  1157. * @since 0.1.3 
  1158. * @access public 
  1159. * @param $id string field id, i.e. the meta key 
  1160. * @param $value paragraph html 
  1161. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1162. */ 
  1163. public function addParagraph( $id, $args, $repeater = false ) { 
  1164. $new_field = array( 'type' => 'paragraph', 'id' => $id, 'value' => '' ); 
  1165. $new_field = array_merge( $new_field, $args ); 
  1166. if( false === $repeater ) { 
  1167. $this->_fields[] = $new_field; 
  1168. else { 
  1169. return $new_field; 
  1170.  
  1171. /** 
  1172. * Add Checkbox Field to meta box 
  1173. * @author Ohad Raz 
  1174. * @since 1.0 
  1175. * @access public 
  1176. * @param $id string field id, i.e. the meta key 
  1177. * @param $args mixed|array 
  1178. * 'name' => // field name/label string optional 
  1179. * 'desc' => // field description, string optional 
  1180. * 'std' => // default value, string optional 
  1181. * 'validate_func' => // validate function, string optional 
  1182. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1183. */ 
  1184. public function addCheckbox( $id, $args, $repeater = false ) { 
  1185. $new_field = array( 'type' => 'checkbox', 'id' => $id, 'std' => '', 'desc' => '', 'style' => '', 'name' => 'Checkbox Field' ); 
  1186. $new_field = array_merge( $new_field, $args ); 
  1187. if( false === $repeater ) { 
  1188. $this->_fields[] = $new_field; 
  1189. else { 
  1190. return $new_field; 
  1191.  
  1192. /** 
  1193. * Add CheckboxList Field to meta box 
  1194. * @author Ohad Raz 
  1195. * @since 1.0 
  1196. * @access public 
  1197. * @param $id string field id, i.e. the meta key 
  1198. * @param $options (array) array of key => value pairs for select options 
  1199. * @param $args mixed|array 
  1200. * 'name' => // field name/label string optional 
  1201. * 'desc' => // field description, string optional 
  1202. * 'std' => // default value, string optional 
  1203. * 'validate_func' => // validate function, string optional 
  1204. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1205. * @return : remember to call: $checkbox_list = get_post_meta(get_the_ID(), 'meta_name', false); 
  1206. * which means the last param as false to get the values in an array 
  1207. */ 
  1208. public function addCheckboxList( $id, $options, $args, $repeater = false ) { 
  1209. $new_field = array( 'type' => 'checkbox_list', 'id' => $id, 'std' => '', 'desc' => '', 'style' => '', 'name' => 'Checkbox List Field', 'options' => $options, 'multiple' => true, ); 
  1210. $new_field = array_merge( $new_field, $args ); 
  1211. if( false === $repeater ) { 
  1212. $this->_fields[] = $new_field; 
  1213. else { 
  1214. return $new_field; 
  1215.  
  1216. /** 
  1217. * Add Textarea Field to meta box 
  1218. * @author Ohad Raz 
  1219. * @since 1.0 
  1220. * @access public 
  1221. * @param $id string field id, i.e. the meta key 
  1222. * @param $args mixed|array 
  1223. * 'name' => // field name/label string optional 
  1224. * 'desc' => // field description, string optional 
  1225. * 'std' => // default value, string optional 
  1226. * 'style' => // custom style for field, string optional 
  1227. * 'validate_func' => // validate function, string optional 
  1228. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1229. */ 
  1230. public function addTextarea( $id, $args, $repeater = false ) { 
  1231. $new_field = array( 'type' => 'textarea', 'id' => $id, 'std' => '', 'desc' => '', 'style' => '', 'name' => 'Textarea Field' ); 
  1232. $new_field = array_merge( $new_field, $args ); 
  1233. if( false === $repeater ) { 
  1234. $this->_fields[] = $new_field; 
  1235. else { 
  1236. return $new_field; 
  1237.  
  1238. /** 
  1239. * Add Select Field to meta box 
  1240. * @author Ohad Raz 
  1241. * @since 1.0 
  1242. * @access public 
  1243. * @param $id string field id, i.e. the meta key 
  1244. * @param $options (array) array of key => value pairs for select options 
  1245. * @param $args mixed|array 
  1246. * 'name' => // field name/label string optional 
  1247. * 'desc' => // field description, string optional 
  1248. * 'std' => // default value, (array) optional 
  1249. * 'multiple' => // select multiple values, optional. Default is false. 
  1250. * 'validate_func' => // validate function, string optional 
  1251. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1252. */ 
  1253. public function addSelect( $id, $options, $args, $repeater = false ) { 
  1254. $new_field = array( 'type' => 'select', 'id' => $id, 'std' => array(), 'desc' => '', 'style' => '', 'name' => 'Select Field', 'multiple' => false, 'options' => $options ); 
  1255. $new_field = array_merge( $new_field, $args ); 
  1256. if( false === $repeater ) { 
  1257. $this->_fields[] = $new_field; 
  1258. else { 
  1259. return $new_field; 
  1260.  
  1261. /** 
  1262. * Add Radio Field to meta box 
  1263. * @author Ohad Raz 
  1264. * @since 1.0 
  1265. * @access public 
  1266. * @param $id string field id, i.e. the meta key 
  1267. * @param $options (array) array of key => value pairs for radio options 
  1268. * @param $args mixed|array 
  1269. * 'name' => // field name/label string optional 
  1270. * 'desc' => // field description, string optional 
  1271. * 'std' => // default value, string optional 
  1272. * 'validate_func' => // validate function, string optional 
  1273. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1274. */ 
  1275. public function addRadio( $id, $options, $args, $repeater = false ) { 
  1276. $new_field = array( 'type' => 'radio', 'id' => $id, 'std' => array(), 'desc' => '', 'style' => '', 'name' => 'Radio Field', 'options' => $options ); 
  1277. $new_field = array_merge( $new_field, $args ); 
  1278. if( false === $repeater ) { 
  1279. $this->_fields[] = $new_field; 
  1280. else { 
  1281. return $new_field; 
  1282.  
  1283. /** 
  1284. * Add Date Field to meta box 
  1285. * @author Ohad Raz 
  1286. * @since 1.0 
  1287. * @access public 
  1288. * @param $id string field id, i.e. the meta key 
  1289. * @param $args mixed|array 
  1290. * 'name' => // field name/label string optional 
  1291. * 'desc' => // field description, string optional 
  1292. * 'std' => // default value, string optional 
  1293. * 'validate_func' => // validate function, string optional 
  1294. * 'format' => // date format, default yy-mm-dd. Optional. Default "'d MM, yy'" See more formats here: http://goo.gl/Wcwxn 
  1295. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1296. */ 
  1297. public function addDate( $id, $args, $repeater = false ) { 
  1298. $new_field = array( 'type' => 'date', 'id' => $id, 'std' => '', 'desc' => '', 'format' => 'd MM, yy', 'name' => 'Date Field' ); 
  1299. $new_field = array_merge( $new_field, $args ); 
  1300. if( false === $repeater ) { 
  1301. $this->_fields[] = $new_field; 
  1302. else { 
  1303. return $new_field; 
  1304.  
  1305. /** 
  1306. * Add Time Field to meta box 
  1307. * @author Ohad Raz 
  1308. * @since 1.0 
  1309. * @access public 
  1310. * @param $id string- field id, i.e. the meta key 
  1311. * @param $args mixed|array 
  1312. * 'name' => // field name/label string optional 
  1313. * 'desc' => // field description, string optional 
  1314. * 'std' => // default value, string optional 
  1315. * 'validate_func' => // validate function, string optional 
  1316. * 'format' => // time format, default hh:mm. Optional. See more formats here: http://goo.gl/83woX 
  1317. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1318. */ 
  1319. public function addTime( $id, $args, $repeater = false ) { 
  1320. $new_field = array( 'type' => 'time', 'id' => $id, 'std' => '', 'desc' => '', 'format' => 'hh:mm', 'name' => 'Time Field', 'ampm' => false ); 
  1321. $new_field = array_merge( $new_field, $args ); 
  1322. if( false === $repeater ) { 
  1323. $this->_fields[] = $new_field; 
  1324. else { 
  1325. return $new_field; 
  1326.  
  1327. /** 
  1328. * Add Color Field to meta box 
  1329. * @author Ohad Raz 
  1330. * @since 1.0 
  1331. * @access public 
  1332. * @param $id string field id, i.e. the meta key 
  1333. * @param $args mixed|array 
  1334. * 'name' => // field name/label string optional 
  1335. * 'desc' => // field description, string optional 
  1336. * 'std' => // default value, string optional 
  1337. * 'validate_func' => // validate function, string optional 
  1338. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1339. */ 
  1340. public function addColor( $id, $args, $repeater = false ) { 
  1341. $new_field = array( 'type' => 'color', 'id' => $id, 'std' => '', 'desc' => '', 'name' => 'ColorPicker Field' ); 
  1342. $new_field = array_merge( $new_field, $args ); 
  1343. if( false === $repeater ) { 
  1344. $this->_fields[] = $new_field; 
  1345. else { 
  1346. return $new_field; 
  1347.  
  1348. /** 
  1349. * Add Image Field to meta box 
  1350. * @author Ohad Raz 
  1351. * @since 1.0 
  1352. * @access public 
  1353. * @param $id string field id, i.e. the meta key 
  1354. * @param $args mixed|array 
  1355. * 'name' => // field name/label string optional 
  1356. * 'desc' => // field description, string optional 
  1357. * 'validate_func' => // validate function, string optional 
  1358. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1359. */ 
  1360. public function addImage( $id, $args, $repeater = false ) { 
  1361. $new_field = array( 'type' => 'image', 'id' => $id, 'desc' => '', 'name' => 'Image Field', 'std' => array( 'id' => '', 'url' => '' ), 'multiple' => false ); 
  1362. $new_field = array_merge( $new_field, $args ); 
  1363. if( false === $repeater ) { 
  1364. $this->_fields[] = $new_field; 
  1365. else { 
  1366. return $new_field; 
  1367.  
  1368. /** 
  1369. * Add File Field to meta box 
  1370. * @author Ohad Raz 
  1371. * @since 1.0 
  1372. * @access public 
  1373. * @param $id string field id, i.e. the meta key 
  1374. * @param $args mixed|array 
  1375. * 'name' => // field name/label string optional 
  1376. * 'desc' => // field description, string optional 
  1377. * 'validate_func' => // validate function, string optional 
  1378. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1379. */ 
  1380. public function addFile( $id, $args, $repeater = false ) { 
  1381. $new_field = array( 'type' => 'file', 'id' => $id, 'desc' => '', 'name' => 'File Field', 'multiple' => false, 'std' => array( 'id' => '', 'url' => '' ) ); 
  1382. $new_field = array_merge( $new_field, $args ); 
  1383. if( false === $repeater ) { 
  1384. $this->_fields[] = $new_field; 
  1385. else { 
  1386. return $new_field; 
  1387.  
  1388. /** 
  1389. * Add Editor Field to meta box 
  1390. * @author Ohad Raz 
  1391. * @since 1.0 
  1392. * @access public 
  1393. * @param $id string field id, i.e. the meta key 
  1394. * @param $args mixed|array 
  1395. * 'name' => // field name/label string optional 
  1396. * 'desc' => // field description, string optional 
  1397. * 'std' => // default value, string optional 
  1398. * 'style' => // custom style for field, string optional Default 'width: 300px; height: 400px' 
  1399. * 'validate_func' => // validate function, string optional 
  1400. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1401. */ 
  1402. public function addEditor( $id, $args, $repeater = false ) { 
  1403. $new_field = array( 'type' => 'editor', 'id' => $id, 'std' => '', 'desc' => '', 'style' => 'width: 300px; height: 400px', 'name' => 'Editor Editor Field' ); 
  1404. $new_field = array_merge( $new_field, $args ); 
  1405. if( false === $repeater ) { 
  1406. $this->_fields[] = $new_field; 
  1407. else { 
  1408. return $new_field; 
  1409.  
  1410. /** 
  1411. * Add Taxonomy Field to meta box 
  1412. * @author Ohad Raz 
  1413. * @since 1.0 
  1414. * @access public 
  1415. * @param $id string field id, i.e. the meta key 
  1416. * @param $options mixed|array options of taxonomy field 
  1417. * 'taxonomy' => // taxonomy name can be category, post_tag or any custom taxonomy default is category 
  1418. * 'type' => // how to show taxonomy? 'select' (default) or 'checkbox_list' 
  1419. * 'args' => // arguments to query taxonomy, see http://goo.gl/uAANN default ('hide_empty' => false) 
  1420. * @param $args mixed|array 
  1421. * 'name' => // field name/label string optional 
  1422. * 'desc' => // field description, string optional 
  1423. * 'std' => // default value, string optional 
  1424. * 'validate_func' => // validate function, string optional 
  1425. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1426. */ 
  1427. public function addTaxonomy( $id, $options, $args, $repeater = false ) { 
  1428. $temp = array( 
  1429. 'args' => array( 'hide_empty' => 0 ),  
  1430. 'tax' => 'category',  
  1431. 'type' => 'select' 
  1432. ); 
  1433. $options = array_merge( $temp, $options ); 
  1434. $new_field = array( 'type' => 'taxonomy', 'id' => $id, 'desc' => '', 'name' => 'Taxonomy Field', 'options' => $options ); 
  1435. $new_field = array_merge( $new_field, $args ); 
  1436. if( false === $repeater ) { 
  1437. $this->_fields[] = $new_field; 
  1438. else { 
  1439. return $new_field; 
  1440.  
  1441. /** 
  1442. * Add posts Field to meta box 
  1443. * @author Ohad Raz 
  1444. * @since 1.0 
  1445. * @access public 
  1446. * @param $id string field id, i.e. the meta key 
  1447. * @param $options mixed|array options of taxonomy field 
  1448. * 'post_type' => // post type name, 'post' (default) 'page' or any custom post type 
  1449. * 'type' => // how to show posts? 'select' (default) or 'checkbox_list' 
  1450. * 'args' => // arguments to query posts, see http://goo.gl/is0yK default ('posts_per_page' => -1) 
  1451. * @param $args mixed|array 
  1452. * 'name' => // field name/label string optional 
  1453. * 'desc' => // field description, string optional 
  1454. * 'std' => // default value, string optional 
  1455. * 'validate_func' => // validate function, string optional 
  1456. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1457. */ 
  1458. public function addPosts( $id, $options, $args, $repeater = false ) { 
  1459. $post_type = isset( $options['post_type'] ) ? $options['post_type'] : ( isset( $args['post_type'] ) ? $args['post_type'] : 'post' ); 
  1460. $type = isset( $options['type'] ) ? $options['type'] : 'select'; 
  1461. $q = array( 'posts_per_page' => -1, 'post_type' => $post_type ); 
  1462. if( isset( $options['args'] ) ) { 
  1463. $q = array_merge( $q, (array)$options['args'] ); 
  1464. $options = array( 'post_type' => $post_type, 'type' => $type, 'args' => $q ); 
  1465. $new_field = array( 'type' => 'posts', 'id' => $id, 'desc' => '', 'name' => 'Posts Field', 'options' => $options, 'multiple' => false ); 
  1466. $new_field = array_merge( $new_field, $args ); 
  1467. if( false === $repeater ) { 
  1468. $this->_fields[] = $new_field; 
  1469. else { 
  1470. return $new_field; 
  1471.  
  1472. /** 
  1473. * Add repeater Field Block to meta box 
  1474. * @author Ohad Raz 
  1475. * @since 1.0 
  1476. * @access public 
  1477. * @param $id string field id, i.e. the meta key 
  1478. * @param $args mixed|array 
  1479. * 'name' => // field name/label string optional 
  1480. * 'desc' => // field description, string optional 
  1481. * 'std' => // default value, string optional 
  1482. * 'style' => // custom style for field, string optional 
  1483. * 'validate_func' => // validate function, string optional 
  1484. * 'fields' => //fields to repeater 
  1485. */ 
  1486. public function addRepeaterBlock( $id, $args ) { 
  1487. $new_field = array( 
  1488. 'type' => 'repeater',  
  1489. 'id' => $id,  
  1490. 'name' => 'Reapeater Field',  
  1491. 'fields' => array(),  
  1492. 'inline' => false,  
  1493. 'sortable' => false 
  1494. ); 
  1495. $new_field = array_merge( $new_field, $args ); 
  1496. $this->_fields[] = $new_field; 
  1497.  
  1498. /** 
  1499. * Add Checkbox conditional Field to Page 
  1500. * @author Ohad Raz 
  1501. * @since 2.9.9 
  1502. * @access public 
  1503. * @param $id string field id, i.e. the key 
  1504. * @param $args mixed|array 
  1505. * 'name' => // field name/label string optional 
  1506. * 'desc' => // field description, string optional 
  1507. * 'std' => // default value, string optional 
  1508. * 'validate_func' => // validate function, string optional 
  1509. * 'fields' => list of fields to show conditionally. 
  1510. * @param $repeater bool is this a field inside a repeatr? true|false(default) 
  1511. */ 
  1512. public function addCondition( $id, $args, $repeater = false ) { 
  1513. $new_field = array( 
  1514. 'type' => 'cond',  
  1515. 'id' => $id,  
  1516. 'std' => '',  
  1517. 'desc' => '',  
  1518. 'style' => '',  
  1519. 'name' => 'Conditional Field',  
  1520. 'fields' => array() 
  1521. ); 
  1522. $new_field = array_merge( $new_field, $args ); 
  1523. if( false === $repeater ) { 
  1524. $this->_fields[] = $new_field; 
  1525. else { 
  1526. return $new_field; 
  1527.  
  1528. /** 
  1529. * Finish Declaration of Meta Box 
  1530. * @author Ohad Raz 
  1531. * @since 1.0 
  1532. * @access public 
  1533. */ 
  1534. public function Finish() { 
  1535. $this->add_missed_values(); 
  1536.  
  1537. /** 
  1538. * Helper function to check for empty arrays 
  1539. * @author Ohad Raz 
  1540. * @since 1.5 
  1541. * @access public 
  1542. * @param $args mixed|array 
  1543. */ 
  1544. public function is_array_empty( $array ) { 
  1545. if( !is_array( $array ) ) { 
  1546. return true; 
  1547.  
  1548. foreach( $array as $a ) { 
  1549. if( is_array( $a ) ) { 
  1550. foreach( $a as $sub_a ) { 
  1551. if( !empty( $sub_a ) && $sub_a != '' ) { 
  1552. return false; 
  1553. else { 
  1554. if( !empty( $a ) && $a != '' ) { 
  1555. return false; 
  1556.  
  1557. return true; 
  1558.  
  1559. /** 
  1560. * Validate_upload_file_type 
  1561. * Checks if the uploaded file is of the expected format 
  1562. * @author Ohad Raz <admin@bainternet.info> 
  1563. * @since 3.0.7 
  1564. * @access public 
  1565. * @uses get_allowed_mime_types() to check allowed types 
  1566. * @param array $file uploaded file 
  1567. * @return array file with error on mismatch 
  1568. */ 
  1569. function Validate_upload_file_type( $file ) { 
  1570. if( isset( $_POST['uploadeType'] ) && !empty( $_POST['uploadeType'] ) && isset( $_POST['uploadeType'] ) && $_POST['uploadeType'] == 'my_meta_box' ) { 
  1571. $allowed = explode( "|", $_POST['uploadeType'] ); 
  1572. $ext = substr( strrchr( $file['name'], '.' ), 1 ); 
  1573.  
  1574. if( !in_array( $ext, (array)$allowed ) ) { 
  1575. $file['error'] = __( 'Sorry, you cannot upload this file type for this field.', 'responsive-II' ); 
  1576.  
  1577. return $file; 
  1578.  
  1579. foreach( get_allowed_mime_types() as $key => $value ) { 
  1580. if( strpos( $key, $ext ) || $key == $ext ) { 
  1581. return $file; 
  1582. $file['error'] = __( 'Sorry, you cannot upload this file type for this field.', 'responsive-II' ); 
  1583.  
  1584. return $file; 
  1585.  
  1586. /** 
  1587. * function to sanitize field id 
  1588. * @author Ohad Raz <admin@bainternet.info> 
  1589. * @since 3.0.7 
  1590. * @access public 
  1591. * @param string $str string to sanitize 
  1592. * @return string sanitized string 
  1593. */ 
  1594. public function idfy( $str ) { 
  1595. return str_replace( " ", "_", $str ); 
  1596.  
  1597.  
  1598. /** 
  1599. * stripNumeric Strip number form string 
  1600. * @author Ohad Raz <admin@bainternet.info> 
  1601. * @since 3.0.7 
  1602. * @access public 
  1603. * @param string $str 
  1604. * @return string number less string 
  1605. */ 
  1606. public function stripNumeric( $str ) { 
  1607. return trim( str_replace( range( 0, 9 ), '', $str ) ); 
  1608.  
  1609. /** 
  1610. * load_textdomain 
  1611. * @author Ohad Raz 
  1612. * @since 2.9.4 
  1613. * @return void 
  1614. */ 
  1615. public function load_textdomain() { 
  1616. //In themes/plugins/mu-plugins directory 
  1617. load_textdomain( 'responsive-II', dirname( __FILE__ ) . '/lang/' . get_locale() . 'mo' ); 
  1618. } // End Class