function et_theme_builder_store_template( $theme_builder_id, $template, $allow_default ) { if ( ! current_user_can( 'edit_others_posts' ) ) { wp_die(); } $_ = et_(); $raw_post_id = $_->array_get( $template, 'id', 0 ); $post_id = is_numeric( $raw_post_id ) ? (int) $raw_post_id : 0; $title = sanitize_text_field( $_->array_get( $template, 'title', '' ) ); $default = $allow_default && '1' === $_->array_get( $template, 'default', '1' ); $enabled = '1' === $_->array_get( $template, 'enabled', '1' ); $header_id = (int) $_->array_get( $template, 'layouts.header.id', 0 ); $header_enabled = (bool) $_->array_get( $template, 'layouts.header.enabled', true ); $body_id = (int) $_->array_get( $template, 'layouts.body.id', 0 ); $body_enabled = (bool) $_->array_get( $template, 'layouts.body.enabled', true ); $footer_id = (int) $_->array_get( $template, 'layouts.footer.id', 0 ); $footer_enabled = (bool) $_->array_get( $template, 'layouts.footer.enabled', true ); $use_on = array_map( 'sanitize_text_field', $_->array_get( $template, 'use_on', array() ) ); $exclude_from = array_map( 'sanitize_text_field', $_->array_get( $template, 'exclude_from', array() ) ); $exists = $post_id > 0 && ET_THEME_BUILDER_TEMPLATE_POST_TYPE === get_post_type( $post_id ) && 'publish' === get_post_status( $post_id ); $autogenerated_title = '1' === $_->array_get( $template, 'autogenerated_title', '1' ); $onboarding = (bool) $_->array_get( $template, 'onboarding', '0' ); if ( ET_THEME_BUILDER_HEADER_LAYOUT_POST_TYPE !== get_post_type( $header_id ) || 'publish' !== get_post_status( $header_id ) ) { $header_id = 0; } if ( ET_THEME_BUILDER_BODY_LAYOUT_POST_TYPE !== get_post_type( $body_id ) || 'publish' !== get_post_status( $body_id ) ) { $body_id = 0; } if ( ET_THEME_BUILDER_FOOTER_LAYOUT_POST_TYPE !== get_post_type( $footer_id ) || 'publish' !== get_post_status( $footer_id ) ) { $footer_id = 0; } if ( $exists ) { // Preform update only when needed. if ( get_post_field( 'post_title', $post_id, 'raw' ) !== $title ) { wp_update_post( array( 'ID' => $post_id, 'post_title' => $title, ) ); // Update layout title for each template. et_theme_builder_update_layout_title( $template ); } update_post_meta( $post_id, '_et_onboarding_created', '0' ); } else { $post_id = wp_insert_post( array( 'post_type' => ET_THEME_BUILDER_TEMPLATE_POST_TYPE, 'post_status' => 'publish', 'post_title' => $title, ) ); if ( $onboarding ) { update_post_meta( $post_id, '_et_onboarding_created', '1' ); } } if ( 0 === $post_id || is_wp_error( $post_id ) ) { return false; } $metas = array( '_et_autogenerated_title' => $autogenerated_title ? '1' : '0', '_et_default' => $default ? '1' : '0', '_et_enabled' => $enabled ? '1' : '0', '_et_header_layout_id' => $header_id, '_et_header_layout_enabled' => $header_enabled ? '1' : '0', '_et_body_layout_id' => $body_id, '_et_body_layout_enabled' => $body_enabled ? '1' : '0', '_et_footer_layout_id' => $footer_id, '_et_footer_layout_enabled' => $footer_enabled ? '1' : '0', ); foreach ( $metas as $key => $value ) { if ( strval( $value ) === strval( get_post_meta( $post_id, $key, true ) ) ) { continue; } update_post_meta( $post_id, $key, $value ); } if ( $onboarding ) { $onboarding_use_on = get_post_meta( $post_id, '_et_use_on' ); $onboarding_exclude_from = get_post_meta( $post_id, '_et_exclude_from' ); foreach ( $onboarding_use_on as $condition ) { add_post_meta( $post_id, '_et_old_use_on', $condition ); } foreach ( $onboarding_exclude_from as $condition ) { add_post_meta( $post_id, '_et_old_exclude_from', $condition ); } } // Handle _et_use_on meta. delete_post_meta( $post_id, '_et_use_on' ); if ( $use_on ) { $use_on_unique = array_unique( $use_on ); foreach ( $use_on_unique as $condition ) { add_post_meta( $post_id, '_et_use_on', $condition ); } } // Handle _et_exclude_from meta. delete_post_meta( $post_id, '_et_exclude_from' ); if ( $exclude_from ) { $exclude_from_unique = array_unique( $exclude_from ); foreach ( $exclude_from_unique as $condition ) { add_post_meta( $post_id, '_et_exclude_from', $condition ); } } return $post_id; }