| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wdk\Extensions; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) exit; // Exit if accessed directly |
| 6 |
|
| 7 |
|
| 8 |
class WdkDependfields |
| 9 |
{ |
| 10 |
/** |
| 11 |
* data array |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
public $data = array(); |
| 16 |
|
| 17 |
public function __construct($data = array(), $args = null) |
| 18 |
{ |
| 19 |
|
| 20 |
/* admin actions */ |
| 21 |
add_action( 'wpdirectorykit/admin/category/edit/after_form', array($this, 'dependfields')); |
| 22 |
|
| 23 |
/* admin actions */ |
| 24 |
add_action( 'wpdirectorykit/admin/listing/edit/after_form', array($this, 'listing_edit')); |
| 25 |
add_action( 'wdk-membership/view/listing_edit/after_form', array($this, 'listing_edit')); |
| 26 |
|
| 27 |
add_action( 'wp_enqueue_scripts', array($this, 'front_search'), 100); |
| 28 |
} |
| 29 |
|
| 30 |
public function listing_edit($data = NULL) |
| 31 |
{ |
| 32 |
|
| 33 |
global $Winter_MVC_WDK; |
| 34 |
$Winter_MVC_WDK->model('dependfields_m'); |
| 35 |
|
| 36 |
$hidden_fields = array(); |
| 37 |
|
| 38 |
$depend_fields = $Winter_MVC_WDK->dependfields_m->get_by(array('main_field' => 'categories')); |
| 39 |
if($depend_fields) { |
| 40 |
foreach ($depend_fields as $depend_field) { |
| 41 |
$hidden_fields[$depend_field->field_id] = ','.$depend_field->hidden_fields_list.','; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
$params = array( |
| 46 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 47 |
'hidden_fields'=> $hidden_fields |
| 48 |
); |
| 49 |
wp_enqueue_script( 'wdk-dependfields-submitform'); |
| 50 |
wp_localize_script( 'wdk-dependfields-submitform', 'script_dependfields_submitform', $params); |
| 51 |
} |
| 52 |
|
| 53 |
public function front_search($data = NULL) |
| 54 |
{ |
| 55 |
|
| 56 |
global $Winter_MVC_WDK; |
| 57 |
$Winter_MVC_WDK->model('dependfields_m'); |
| 58 |
|
| 59 |
$hidden_fields = array(); |
| 60 |
|
| 61 |
$depend_fields = $Winter_MVC_WDK->dependfields_m->get_by(array('main_field' => 'categories')); |
| 62 |
if($depend_fields) { |
| 63 |
foreach ($depend_fields as $depend_field) { |
| 64 |
$hidden_fields[$depend_field->field_id] = ','.$depend_field->hidden_fields_list.','; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
$params = array( |
| 69 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 70 |
'hidden_fields'=> $hidden_fields |
| 71 |
); |
| 72 |
wp_enqueue_script( 'wdk-dependfields-search'); |
| 73 |
wp_localize_script( 'wdk-dependfields-search', 'script_dependfields_search', $params); |
| 74 |
} |
| 75 |
|
| 76 |
public function dependfields($data = NULL) |
| 77 |
{ |
| 78 |
if (empty($data)) |
| 79 |
return false; |
| 80 |
|
| 81 |
global $Winter_MVC_WDK; |
| 82 |
$Winter_MVC_WDK->model('field_m'); |
| 83 |
$Winter_MVC_WDK->model('dependfields_m'); |
| 84 |
$this->data['fields_data'] = $Winter_MVC_WDK->field_m->get(); |
| 85 |
$this->data['hidden_fields'] = array(); |
| 86 |
|
| 87 |
wp_enqueue_script( 'wdk-dependfields-edit'); |
| 88 |
|
| 89 |
$depend_fields = $Winter_MVC_WDK->dependfields_m->get_by(array('field_id' => wmvc_show_data('idcategory', $data),'main_field' => 'categories'), TRUE); |
| 90 |
if($depend_fields && !empty($depend_fields->hidden_fields_list)) { |
| 91 |
$this->data['hidden_fields'] = explode(',', $depend_fields->hidden_fields_list); |
| 92 |
} |
| 93 |
?> |
| 94 |
|
| 95 |
<div class="wdk-body"> |
| 96 |
<div class="postbox" style="display: block;"> |
| 97 |
<div class="postbox-header flex-align-left"> |
| 98 |
<h3 class="wdk-mr-5"> |
| 99 |
<?php echo __('Visible fields','wpdirectorykit'); ?> |
| 100 |
</h3> |
| 101 |
<a href="#" class="wdk-mr-5 button button-secondary wdk_copy_on_subcategories" data-category="<?php echo esc_attr(wmvc_show_data('idcategory', $data));?>" data-wpnonce="<?php echo wp_create_nonce('wdk_depend_copy_on_subcategories');?>"> |
| 102 |
<span class="dashicons dashicons-admin-page" style="margin-top: 4px;"></span> <?php echo __('Copy on subcategories','wpdirectorykit'); ?> |
| 103 |
</a> |
| 104 |
</div> |
| 105 |
<div class="inside"> |
| 106 |
<form method="post" action="#" novalidate="novalidate" class="wdk-depend-fields" enctype="multipart/form-data"> |
| 107 |
<input name="_wpnonce" type="hidden" value="<?php echo esc_attr(wp_create_nonce( 'wdk-update_depend'));?>" class="regular-text"> |
| 108 |
<input name="main_field" type="hidden" value="categories" class="regular-text"> |
| 109 |
<input name="field_id" type="hidden" value="<?php echo esc_attr(wmvc_show_data('idcategory', $data));?>" class="regular-text"> |
| 110 |
|
| 111 |
<div class="wdk-row"> |
| 112 |
<?php echo esc_html(wmvc_show_data('field_label', $field));?>: |
| 113 |
<hr/> |
| 114 |
</div> |
| 115 |
|
| 116 |
<div class="wdk-row"> |
| 117 |
<?php foreach ($this->data['fields_data'] as $field) :;?> |
| 118 |
<?php if($field->field_type == "SECTION"):?> |
| 119 |
<div class="wdk-col-md-12 wdk-col-section"> |
| 120 |
<div class="wdk_field section"> |
| 121 |
<?php echo esc_html(wmvc_show_data('field_label', $field));?>: |
| 122 |
<label for=""> |
| 123 |
<input name="field_all_depend_<?php echo esc_attr(wmvc_show_data('idfield', $field));?>" type="checkbox" id="field_all_depend_<?php echo esc_attr(wmvc_show_data('idfield', $field));?>" value="1" class="regular-text trigger_section"> |
| 124 |
</label> |
| 125 |
</div> |
| 126 |
</div> |
| 127 |
<?php else: ?> |
| 128 |
<div class="wdk-col-md-<?php echo esc_attr($field->columns_number); ?> "> |
| 129 |
<div class="wdk_field"> |
| 130 |
<label for="field_depend_<?php echo esc_attr(wmvc_show_data('idfield', $field));?>"><?php echo esc_html(wmvc_show_data('field_label', $field));?></label> |
| 131 |
<input <?php if(empty($this->data['hidden_fields']) || !in_array(wmvc_show_data('idfield', $field), $this->data['hidden_fields'])):?> checked="checked"<?php endif;?> name="field_hide_<?php echo esc_attr(wmvc_show_data('idfield', $field));?>" type="checkbox" id="field_depend_<?php echo esc_attr(wmvc_show_data('idfield', $field));?>" value="1" class="regular-text"> |
| 132 |
</div> |
| 133 |
</div> |
| 134 |
<?php endif;?> |
| 135 |
<?php endforeach; ?> |
| 136 |
</div> |
| 137 |
</form> |
| 138 |
</div> |
| 139 |
</div> |
| 140 |
</div> |
| 141 |
<?php |
| 142 |
} |
| 143 |
|
| 144 |
} |
| 145 |
|