| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* ACF Widget Form Class |
| 5 |
* |
| 6 |
* All the logic for adding fields to widgets |
| 7 |
* |
| 8 |
* @class acf_form_widget |
| 9 |
* @package ACF |
| 10 |
* @subpackage Forms |
| 11 |
*/ |
| 12 |
|
| 13 |
if( ! class_exists('acf_form_widget') ) : |
| 14 |
|
| 15 |
class acf_form_widget { |
| 16 |
|
| 17 |
|
| 18 |
/* |
| 19 |
* __construct |
| 20 |
* |
| 21 |
* This function will setup the class functionality |
| 22 |
* |
| 23 |
* @type function |
| 24 |
* @date 5/03/2014 |
| 25 |
* @since 5.0.0 |
| 26 |
* |
| 27 |
* @param n/a |
| 28 |
* @return n/a |
| 29 |
*/ |
| 30 |
|
| 31 |
function __construct() { |
| 32 |
|
| 33 |
// vars |
| 34 |
$this->preview_values = array(); |
| 35 |
$this->preview_reference = array(); |
| 36 |
$this->preview_errors = array(); |
| 37 |
|
| 38 |
|
| 39 |
// actions |
| 40 |
add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts')); |
| 41 |
add_action('in_widget_form', array($this, 'edit_widget'), 10, 3); |
| 42 |
add_action('acf/validate_save_post', array($this, 'acf_validate_save_post'), 5); |
| 43 |
|
| 44 |
|
| 45 |
// filters |
| 46 |
add_filter('widget_update_callback', array($this, 'save_widget'), 10, 4); |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
/* |
| 52 |
* admin_enqueue_scripts |
| 53 |
* |
| 54 |
* This action is run after post query but before any admin script / head actions. |
| 55 |
* It is a good place to register all actions. |
| 56 |
* |
| 57 |
* @type action (admin_enqueue_scripts) |
| 58 |
* @date 26/01/13 |
| 59 |
* @since 3.6.0 |
| 60 |
* |
| 61 |
* @param N/A |
| 62 |
* @return N/A |
| 63 |
*/ |
| 64 |
|
| 65 |
function admin_enqueue_scripts() { |
| 66 |
|
| 67 |
// validate screen |
| 68 |
if( acf_is_screen('widgets') || acf_is_screen('customize') ) { |
| 69 |
|
| 70 |
// valid |
| 71 |
|
| 72 |
} else { |
| 73 |
|
| 74 |
return; |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
// load acf scripts |
| 80 |
acf_enqueue_scripts(); |
| 81 |
|
| 82 |
|
| 83 |
// actions |
| 84 |
add_action('acf/input/admin_footer', array($this, 'admin_footer'), 1); |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
/* |
| 90 |
* acf_validate_save_post |
| 91 |
* |
| 92 |
* This function will loop over $_POST data and validate |
| 93 |
* |
| 94 |
* @type action 'acf/validate_save_post' 5 |
| 95 |
* @date 7/09/2016 |
| 96 |
* @since 5.4.0 |
| 97 |
* |
| 98 |
* @param n/a |
| 99 |
* @return n/a |
| 100 |
*/ |
| 101 |
|
| 102 |
function acf_validate_save_post() { |
| 103 |
|
| 104 |
// bail ealry if not widget |
| 105 |
if( !isset($_POST['_acf_widget_id']) ) return; |
| 106 |
|
| 107 |
|
| 108 |
// vars |
| 109 |
$id = $_POST['_acf_widget_id']; |
| 110 |
$number = $_POST['_acf_widget_number']; |
| 111 |
$prefix = $_POST['_acf_widget_prefix']; |
| 112 |
|
| 113 |
|
| 114 |
// validate |
| 115 |
acf_validate_values( $_POST[ $id ][ $number ]['acf'], $prefix ); |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
/* |
| 121 |
* edit_widget |
| 122 |
* |
| 123 |
* This function will render the fields for a widget form |
| 124 |
* |
| 125 |
* @type function |
| 126 |
* @date 11/06/2014 |
| 127 |
* @since 5.0.0 |
| 128 |
* |
| 129 |
* @param $widget (object) |
| 130 |
* @param $return (null) |
| 131 |
* @param $instance (object) |
| 132 |
* @return $post_id (int) |
| 133 |
*/ |
| 134 |
|
| 135 |
function edit_widget( $widget, $return, $instance ) { |
| 136 |
|
| 137 |
// vars |
| 138 |
$post_id = 0; |
| 139 |
$prefix = 'widget-' . $widget->id_base . '[' . $widget->number . '][acf]'; |
| 140 |
|
| 141 |
|
| 142 |
// get id |
| 143 |
if( $widget->number !== '__i__' ) { |
| 144 |
|
| 145 |
$post_id = "widget_{$widget->id}"; |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
// get field groups |
| 151 |
$field_groups = acf_get_field_groups(array( |
| 152 |
'widget' => $widget->id_base |
| 153 |
)); |
| 154 |
|
| 155 |
|
| 156 |
// render |
| 157 |
if( !empty($field_groups) ) { |
| 158 |
|
| 159 |
// render post data |
| 160 |
acf_form_data(array( |
| 161 |
'screen' => 'widget', |
| 162 |
'post_id' => $post_id, |
| 163 |
'widget_id' => 'widget-' . $widget->id_base, |
| 164 |
'widget_number' => $widget->number, |
| 165 |
'widget_prefix' => $prefix |
| 166 |
)); |
| 167 |
|
| 168 |
|
| 169 |
// wrap |
| 170 |
echo '<div class="acf-widget-fields acf-fields -clear">'; |
| 171 |
|
| 172 |
// loop |
| 173 |
foreach( $field_groups as $field_group ) { |
| 174 |
|
| 175 |
// load fields |
| 176 |
$fields = acf_get_fields( $field_group ); |
| 177 |
|
| 178 |
|
| 179 |
// bail if not fields |
| 180 |
if( empty($fields) ) continue; |
| 181 |
|
| 182 |
|
| 183 |
// change prefix |
| 184 |
acf_prefix_fields( $fields, $prefix ); |
| 185 |
|
| 186 |
|
| 187 |
// render |
| 188 |
acf_render_fields( $fields, $post_id, 'div', $field_group['instruction_placement'] ); |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
//wrap |
| 193 |
echo '</div>'; |
| 194 |
|
| 195 |
|
| 196 |
// jQuery selector looks odd, but is necessary due to WP adding an incremental number into the ID |
| 197 |
// - not possible to find number via PHP parameters |
| 198 |
if( $widget->updated ): ?> |
| 199 |
<script type="text/javascript"> |
| 200 |
(function($) { |
| 201 |
|
| 202 |
acf.doAction('append', $('[id^="widget"][id$="<?php echo $widget->id; ?>"]') ); |
| 203 |
|
| 204 |
})(jQuery); |
| 205 |
</script> |
| 206 |
<?php endif; |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
/* |
| 214 |
* save_widget |
| 215 |
* |
| 216 |
* This function will hook into the widget update filter and save ACF data |
| 217 |
* |
| 218 |
* @type function |
| 219 |
* @date 27/05/2015 |
| 220 |
* @since 5.2.3 |
| 221 |
* |
| 222 |
* @param $instance (array) widget settings |
| 223 |
* @param $new_instance (array) widget settings |
| 224 |
* @param $old_instance (array) widget settings |
| 225 |
* @param $widget (object) widget info |
| 226 |
* @return $instance |
| 227 |
*/ |
| 228 |
|
| 229 |
function save_widget( $instance, $new_instance, $old_instance, $widget ) { |
| 230 |
|
| 231 |
// bail ealry if not valid (!customize + acf values + nonce) |
| 232 |
if( isset($_POST['wp_customize']) || !isset($new_instance['acf']) || !acf_verify_nonce('widget') ) return $instance; |
| 233 |
|
| 234 |
|
| 235 |
// save |
| 236 |
acf_save_post( "widget_{$widget->id}", $new_instance['acf'] ); |
| 237 |
|
| 238 |
|
| 239 |
// return |
| 240 |
return $instance; |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
/* |
| 246 |
* admin_footer |
| 247 |
* |
| 248 |
* This function will add some custom HTML to the footer of the edit page |
| 249 |
* |
| 250 |
* @type function |
| 251 |
* @date 11/06/2014 |
| 252 |
* @since 5.0.0 |
| 253 |
* |
| 254 |
* @param n/a |
| 255 |
* @return n/a |
| 256 |
*/ |
| 257 |
|
| 258 |
function admin_footer() { |
| 259 |
?> |
| 260 |
<script type="text/javascript"> |
| 261 |
(function($) { |
| 262 |
|
| 263 |
// vars |
| 264 |
acf.set('post_id', 'widgets'); |
| 265 |
|
| 266 |
// Only initialize visible fields. |
| 267 |
acf.addFilter('find_fields', function( $fields ){ |
| 268 |
|
| 269 |
// not templates |
| 270 |
$fields = $fields.not('#available-widgets .acf-field'); |
| 271 |
|
| 272 |
// not widget dragging in |
| 273 |
$fields = $fields.not('.widget.ui-draggable-dragging .acf-field'); |
| 274 |
|
| 275 |
// return |
| 276 |
return $fields; |
| 277 |
}); |
| 278 |
|
| 279 |
// on publish |
| 280 |
$('#widgets-right').on('click', '.widget-control-save', function( e ){ |
| 281 |
|
| 282 |
// vars |
| 283 |
var $button = $(this); |
| 284 |
var $form = $button.closest('form'); |
| 285 |
|
| 286 |
// validate |
| 287 |
var valid = acf.validateForm({ |
| 288 |
form: $form, |
| 289 |
event: e, |
| 290 |
reset: true |
| 291 |
}); |
| 292 |
|
| 293 |
// if not valid, stop event and allow validation to continue |
| 294 |
if( !valid ) { |
| 295 |
e.preventDefault(); |
| 296 |
e.stopImmediatePropagation(); |
| 297 |
} |
| 298 |
}); |
| 299 |
|
| 300 |
// show |
| 301 |
$('#widgets-right').on('click', '.widget-top', function(){ |
| 302 |
var $widget = $(this).parent(); |
| 303 |
if( $widget.hasClass('open') ) { |
| 304 |
acf.doAction('hide', $widget); |
| 305 |
} else { |
| 306 |
acf.doAction('show', $widget); |
| 307 |
} |
| 308 |
}); |
| 309 |
|
| 310 |
$(document).on('widget-added', function( e, $widget ){ |
| 311 |
|
| 312 |
// - use delay to avoid rendering issues with customizer (ensures div is visible) |
| 313 |
setTimeout(function(){ |
| 314 |
acf.doAction('append', $widget ); |
| 315 |
}, 100); |
| 316 |
}); |
| 317 |
|
| 318 |
})(jQuery); |
| 319 |
</script> |
| 320 |
<?php |
| 321 |
|
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
new acf_form_widget(); |
| 326 |
|
| 327 |
endif; |
| 328 |
|
| 329 |
?> |
| 330 |
|