/**
* wp_ajax_wpv_shortcode_gui_dialog_create
*
* Render dialog for shortcodes attributes
*
* @since 1.9.0
*/
function wp_ajax_wpv_shortcode_gui_dialog_create() {
    if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'wpv_editor_callback' ) ) {
        die();
    }
    if (
		! isset( $_GET['shortcode'] ) 
		|| empty( $_GET['shortcode'] ) 
	) {
        die();
    }
    /**
	* White list of shortcodes.
	*
	* Filter allow to add shortcode definition to white list of allowed 
	* shortcodes.
	*
	* @since 1.9.0
	*
	* @param array $views_shortcodes {
	*     Complex array with shortcode definition.
	*
	*     @type string $name Name of shortcode.
	*     @type string $label Label displayed as a title of modal popup.
	*     @type array $attributes {
	*          Allowed attributes of shortode.
	*
	*          @type string $label Label of field.
	*          @type string $type Type of field
	*          @type array $options {
	*              Optional param with radio or select options. Array keys is 
	*              a field name.
	*
	*              @@type string Label of value.
	*
	*          }
	*     }
	* }
	*/
	
	$shortcode = $_GET['shortcode'];
	$views_shortcodes_gui_data = apply_filters( 'wpv_filter_wpv_shortcodes_gui_data', array() );
	
    if ( ! isset( $views_shortcodes_gui_data[$shortcode] ) ) {
        die();
    }
    
	$shortcode_data = $views_shortcodes_gui_data[$shortcode];
	if ( 
		isset( $shortcode_data['callback'] )
		&& is_callable( $shortcode_data['callback'] )
	) {
		$options = call_user_func( $shortcode_data['callback'] );
	} else {
		die();
	}

    /**
	* Post selection tab
	*/
    if ( 
		isset( $options['post-selection'] ) 
		&& $options['post-selection'] 
	) {
        if ( ! isset($options['attributes'] ) ) {
            $options['attributes'] = array();
        }
        $options['attributes']['post-selection'] = array(
            'label' => __('Post selection', 'wpv-views'),
            'header' => __('Display data for:', 'wpv-views'),
            'fields' => array(
                'id' => array(
                    'type' => 'post'
                ),
            ),
        );
    }
	
	/**
	* User selection tab
	*/
    if ( 
		isset( $options['user-selection'] ) 
		&& $options['user-selection'] 
	) {
        if ( ! isset($options['attributes'] ) ) {
            $options['attributes'] = array();
        }
        $options['attributes']['user-selection'] = array(
            'label'		=> __( 'User selection', 'wpv-views' ),
            'header'	=> __( 'Display data for:', 'wpv-views' ),
            'fields'	=> array(
                'id'	=> array(
                    'type'	=> 'user'
                ),
            ),
        );
    }

    /**
	* If post_id was passed, get the current post type object
	*/
    $post_id = 0;
    if ( isset( $_GET['post_id'] ) ) {
        $post_id = intval( $_GET['post_id'] );
    }
    $post_type = array();
    if ( $post_id ) {
        $post_type = get_post_type_object( get_post_type( $post_id ) );
    }

    printf(
        '<div class="wpv-dialog js-insert-%s-dialog">',
        esc_attr( $shortcode )
    );
    echo '<input type="hidden" value="' . esc_attr( $shortcode ) . '" class="wpv-shortcode-gui-shortcode-name js-wpv-shortcode-gui-shortcode-name" />';
    echo '<div id="js-wpv-shortcode-gui-dialog-tabs" class="wpv-shortcode-gui-tabs js-wpv-shortcode-gui-tabs">';
    $tabs = '';
    $content = '';
    foreach( $options['attributes'] as $group_id => $group_data ) {
        $tabs .= sprintf(
            '<li><a href="#%s-%s">%s</a></li>',
            esc_attr( $shortcode ),
            esc_attr( $group_id ),
            esc_html( $group_data['label'] )
        );
        $content .= sprintf(
			'<div id="%s-%s">', 
			esc_attr( $shortcode ), 
			esc_attr( $group_id )
		);
        if ( isset( $group_data['header'] ) ) {
            $content .= sprintf(
				'<h2>%s</h2>', 
				esc_html( $group_data['header'] ) 
			);
        }
        /**
         * add fields
         */
        foreach ( $group_data['fields'] as $key => $data ) {
            if ( ! isset( $data['type'] ) ) {
                continue;
            }
            $id = sprintf(
				'%s-%s', 
				$shortcode, 
				$key
			);
            $content .= sprintf(
                '<div class="wpv-shortcode-gui-attribute-wrapper js-wpv-shortcode-gui-attribute-wrapper js-wpv-shortcode-gui-attribute-wrapper-for-%s" data-type="%s" data-attribute="%s" data-default="%s">',
                esc_attr( $key ),
				esc_attr( $data['type'] ),
                esc_attr( $key ),
                isset( $data['default'] ) ? esc_attr( $data['default'] ) : ''
            );
			$attr_value = isset( $data['default'] ) ? $data['default'] : '';
			$attr_value = isset( $data['default_force'] ) ? $data['default_force'] : $attr_value;
			
            $classes = array('js-shortcode-gui-field');
			$required = '';
			if ( 
				isset( $data['required'] ) 
				&& $data['required'] 
			) {
				$classes[] = 'js-wpv-shortcode-gui-required';
				$required = ' <span>- ' . esc_html( __( 'required', 'wpv-views' ) ) . '</span>';
			}
			if ( isset( $data['label'] ) ) {
                $content .= sprintf(
					'<h3>%s%s</h3>', 
					esc_html( $data['label'] ),
					$required
				);
            }
            /**
             * require
             */
            if ( isset($data['required']) && $data['required']) {
                $classes[] = 'js-required';
            }
            /**
             * Filter of options
             *
             * This filter allow to manipulate of radio/select field options.
             * Filter is 'wpv_filter_wpv_shortcodes_gui_api_{shortode}_options'
             *
             * @param array $options for description see param $options in 
             * wpv_filter_wpv_shortcodes_gui_api filter.
             *
             * @param string $type field type
             *
             */
            if ( isset( $data['options'] ) ) {
                $data['options'] = apply_filters( 'wpv_filter_wpv_shortcodes_gui_api_' . $id . '_options', $data['options'], $data['type'] );
            }

            $content .= wpv_shortcode_gui_dialog_render_attribute( $id, $data, $classes, $post_type );

			$desc_and_doc = array();
			if ( isset( $data['description'] ) ) {
				$desc_and_doc[] = esc_html( $data['description'] );
			}
			if ( isset( $data['documentation'] ) ) {
				$desc_and_doc[] = sprintf(
					__( 'Specific documentation: %s', 'wpv-views' ),
					$data['documentation']
				);
			}
			if ( ! empty( $desc_and_doc ) ) {
				$content .= '<p class="description">' . implode( '<br />', $desc_and_doc ) . '</p>';
			}
			$content .= '</div>';
		}
		if ( isset( $group_data['content'] ) ) {
			if ( isset( $group_data['content']['hidden'] ) ) {
				$content .= '<span class="wpv-shortcode-gui-content-wrapper js-wpv-shortcode-gui-content-wrapper" style="display:none">';
				$content .= sprintf(
					'<input id="shortcode-gui-content-%s" type="text" class="large-text js-wpv-shortcode-gui-content" />',
					esc_attr( $shortcode )
				);
				$content .= '</span>';
			} else {
				$content .= '<div class="wpv-shortcode-gui-content-wrapper js-wpv-shortcode-gui-content-wrapper">';
				$content .= sprintf(
					'<h3>%s</h3>', 
					esc_html( $group_data['content']['label'] )
				);
				$content .= sprintf(
					'<input id="shortcode-gui-content-%s" type="text" class="large-text js-wpv-shortcode-gui-content" />',
					esc_attr( $shortcode )
				);
				$desc_and_doc = array();
				if ( isset( $group_data['content']['description'] ) ) {
					$desc_and_doc[] = $group_data['content']['description'];
				}
				if ( isset( $group_data['content']['documentation'] ) ) {
					$desc_and_doc[] = sprintf(
						__( 'Specific documentation: %s', 'wpv-views' ),
						$group_data['content']['documentation']
					);
				}
				if ( ! empty( $desc_and_doc ) ) {
					$content .= '<p class="description">' . implode( '<br />', $desc_and_doc ) . '</p>';
				}
				$content .= '</div>';
			}
		}
        $content .= '</div>';
    }
    printf(
		'<ul>%s</ul>', 
		$tabs
	);
    echo $content;
	echo '</div>';
	echo '<div class="wpv-filter-toolset-messages js-wpv-filter-toolset-messages"></div>';
	echo '</div>';
    die();
}
/**
* wp_ajax_wpv_shortcode_gui_dialog_conditional_create
*
* Render dialog_conditional for shortcodes attributes
*
* @since 1.9.0
*/
function wp_ajax_wpv_shortcode_gui_dialog_conditional_create() {
    if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'wpv_editor_callback' ) ) {
        _e('Nounce verify fail, please reload page and try again.', 'wpv-views');
        die();
    }
    /**
     * If post_id was passed, get the current post type object
     */
    $post_id = 0;
    if ( isset( $_GET['post_id'] ) ) {
        $post_id = intval( $_GET['post_id'] );
    }
    $post_type = array();
    if ( $post_id ) {
        $post_type = get_post_type_object( get_post_type( $post_id ) );
    }
    $shortcode = 'wpv-conditional';
    $options = array(
        'post-selection' => true,
        'attributes' => array(
            'expressions' => array(
                'label' => __('Conditions', 'wpv-views'),
                'header' => __('Conditional output', 'wpv-views'),
                'fields' => array(
                    'if' => array(
						'label' => __('Conditions to evaluate', 'wpv-views'),
                        'type' => 'callback',
                        'callback' => 'wpv_shortcode_wpv_conditional_callback',
                    ),
                    'custom-expressions' => array(
						'label' => __('Manual conditions', 'wpv-views'),
                        'type' => 'textarea',
						'description' => __( 'You can manually insert the conditions.', 'wpv-views' ),
                        'default' => '',
                    ),
					'evaluate' => array(
                        'label' => __('Conditions evaluation', 'wpv-views'),
                        'type' => 'radio',
                        'options' => array(
                            'true' => __('The evaluation result should be TRUE', 'wpv-views'),
                            'false' => __('The evaluation result should be FALSE', 'wpv-views'),
                        ),
						'description' => __( 'Whether the condition should be compared to TRUE or to FALSE.', 'wpv-views' ),
                        'default' => 'true',
                    ),
                    'debug' => array(
                        'label' => __('Show debug', 'wpv-views'),
                        'type' => 'radio',
                        'options' => array(
                            'true' => __('Show debug information to administrators', 'wpv-views'),
                            'false' => __('Don\'t show any debug information', 'wpv-views'),
                        ),
						'description' => __( 'Show additional information to administrators about the evaluation process.', 'wpv-views' ),
                        'default' => 'false',
                    ),
                ),
            ),
        ),
    );

    printf(
        '<div class="wpv-dialog js-insert-%s-dialog">',
        esc_attr( $shortcode )
    );
    echo '<input type="hidden" value="' . esc_attr( $shortcode ) . '" class="wpv-views-conditional-shortcode-gui-dialog-name js-wpv-views-conditional-shortcode-gui-dialog-name" />';
    $content = '';
    foreach( $options['attributes'] as $group_id => $group_data ) {
        $content .= sprintf(
            '<div id="%s-%s" style="position:relative">',
            esc_attr( $shortcode ),
            esc_attr( $group_id )
        );
        if ( isset( $group_data['header'] ) ) {
            $content .= sprintf(
                '<h2>%s</h2>',
                esc_html( $group_data['header'] )
            );
        }
		$content .= sprintf(
			'<button class="button js-wpv-shortcode-expression-switcher" style="position:absolute;top:-5px;right:0;">%s</button>',
			esc_html( __('Edit conditions manually', 'wpv-views') )
		);
        /**
         * add fields
         */
        foreach ( $group_data['fields'] as $key => $data ) {
            if ( ! isset( $data['type'] ) ) {
                continue;
            }
            $id = sprintf(
                '%s-%s',
                $shortcode,
                $key
            );
            $content .= sprintf(
                '<div class="wpv-shortcode-gui-attribute-wrapper js-wpv-shortcode-gui-attribute-wrapper js-wpv-shortcode-gui-attribute-wrapper-for-%s" data-type="%s" data-attribute="%s" data-default="%s" %s>',
                esc_attr( $key ),
                esc_attr( $data['type'] ),
                esc_attr( $key ),
                isset( $data['default'] ) ? esc_attr( $data['default'] ) : '',
                'custom-expressions' == $key? 'style="display:none"':''
            );
            $attr_value = isset( $data['default'] ) ? $data['default'] : '';
            $attr_value = isset( $data['default_force'] ) ? $data['default_force'] : $attr_value;

            $classes = array('js-shortcode-gui-field');
            $required = '';
            if (
                isset( $data['required'] )
                && $data['required']
            ) {
                $classes[] = 'js-wpv-shortcode-gui-required';
                $required = ' <span>- ' . esc_html( __( 'required', 'wpv-views' ) ) . '</span>';
            }
            if ( isset( $data['label'] ) ) {
                $content .= sprintf(
                    '<h3>%s%s</h3>',
                    esc_html( $data['label'] ),
                    $required
                );
            }
            /**
             * require
             */
            if ( isset($data['required']) && $data['required']) {
                $classes[] = 'js-required';
            }
            /**
             * Filter of options
             *
             * This filter allow to manipulate of radio/select field options.
             * Filter is 'wpv_filter_wpv_shortcodes_gui_api_{shortode}_options'
             *
             * @param array $options for description see param $options in
             * wpv_filter_wpv_shortcodes_gui_api filter.
             *
             * @param string $type field type
             *
             */
            if ( isset( $data['options'] ) ) {
                $data['options'] = apply_filters( 'wpv_filter_wpv_shortcodes_gui_api_' . $id . '_options', $data['options'], $data['type'] );
            }

            $content .= wpv_shortcode_gui_dialog_render_attribute( $id, $data, $classes, $post_type );

            $desc_and_doc = array();
            if ( isset( $data['description'] ) ) {
                $desc_and_doc[] = esc_html( $data['description'] );
            }
            if ( isset( $data['documentation'] ) ) {
                $desc_and_doc[] = sprintf(
                    __( 'Specific documentation: %s', 'wpv-views' ),
                    $data['documentation']
                );
            }
            if ( ! empty( $desc_and_doc ) ) {
                $content .= '<p class="description">' . implode( '<br />', $desc_and_doc ) . '</p>';
            }
            $content .= '</div>';
        }
        if ( isset( $group_data['content'] ) ) {
            if ( isset( $group_data['content']['hidden'] ) ) {
                $content .= '<span class="wpv-shortcode-gui-content-wrapper js-wpv-shortcode-gui-content-wrapper" style="display:none">';
                $content .= sprintf(
                    '<input id="shortcode-gui-content-%s" type="text" class="large-text js-wpv-shortcode-gui-content" />',
                    esc_attr( $shortcode )
                );
                $content .= '</span>';
            } else {
                $content .= '<div class="wpv-shortcode-gui-content-wrapper js-wpv-shortcode-gui-content-wrapper">';
                $content .= sprintf(
                    '<h3>%s</h3>',
                    esc_html( $group_data['content']['label'] )
                );
                $content .= sprintf(
                    '<input id="shortcode-gui-content-%s" type="text" class="large-text js-wpv-shortcode-gui-content" />',
                    esc_attr( $shortcode )
                );
                $desc_and_doc = array();
                if ( isset( $group_data['content']['description'] ) ) {
                    $desc_and_doc[] = $group_data['content']['description'];
                }
                if ( isset( $group_data['content']['documentation'] ) ) {
                    $desc_and_doc[] = sprintf(
                        __( 'Specific documentation: %s', 'wpv-views' ),
                        $group_data['content']['documentation']
                    );
                }
                if ( ! empty( $desc_and_doc ) ) {
                    $content .= '<p class="description">' . implode( '<br />', $desc_and_doc ) . '</p>';
                }
                $content .= '</div>';
            }
        }
        $content .= '</div>';
    }
    $content .= '</div>';
    echo $content;
    die;
}