public function for_post($type, $name = false)
 {
     if (!$this->needed_components_loaded()) {
         return false;
     }
     global $WPV_settings;
     $option = sanitize_text_field(sprintf('views_template_for_%s', $type));
     // already has an content template
     if (isset($WPV_settings[$option]) && is_numeric($WPV_settings[$option]) && $WPV_settings[$option] > 0) {
         return $WPV_settings[$option];
     }
     if (!$name) {
         $type_object = get_post_type_object($type);
         $name = sprintf(__('Template for %s', 'types'), $type_object->labels->name);
     }
     $name = $this->validate_name($name);
     if (!$name) {
         return false;
     }
     $ct = WPV_Content_Template::create($name);
     $ct_post = get_post($ct->id);
     if ($ct_post === null) {
         return false;
     }
     $WPV_settings[$option] = $ct_post->ID;
     $WPV_settings->save();
     $posts = get_posts('post_type=' . $type . '&post_status=any&posts_per_page=-1&fields=ids');
     foreach ($posts as $id) {
         $ct = get_post_meta($id, '_views_template', true);
         if (empty($ct)) {
             update_post_meta($id, '_views_template', $ct_post->ID);
         }
     }
     return $ct_post->ID;
 }
 /**
  * Creates a content template for a given post type
  *
  * @param $type
  * @param bool|string $name Name for the Content Template
  *
  * @return bool
  * @since 2.0
  */
 public function for_post($type, $name = false)
 {
     // abort if any needed dependency is not available
     if (!$this->needed_components_loaded()) {
         return false;
     }
     global $WPV_settings;
     // option key for Views Content Templates is "views_template_for_{post-type-name}"
     $option = sanitize_text_field(sprintf('views_template_for_%s', $type));
     // already has an content template
     if (isset($WPV_settings[$option]) && is_numeric($WPV_settings[$option]) && $WPV_settings[$option] > 0) {
         return $WPV_settings[$option];
     }
     // create name if not given
     if (!$name) {
         $type_object = get_post_type_object($type);
         $name = sprintf(__('Template for %s', 'types'), $type_object->labels->name);
     }
     $name = $this->validate_name($name);
     // abort if name not valid (shouldn't happen, see validate_name())
     if (!$name) {
         return false;
     }
     // create template
     $ct = WPV_Content_Template::create($name);
     $ct_post = get_post($ct->id);
     if ($ct_post === null) {
         return false;
     }
     $WPV_settings[$option] = $ct_post->ID;
     $WPV_settings->save();
     // get all posts of post type to assign the new content template
     $posts = get_posts('post_type=' . $type . '&post_status=any&posts_per_page=-1&fields=ids');
     foreach ($posts as $id) {
         $ct = get_post_meta($id, '_views_template', true);
         // only assign if there is not already an assigned content template
         if (empty($ct)) {
             update_post_meta($id, '_views_template', $ct_post->ID);
         }
     }
     return $ct_post->ID;
 }
    /**
     * Clone a Content Template.
     *
     * @param string $title Title of the new CT.
     * @param bool $adjust_duplicate_title If true, the title might get changed in order to ensure it's uniqueness.
     *     Otherwise, if $title is not unique, the cloning will fail.
     * @return null|WPV_Content_Template The cloned CT or null on failure.
     *
     * @since 1.9
     */
    public function clone_this( $title, $adjust_duplicate_title = true ) {

        // Create new CT
        $cloned_ct = WPV_Content_Template::create( $title, $adjust_duplicate_title );
        if( null == $cloned_ct ) {
            return null;
        }


        // Copy postmeta
        $cloned_ct->defer_after_update_actions();
        $postmeta_defaults = $this->get_postmeta_defaults();
        foreach( $postmeta_defaults as $meta_key => $ignored_value ) {
            if( !in_array( $meta_key, WPV_Content_Template::$postmeta_keys_not_to_clone ) ) {
                $cloned_ct->update_postmeta( $meta_key, $this->get_postmeta( $meta_key ) );
            }
        }
        $cloned_ct->resume_after_update_actions();

        // Copy content
        $cloned_ct->content_raw = $this->content_raw;

        return $cloned_ct;
    }
    /**
     * Create new Content Template and set it as this View's Loop template.
     *
     * Note that this method doesn't care about existing Loop template. That should be handled separately before it
     * is called.
     *
     * @param string $title Valid title for the Content Template (will be adjusted if not unique).
     * @param string $content Content of the CT
     * @throws RuntimeException
     * @return WPV_Content_Template Newly created CT.
     * @since 1.10
     */
    public function create_loop_template( $title, $content = '[wpv-post-link]' ) {

        $ct = WPV_Content_Template::create( $title, true );
        if( null == $ct ) {
            throw new RuntimeException( 'couldn\'t create the loop template' );
        }

        $ct->defer_after_update_actions();

        // Update Loop output and content of the Loop template
        $ct->content = $content;

        $this->loop_meta_html = str_replace(
            '<wpv-loop>',
            sprintf( "<wpv-loop>\n\t\t\t[wpv-post-body view_template=\"%s\"]", $ct->title ),
            $this->loop_meta_html
        );

        // Create bindings between View and Loop template
        $this->loop_included_ct_ids = $ct->id;

        $this->loop_template_id = $ct->id;
        $ct->loop_output_id = $this->id;

        $ct->resume_after_update_actions();

        return $ct;
    }
