Пример #1
0
/**
 * View duplicate callback function.
 *
 * Expects following POST arguments:
 * - wpnonce: A valid wpv_duplicate_view_nonce.
 * - id: View ID.
 * - name: Name of the new View.
 *
 * Refer to WPV_View::duplicate() for more information about the duplication itself.
 *
 * @since unknown
 */
function wpv_duplicate_this_view_callback() {
	wpv_ajax_authenticate( 'wpv_duplicate_view_nonce', array( 'parameter_source' => 'post', 'type_of_death' => 'data' ) );
	
    $post_id = (int) wpv_getpost( 'id', 0 );
    $post_name= sanitize_text_field( wpv_getpost( 'name', '' ) );
	if ( ( 0 == $post_id ) || empty( $post_name ) ) {
		$data = array(
			'message' => __('Wrong data', 'wpv-views')
		);
		wp_send_json_error( $data );
	}

    if ( WPV_View_Base::is_name_used( $post_name ) ) {
        $data = array(
			'message' => __( 'A View with that name already exists. Please use another name', 'wpv-views' )
		);
		wp_send_json_error( $data );
	}

    // Get the original View.
    $original_view = WPV_View::get_instance( $post_id );
    if( null == $original_view ) {
		$data = array(
			'message' => __('Wrong data', 'wpv-views')
		);
		wp_send_json_error( $data );
    }
    
    $duplicate_view_id = $original_view->duplicate( $post_name );
    if ( $duplicate_view_id ) {
        // original post id (shouldn't we rather return new id?)
        wp_send_json_success();
    } else {
        $data = array(
			'message' => __( 'Unexpected error', 'wpv-views' )
		);
		wp_send_json_error( $data );
    }

}
function wpv_update_filter_extra_callback() {
    // Authentication
	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"], 'wpv_view_filter_extra_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 );
	}

    // Get the View
    $view_id = (int) wpv_getpost( 'id', 0 );
    $view = WPV_View::get_instance( $view_id );
    if ( $view_id < 1 || null == $view ) {
		$data = array(
			'type' => 'id',
			'message' => __( 'Wrong or missing ID.', 'wpv-views' )
		);
		wp_send_json_error( $data );
	}

    // Update View settings. Note that if any of those properties fail to update, nothing will be saved -
    // that doesn't happen until finish_modifying_view_settings() is called.
    try {
        $view->begin_modifying_view_settings();

        $filter_meta_html = wpv_getpost('query_val', null);
        if (null != $filter_meta_html) {
            $view->filter_meta_html = $filter_meta_html;
        }
        $view->filter_css = wpv_getpost('query_css_val');
        $view->filter_js = wpv_getpost('query_js_val');

        $view->finish_modifying_view_settings();
    } catch ( WPV_RuntimeExceptionWithMessage $e ) {
        wp_send_json_error( array( 'type' => '', 'message' => $e->getUserMessage() ) );
    } catch ( Exception $e ) {
        wp_send_json_error( array( 'type' => '', 'message' => __( 'An unexpected error ocurred.', 'wpv-views' ) ) );
    }

    // Indicate success.
	$data = array(
		'id' => $view_id,
		'message' => __( 'Filter saved', 'wpv-views' )
	);
	wp_send_json_success( $data );
}