/** * Create a new Content Template with default settings. * * @param string $title Content Template title. * @param bool $adjust_duplicate_title If true, change the title if it's not unique among Content Templates. You can (and * should) check for the value that was actually saved to database through the returned $ct->title. If this is * false and the title is not unique (determined by is_name_used()), the operation will fail. * * @return null|WPV_Content_Template CT object or null if creating has failed. * * @since 1.9 */ public static function create( $title, $adjust_duplicate_title = true ) { $sanitized_title = sanitize_text_field( $title ); // Handle empty title if( empty( $sanitized_title ) ) { if( $adjust_duplicate_title ) { $sanitized_title = sanitize_text_field( __( 'Content Template', 'wpv-views' ) ); } else { // empty title, but we're not allowed to adjust it -> fail return null; } } // Ensure title uniqueness (or fail) $is_title_unique = ! WPV_Content_Template_Embedded::is_name_used( $sanitized_title ); if( !$is_title_unique ) { if( $adjust_duplicate_title ) { $sanitized_title = WPV_Content_Template::get_unique_title( $sanitized_title ); } else { // Non-unique title & we're not allowed to re-use it -> fail return null; } } // Insert the post in database. $post_id = wp_insert_post( array( 'post_title' => $sanitized_title, 'post_status' => 'publish', 'post_type' => WPV_Content_Template_Embedded::POST_TYPE, 'post_content' => '' ), false ); // Create the CT object or fail $ct = WPV_Content_Template::get_instance( $post_id ); if( null == $ct ) { return null; } $ct->defer_after_update_actions(); // Save default postmeta values foreach( WPV_Content_Template_Embedded::$postmeta_defaults as $meta_key => $meta_value ) { $ct->update_postmeta( $meta_key, $meta_value ); } // After update action will be called exactly once. $ct->maybe_after_update_action(); $ct->resume_after_update_actions(); return $ct; }