Пример #1
0
    /**
     * 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;
    }
Пример #2
0
/**
 * Generate default View settings or layout settings.
 *
 * This is now merely a wrapper around wpv_view_default_settings() and wpv_view_default_layout_settings().
 * Feel free to use those functions directly instead.
 *
 * @param string $settings Field: 'view_settings' or 'view_layout_settings'.
 * @param string $purpose Purpose of the view: 'all', 'pagination', 'slide', 'parametric' or 'full'.
 *
 * @return array Array with desired values or an empty array if invalid parameters are provided.
 *
 * @since unknown
 */
function wpv_view_defaults($settings = 'view_settings', $purpose = 'full')
{
    switch ($settings) {
        case 'view_settings':
            return wpv_view_default_settings($purpose);
        case 'view_layout_settings':
            return wpv_view_default_layout_settings($purpose);
        default:
            return array();
    }
}