PageRenderTime 62ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/liguelista/library/admin/javo-custom-fileds.php

https://gitlab.com/oxidigitaluser/liguelista
PHP | 283 lines | 254 code | 28 blank | 1 comment | 15 complexity | 8724ce6ac3118069f21616258b28f18d MD5 | raw file
  1. <?php
  2. class javo_custom_field{
  3. public function __construct(){
  4. add_filter('javo_custom_field', Array('javo_custom_field', 'insert_field'), 10, 5);
  5. add_action('save_post', Array(__class__, 'javo_custom_in_post_save_callback'));
  6. add_action('wp_ajax_get_custom_field_form', Array($this, 'wp_ajax_get_custom_field_form_callback'));
  7. add_action('wp_ajax_nopriv_get_custom_field_form', Array($this, 'wp_ajax_get_custom_field_form_callback'));
  8. }
  9. public function wp_ajax_get_custom_field_form_callback(){
  10. $javo_get_custom_filed_id = 'id'.md5( strtotime( date( 'YmdHis' ) ).rand(10,1000000) );
  11. ob_start();?>
  12. <div class="postbox-container" style='width:100%;'>
  13. <div class="meta-box-sortables">
  14. <div class="postbox">
  15. <h3>
  16. <label style="font:0.7em/0.7em 'Trebuchet MS', Helvetica, sans-serif; color:#868686;"><?php _e('Field Attributes', 'javo_fr');?></label>
  17. </h3>
  18. <div class="inside">
  19. <dl>
  20. <dt><?php _e('Input Label', 'javo_fr');?></dt>
  21. <dd>
  22. <input type="hidden" name="javo_ts[custom_field][<?php echo $javo_get_custom_filed_id;?>][name]" value="<?php echo $javo_get_custom_filed_id;?>">
  23. <input type="text" name="javo_ts[custom_field][<?php echo $javo_get_custom_filed_id;?>][label]" value="">
  24. </dd>
  25. </dl>
  26. <dl>
  27. <dt><?php _e('Element Type', 'javo_fr');?></dt>
  28. <dd>
  29. <select name="javo_ts[custom_field][<?php echo $javo_get_custom_filed_id;?>][type]">
  30. <?php
  31. echo $this->insert_option(Array(
  32. __('Text Field', 'javo_fr') => 'text'
  33. , __('Textarea', 'javo_fr') => 'textarea'
  34. , __('Select Box', 'javo_fr') => 'select'
  35. ));?>
  36. </select>
  37. </dd>
  38. </dl>
  39. <dl>
  40. <dt><?php _e('Values', 'javo_fr');?></dt>
  41. <dd>
  42. <input name="javo_ts[custom_field][<?php echo $javo_get_custom_filed_id;?>][value]" value="">
  43. </dd>
  44. </dl>
  45. <dl>
  46. <dt><?php _e('CSS Class Name', 'javo_fr');?></dt>
  47. <dd><input name="javo_ts[custom_field][<?php echo $javo_get_custom_filed_id;?>][css]" value=""></dd>
  48. </dl>
  49. <dl>
  50. <dt></dt>
  51. <dd>
  52. <a class="button button-warning javo-remove-custom-field"><?php _e('Remove', 'javo_fr');?></a>
  53. </dd>
  54. </dl>
  55. </div>
  56. </div><!-- PostBox End -->
  57. </div><!-- PostBox Sortable End -->
  58. </div><!-- PostBox Container End -->
  59. <?php
  60. $javo_get_this_content = ob_get_clean();
  61. echo json_encode(Array(
  62. 'output'=> $javo_get_this_content
  63. ));
  64. exit;
  65. }
  66. static function javo_custom_in_post_save_callback($post_id){
  67. $javo_query = new javo_ARRAY( $_POST );
  68. if( $javo_query->get('javo_custom_field', null ) != null ){
  69. update_post_meta($post_id, 'custom_variables', $javo_query->get('javo_custom_field'));
  70. };
  71. }
  72. public function insert_option( $options, $default=NULL){
  73. $javo_this_output ="";
  74. foreach( (Array) $options as $key=> $value){
  75. $javo_this_output .= sprintf('<option value="%s"%s>%s</option>', $value, ($value == $default ? ' selected': ''), $key);
  76. }
  77. return $javo_this_output;
  78. }
  79. static function gets(){
  80. global $post;
  81. // Output : Label, Value
  82. $javo_get_custom_variables = get_post_meta($post->ID, 'custom_variables', true);
  83. if( empty( $javo_get_custom_variables ) ){ return; };
  84. return $javo_get_custom_variables;
  85. }
  86. static function insert_field($label, $type, $attributes = Array(), $values = NULL, $default_value=NULL){
  87. $javo_this_output = Array('attribute' => '', 'values' => '');
  88. $attributes['class'] .= ' form-control';
  89. foreach( (Array)$attributes as $key => $value){
  90. if($key == 'name'){
  91. $javo_this_output['attribute'] .= ' '.$key.'="javo_custom_field['.$value.'][value]"';
  92. }else{
  93. $javo_this_output['attribute'] .= ' '.$key.'="'.$value.'"';
  94. };
  95. }
  96. $javo_this_output['attribute'] .= ">";
  97. switch( $type ){
  98. case 'textarea':
  99. $javo_this_output['before'] = '<textarea';
  100. $javo_this_output['after'] = '</textarea>';
  101. $javo_this_output['values'] = $default_value != NULL ? $default_value : $values;
  102. break;
  103. case 'select':
  104. $javo_this_output['before'] = '<select';
  105. if( !empty( $values ) ){
  106. $javo_this_values = explode(',', $values);
  107. foreach($javo_this_values as $value){
  108. $javo_this_output['values'] .= sprintf('<option value="%s"%s>%s</option>'
  109. , trim($value)
  110. , (trim($value) == trim($default_value)? ' selected="selected"':'' )
  111. , trim($value)
  112. );
  113. };
  114. };
  115. $javo_this_output['after'] = '</select>';
  116. break;
  117. case 'text':
  118. default:
  119. $javo_this_output['before'] = '<input type="text" value="'. ( $default_value != NULL ? $default_value : $values ).'"';
  120. $javo_this_output['after'] = '';
  121. break;
  122. };
  123. ob_start();?>
  124. <div class="form-group">
  125. <div class="input-group">
  126. <span class="input-group-addon"><?php echo $label;?></span>
  127. <?php echo $javo_this_output['before'].$javo_this_output['attribute'].$javo_this_output['values'].$javo_this_output['after'];?>
  128. </div>
  129. <input type="hidden" name="javo_custom_field[<?php echo $attributes['name'];?>][label]" value="<?php echo $label;?>">
  130. </div>
  131. <?php
  132. return ob_get_clean();
  133. }
  134. public function form(){
  135. global $javo_tso, $edit;
  136. $javo_get_custom_field = $javo_tso->get('custom_field', null);
  137. $javo_get_custom_variables = Array();
  138. if(!empty($edit)){
  139. $javo_get_custom_variables = get_post_meta($edit->ID, 'custom_variables', true);
  140. }
  141. ob_start();?>
  142. <?php
  143. if( !empty( $javo_get_custom_field ) ){
  144. foreach( $javo_get_custom_field as $key => $field){
  145. if( !empty( $javo_get_custom_variables[$field['name']] )){
  146. $javo_this_form_data = new javo_ARRAY( $javo_get_custom_variables[$field['name']] );
  147. };
  148. echo apply_filters(
  149. 'javo_custom_field'
  150. , $field['label']
  151. , $field['type']
  152. , Array(
  153. 'name'=> $field['name']
  154. , 'class'=> $field['css']
  155. ), $field['value']
  156. , (!empty($javo_this_form_data)? $javo_this_form_data->get('value', NULL) : NULL)
  157. );
  158. };
  159. };
  160. return ob_get_clean();
  161. }
  162. public function admin(){
  163. global $javo_tso;
  164. $javo_get_custom_field = $javo_tso->get('custom_field', null);
  165. ob_start(); ?>
  166. <div class="javo_custom_field_forms">
  167. <?php
  168. if( !empty($javo_get_custom_field) ){
  169. foreach($javo_get_custom_field as $key => $field){
  170. $javo_field_string = new javo_Array($field);
  171. ?>
  172. <div class="postbox-container" style='width:100%;'>
  173. <div class="meta-box-sortables">
  174. <div class="postbox">
  175. <h3>
  176. <label style="font:0.7em/0.7em 'Trebuchet MS', Helvetica, sans-serif; color:#868686;"><?php _e('Field Attributes', 'javo_fr');?></label>
  177. </h3>
  178. <div class="inside">
  179. <dl>
  180. <dt><?php _e('Input Label', 'javo_fr');?></dt>
  181. <dd>
  182. <input type="hidden" name="javo_ts[custom_field][<?php echo $key;?>][name]" value="<?php echo $field['name'];?>">
  183. <input type="text" name="javo_ts[custom_field][<?php echo $key;?>][label]" value="<?php echo $javo_field_string->get('label');?>">
  184. </dd>
  185. </dl>
  186. <dl>
  187. <dt><?php _e('Element Type', 'javo_fr');?></dt>
  188. <dd>
  189. <select name="javo_ts[custom_field][<?php echo $key;?>][type]">
  190. <?php
  191. echo $this->insert_option(Array(
  192. __('Text Field', 'javo_fr') => 'text'
  193. , __('Textarea', 'javo_fr') => 'textarea'
  194. , __('Select Box', 'javo_fr') => 'select'
  195. ), $field['type']);?>
  196. </select>
  197. </dd>
  198. </dl>
  199. <dl>
  200. <dt><?php _e('Values', 'javo_fr');?></dt>
  201. <dd>
  202. <input name="javo_ts[custom_field][<?php echo $key;?>][value]" value="<?php echo $javo_field_string->get('value');?>">
  203. </dd>
  204. </dl>
  205. <dl>
  206. <dt><?php _e('CSS Class Name', 'javo_fr');?></dt>
  207. <dd>
  208. <input name="javo_ts[custom_field][<?php echo $key;?>][css]" value="<?php echo $javo_field_string->get('css');?>">
  209. </dd>
  210. </dl>
  211. <dl>
  212. <dt></dt>
  213. <dd>
  214. <a class="button button-warning javo-remove-custom-field"><?php _e('Remove', 'javo_fr');?></a>
  215. </dd>
  216. </dl>
  217. </div>
  218. </div><!-- PostBox End -->
  219. </div><!-- PostBox Sortable End -->
  220. </div><!-- PostBox Container End -->
  221. <?php
  222. };
  223. };?>
  224. </div>
  225. <a class="button button-primary javo-add-custom-field"><?php _e('Add New Field', 'javo_fr');?></a>
  226. <script type="text/javascript">
  227. (function($){
  228. "use strict";
  229. $(document).on('click', '.javo-add-custom-field', function(){
  230. var $this = $(this);
  231. var javo_get_custom_field_form_ajax = {
  232. url:'<?php echo admin_url("admin-ajax.php");?>'
  233. , type: 'post'
  234. , dataType: 'json'
  235. , data:{ action:'get_custom_field_form' }
  236. , success:function(d){
  237. $('body').find('.javo_custom_field_forms').append( d.output );
  238. $this.removeClass('disabled');
  239. }
  240. };
  241. if( !$this.hasClass('disabled') ){
  242. $this.addClass('disabled');
  243. $.ajax( javo_get_custom_field_form_ajax );
  244. };
  245. }).on('click', '.javo-remove-custom-field', function(){
  246. $(this).closest('.postbox-container').remove();
  247. });
  248. })(jQuery);
  249. </script>
  250. <?php
  251. return ob_get_clean();
  252. }
  253. };
  254. global $javo_custom_field;
  255. $javo_custom_field = new javo_custom_field();