/**
 * wpv_create_content_template
 *
 * Creates a new Content Template given a title and an optional suffix.
 *
 * Consider using WPV_Content_Template::create_new directly.
 *
 * @note Used by Layouts plugin.
 *
 * @param string $title
 * @param string $suffix
 * @param bool $force Whether to force the creation of the Template by incremental numbers added to the title in case it is already in use
 * @param string $content
 *
 * @return array {
 *     'success' => (int) The ID of the CT created
 *      'error' => (string) Error message
 *      'title' => (string) The title of the CT created or the one that made this fail
 *
 * @since 1.7
 */
function wpv_create_content_template( $title, $suffix = '', $force = true, $content = '' ) {

	$real_suffix = '';
	if ( ! empty( $suffix ) ) {
		$real_suffix = ' - ' . $suffix;
	}
    $template_title = $title . $real_suffix;

    $result = array();

    if( $force ) {
        $ct = WPV_Content_Template::create( $template_title, true );
    } else {

        if( WPV_Content_Template::is_name_used( $template_title ) ) {
            $result['error'] = __( 'A Content Template with that title already exists. Please use another title.', 'wpv-views' );
            $result['title'] = $template_title;
            return $result;
        }

        $ct = WPV_Content_Template::create( $template_title, false );
    }

    if( null == $ct ) {
        $return['title'] = $template_title;
        $return['error'] = __( 'An error occurred while creating a Content Template.', 'wpv-views' );
    } else {

        $return['title'] = $ct->title;

        try {
            $ct->content_raw = $content;
            $return['success'] = $ct->id;
        } catch( Exception $e ) {
            $return['error'] = __( 'An error occurred while creating a Content Template.', 'wpv-views' );
        }

    }

    return $return;
}
Beispiel #6
0
/**
 * Handle creating a new Content Template with given parameters.
 *
 * @param string $title Title for the CT.
 * @param array $usage See wpv_ct_editor_page() description.
 * @return null|WPV_Content_Template A CT object or null if the creation has failed.
 *
 * @since 1.9
 */
function wpv_ct_editor_page_create( $title, $usage ) {

    // Create new Content Template
    $ct = WPV_Content_Template::create( $title );

    if( null == $ct ) {
        return null;
    }

    // Process the assignments to post types and taxonomies
    $single_post_types_assigned = wpv_ct_editor_assign_usage( $ct, 'single_post_types', $usage );
    $post_archives_assigned = wpv_ct_editor_assign_usage( $ct, 'post_archives', $usage );
    $taxonomy_archives_assigned = wpv_ct_editor_assign_usage( $ct, 'taxonomy_archives', $usage );

    if( !$single_post_types_assigned || !$post_archives_assigned || !$taxonomy_archives_assigned ) {
        return null;
    }

    return $ct;
}