File "form-widget.php"

Full path: /home/kosmetik/public_html/wp-content/plugins/advanced-custom-fields/includes/forms/form-widget.php
File size: 4.57 B
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php

if (!class_exists('acf_form_widget')) {
    class acf_form_widget
    {
        function __construct()
        {
            $this->preview_values = array();
            $this->preview_reference = array();
            $this->preview_errors = array();
            add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
            add_action('in_widget_form', array($this, 'edit_widget'), 10, 3);
            add_action('acf/validate_save_post', array($this, 'acf_validate_save_post'), 5);
            add_filter('widget_update_callback', array($this, 'save_widget'), 10, 4);
        }
        function admin_enqueue_scripts()
        {
            if (acf_is_screen('widgets') || acf_is_screen('customize')) {
            } else {
                return;
            }
            acf_enqueue_scripts();
            add_action('acf/input/admin_footer', array($this, 'admin_footer'), 1);
        }
        function acf_validate_save_post()
        {
            if (!isset($_POST['_acf_widget_id'])) {
                return;
            }
            $id = $_POST['_acf_widget_id'];
            $number = $_POST['_acf_widget_number'];
            $prefix = $_POST['_acf_widget_prefix'];
            acf_validate_values($_POST[$id][$number]['acf'], $prefix);
        }
        function edit_widget($widget, $return, $instance)
        {
            $post_id = 0;
            $prefix = 'widget-' . $widget->id_base . '[' . $widget->number . '][acf]';
            if ($widget->number !== '__i__') {
                $post_id = "widget_{$widget->id}";
            }
            $field_groups = acf_get_field_groups(array('widget' => $widget->id_base));
            if (!empty($field_groups)) {
                acf_form_data(array('screen' => 'widget', 'post_id' => $post_id, 'widget_id' => 'widget-' . $widget->id_base, 'widget_number' => $widget->number, 'widget_prefix' => $prefix));
                echo '<div class="acf-widget-fields acf-fields -clear">';
                foreach ($field_groups as $field_group) {
                    $fields = acf_get_fields($field_group);
                    if (empty($fields)) {
                        continue;
                    }
                    acf_prefix_fields($fields, $prefix);
                    acf_render_fields($fields, $post_id, 'div', $field_group['instruction_placement']);
                }
                echo '</div>';
                if ($widget->updated) {
                    ?>
			<script type="text/javascript">
			(function($) {
				
				acf.doAction('append', $('[id^="widget"][id$="<?php 
                    echo $widget->id;
                    ?>"]') );
				
			})(jQuery);	
			</script>
					<?php 
                }
            }
        }
        function save_widget($instance, $new_instance, $old_instance, $widget)
        {
            if (!(function_exists('wp_is_json_request') && wp_is_json_request())) {
                if (!acf_verify_nonce('widget')) {
                    return $instance;
                }
            }
            if (isset($_POST['wp_customize']) || !isset($new_instance['acf'])) {
                return $instance;
            }
            acf_save_post("widget_{$widget->id}", $new_instance['acf']);
            return $instance;
        }
        function admin_footer()
        {
            ?>
<script type="text/javascript">
(function($) {
	
	// vars
	acf.set('post_id', 'widgets');
	
	// Only initialize visible fields.
	acf.addFilter('find_fields', function( $fields ){
		
		// not templates
		$fields = $fields.not('#available-widgets .acf-field');
		
		// not widget dragging in
		$fields = $fields.not('.widget.ui-draggable-dragging .acf-field');
		
		// return
		return $fields;
	});
	
	// on publish
	$('#widgets-right').on('click', '.widget-control-save', function( e ){
		
		// vars
		var $button = $(this);
		var $form = $button.closest('form');
		
		// validate
		var valid = acf.validateForm({
			form: $form,
			event: e,
			reset: true
		});
		
		// if not valid, stop event and allow validation to continue
		if( !valid ) {
			e.preventDefault();
			e.stopImmediatePropagation();
		}
	});
	
	// show
	$('#widgets-right').on('click', '.widget-top', function(){
		var $widget = $(this).parent();
		if( $widget.hasClass('open') ) {
			acf.doAction('hide', $widget);
		} else {
			acf.doAction('show', $widget);
		}
	});
	
	$(document).on('widget-added', function( e, $widget ){
		
		// - use delay to avoid rendering issues with customizer (ensures div is visible)
		setTimeout(function(){
			acf.doAction('append', $widget );
		}, 100);
	});
	
})(jQuery);	
</script>
			<?php 
        }
    }
    new acf_form_widget();
}