/** * Create a new View. * * If the View purpose is set to "slider", also automatically create new Loop template. * * @param string $title New View title. Must be unique and valid (see validate_title()). * @param array $args ( * @type array $view_settings View settings that should override the default ones. Optional. * @type array $loop_settings Loop settings that should override the default ones. Optional. * @type bool $forbid_loop_template Never create a Loop template for this View. Optional, default is false. * ) * * @return WPV_View New View object. * * @throws InvalidArgumentException * @throws RuntimeException * @throws WPV_RuntimeExceptionWithMessage * * @note overriding default Views settings and layout settings must provide complete data when the element is an * array, because it overrides them all. For example, $args['settings']['pagination'] can not override just the * "postsper page" options: it must provide a complete pagination implementation. This might change and be corrected * in the future, keeping backwards compatibility. * * @since 1.10 */ public static function create( $title, $args ) { $view_id = WPV_View_Base::create_post( $title ); $view = new WPV_View( $view_id ); $view->defer_after_update_actions(); // Construct default View settings and Loop settings based on View purpose $view_settings = wpv_getarr( $args, 'view_settings', array() ); $view_settings[ WPV_View_Base::VIEW_SETTINGS_QUERY_MODE ] = 'normal'; $view_purpose = wpv_getarr( $view_settings, WPV_View_Embedded::VIEW_SETTINGS_PURPOSE, 'full', array( 'full', 'pagination', 'parametric', 'slider', 'all' ) ); $view_settings[ WPV_View_Embedded::VIEW_SETTINGS_PURPOSE ] = $view_purpose; $view_settings_default = wpv_view_default_settings( $view_purpose ); $view_settings = wp_parse_args( $view_settings, $view_settings_default ); $view->update_postmeta( WPV_View_Base::POSTMETA_VIEW_SETTINGS, $view_settings ); $loop_settings_default = wpv_view_default_layout_settings( $view_purpose ); $loop_settings = wpv_getarr( $args, 'loop_settings', array() ); $loop_settings = wp_parse_args( $loop_settings, $loop_settings_default ); $view->update_postmeta( WPV_View_Base::POSTMETA_LOOP_SETTINGS, $loop_settings ); // For the Slider purpose, automatically create a Loop template $forbid_loop_template = wpv_getarr( $args, 'forbid_loop_template', false ); if ( ! $forbid_loop_template && ( 'slider' == $view_purpose ) ) { $ct_title = sprintf( '%s - %s', $title, __( 'slide', 'wpv-views' ) ); $view->create_loop_template( $ct_title, '[wpv-post-link]' ); // I really hate this solution $view->update_postmeta( '_wpv_first_time_load', 'on' ); } $view->resume_after_update_actions(); return $view; }
public function for_post($type, $name = false) { if (!class_exists('WPV_View') || !method_exists('WPV_View', 'create')) { return false; } if (!$name) { $type_object = get_post_type_object($type); $name = sprintf(__('View for %s', 'types'), $type_object->labels->name); } $name = $this->validate_name($name); $args = array('view_settings' => array('view-query-mode' => 'normal', 'view_purpose' => 'all', 'post_type' => array($type))); $view = WPV_View::create($name, $args); return $view->id; }
/** * 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 ); }
/** * API function to create a new View * * @param $args array set of arguments for the new View * 'title' (string) (semi-mandatory) Title for the View * 'settings' (array) (optional) Array compatible with the View settings to override the defaults * 'layout_settings' (array) (optional) Array compatible with the View layout settings to override the defaults * * @return array response of the operation, one of the following * $return['success] = View ID * $return['error'] = 'Error message' * * @since 1.6.0 * * @note overriding default Views settings and layout settings must provide complete data when the element is an array, because it overrides them all. * For example, $args['settings']['pagination'] can not override just the "postsper page" options: it must provide a complete pagination implementation. * This might change and be corrected in the future, keeping backwards compatibility. * * @todo once we create a default layout for a View, we need to make sure that: * - the _view_loop_template postmeta is created and updated - DONE * - the fields added to that loop Template are stored in the layout settings - PENDING * - check how Layouts can apply this all to their Views, to create a Bootstrap loop by default - PENDING * * @deprecated Since 1.10. Consider using WPV_View::create() or WPV_WordPress_Archive::create() instead. */ function wpv_create_view( $args ) { $title = wpv_getarr( $args, 'title' ); $creation_args = $args; $view_settings = wpv_getarr( $args, 'settings', array() ); $creation_args['view_settings'] = $view_settings; $query_mode = wpv_getarr( $view_settings, WPV_View_Base::VIEW_SETTINGS_QUERY_MODE, 'normal', array( 'normal', 'archive', 'layouts-loop' ) ); try { if( 'normal' == $query_mode ) { $view = WPV_View::create( $title, $creation_args ); $id = $view->id; } else { $wpa = WPV_WordPress_Archive::create( $title, $creation_args ); $id = $wpa->id; } return array( 'success' => $id ); } catch( WPV_RuntimeExceptionWithMessage $e ) { return array( 'error' => $e->getUserMessage() ); } catch( Exception $e ) { return array( 'error' => __( 'The View could not be created.', 'wpv-views' ) ); } }