function wpv_generate_view_loop_output_callback() {
	if ( ! current_user_can( 'manage_options' ) ) {
		$data = array(
			'type' => 'capability',
			'message' => __( 'You do not have permissions for that.', 'wpv-views' )
		);
		wp_send_json_error( $data );
	}
	if ( 
		! isset( $_POST["wpnonce"] )
		|| ! wp_verify_nonce( $_POST["wpnonce"], 'layout_wizard_nonce' ) 
	) {
		$data = array(
			'type' => 'nonce',
			'message' => __( 'Your security credentials have expired. Please reload the page to get new ones.', 'wpv-views' )
		);
		wp_send_json_error( $data );
	}
	if (
		! isset( $_POST["view_id"] )
		|| ! is_numeric( $_POST["view_id"] )
		|| intval( $_POST['view_id'] ) < 1 
	) {
		$data = array(
			'type' => 'id',
			'message' => __( 'Wrong or missing ID.', 'wpv-views' )
		);
		wp_send_json_error( $data );
	}

	$view_id = $_POST['view_id'];
	$style = sanitize_text_field( $_POST['style'] );
	
	// @todo better validation
	$fields = json_decode( stripslashes( $_POST['fields'] ), true );
	$args = json_decode( stripslashes( $_POST['args'] ), true );

    // Translate field data from non-associative arrays into something that WPV_View_Base::generate_loop_output() understands.
    $fields_normalized = array();
    foreach( $fields as $field ) {
	    $fields_normalized[] = array(
				'prefix' => $field[0],
				'shortcode' => $field[1],
				'suffix' => $field[2],
				'field_name' => $field[3],
				'header_name' => $field[4],
				'row_title' => $field[5] );
	}
	
	$loop_output = WPV_View_Base::generate_loop_output( $style, $fields_normalized, $args );

	// Forward the fail when loop output couldn't have been generated. 
	if ( null == $loop_output ) {
		$data = array(
			'type' => 'error',
			'message' => __( 'Could not generate the Loop Output. Please reload and try again.', 'wpv-views' )
		);
		wp_send_json_error( $data );
	}
		
	// Merge new settings to existing ones (overwrite keys from $layout_settings but keep the rest).
	$loop_output_settings = $loop_output['loop_output_settings'];
	$prev_settings = get_post_meta( $view_id, '_wpv_layout_settings', true );
	if( ! is_array( $prev_settings ) ) {
		// Handle missing _wpv_layout_settings for given View.
		$prev_settings = array();
	}
	$loop_output_settings = array_merge( $prev_settings, $loop_output_settings );
	
	if ( 
		isset( $loop_output_settings['fields'] )
		&& is_array( $loop_output_settings['fields'] )
	) {
		$loop_output_settings['fields'] = array_values( $loop_output_settings['fields'] );
	}

	// Return the results.
	$data = array(
		'loop_output_settings' => $loop_output_settings,
		'ct_content' => $loop_output['ct_content'] 
	);
	wp_send_json_success( $data );
}
Example #2
0
/**
 * Set default WordPress Archives settings and layout settings
 *
 * @param string $settings field: view_settings or view_layout_settings
 * @return array with desired values
 * @since unknown
 */
function wpv_wordpress_archives_defaults( $settings = 'view_settings' ) {

    $empty_loop_output = WPV_View_Base::generate_loop_output();

	$defaults = array(
		'view_settings' => array(
			'view-query-mode' => 'archive',
			'sections-show-hide' => array(
                'content' => 'off',
            )
		),
		'view_layout_settings' => array(
		    // almost all of this settings are only needed to create the layout on the fly, so they are not needed here
			'additional_js' => '',
			'layout_meta_html' => $empty_loop_output['loop_output_settings']['layout_meta_html'],
		),
	);
	return $defaults[ $settings ];
}