/**
  * Creates a WordPress Archive for a given post type
  *
  * @param $type
  * @param bool|string $name Name for the WordPress Archive
  *
  * @return bool
  * @since 2.0
  */
 public function for_post($type, $name = false)
 {
     // check dependencies
     if (!$this->needed_components_loaded()) {
         return false;
     }
     global $WPV_settings;
     $option = sanitize_text_field(sprintf('view_cpt_%s', $type));
     // for type 'post'
     if ($type == 'post') {
         $name = __('Archive for Home/Blog', 'types');
         $option = 'view_home-blog-page';
     }
     // already has an archive
     if (isset($WPV_settings[$option]) && is_numeric($WPV_settings[$option]) && $WPV_settings[$option] > 0) {
         return $WPV_settings[$option];
     }
     // set name if not given
     if (!$name) {
         $type_object = get_post_type_object($type);
         $name = sprintf(__('Archive for %s', 'types'), $type_object->labels->name);
     }
     $name = $this->validate_name($name);
     if (!$name) {
         return false;
     }
     $archive = WPV_WordPress_Archive::create($name, array());
     $archive_post = get_post($archive->id);
     if ($archive_post === null) {
         return false;
     }
     $WPV_settings[$option] = $archive_post->ID;
     $WPV_settings->save();
     return $archive_post->ID;
 }
Example #2
0
/**
 * 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' ) );
    }
}