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