'page', 'post_status' => array( 'publish', 'draft', 'pending', 'private', 'future' ), 'posts_per_page' => 1, 'meta_key' => AIEO_Content::META_RECIPE, 'meta_value' => AIEO_Content::recipe( $key ), 'no_found_rows' => true, ) ); if ( $posts ) { return array( 'post' => $posts[0], 'adopted' => false ); } $slug = '' !== (string) ( $spec['slug'] ?? '' ) ? (string) $spec['slug'] : $key; $posts = get_posts( array( 'post_type' => 'page', 'post_status' => array( 'publish', 'draft', 'pending', 'private', 'future' ), 'name' => $slug, 'posts_per_page' => 1, 'no_found_rows' => true, ) ); return $posts ? array( 'post' => $posts[0], 'adopted' => true ) : null; } public static function media_defaults( array $opts = array() ) { return array( 'media_dir' => (string) ( $opts['media_dir'] ?? '' ), 'wp_content_dir' => (string) ( $opts['wp_content_dir'] ?? '' ), ); } private static function media_opts( array $opts, $sideload ) { return self::media_defaults( $opts ) + array( 'sideload' => (bool) $sideload ); } private static function check( array $spec, array $opts, $check_files ) { $check = AIEO_Content_Spec::validate( $spec, self::media_opts( $opts, false ) + array( 'check_files' => $check_files ) ); $template = (string) ( $spec['template'] ?? '' ); if ( '' !== $template && ! self::template_ok( $template ) ) { $check['errors'][] = sprintf( 'template: %s is not a page template of the active theme (%s).', $template, get_stylesheet() ); } if ( $check['errors'] || ( ! empty( $opts['strict'] ) && $check['warnings'] ) ) { $lines = $check['errors']; if ( ! empty( $opts['strict'] ) ) { $lines = array_merge( $lines, array_map( static fn ( $w ) => 'strict: ' . $w, $check['warnings'] ) ); } return new WP_Error( 'aieo_content_invalid', implode( "\n", $lines ), $check ); } return $check; } public static function template_ok( $template ) { $template = (string) $template; return 'default' === $template || array_key_exists( $template, (array) wp_get_theme()->get_page_templates( null, 'page' ) ); } private static function permitted( $post, array $spec, array $opts ) { $deny = static function ( $message ) { return new WP_Error( 'aieo_content_forbidden', $message, array( 'status' => 403 ) ); }; if ( $post instanceof WP_Post && ! current_user_can( 'edit_post', (int) $post->ID ) ) { return $deny( sprintf( 'You may not edit the page %d.', (int) $post->ID ) ); } if ( ! empty( $opts['dry_run'] ) ) { return true; } $apply = 'apply' === (string) ( $opts['mode'] ?? 'draft' ); $status = (string) ( $opts['status'] ?? '' ); if ( '' !== $status && ! in_array( $status, array( 'draft', 'pending', 'private', 'publish' ), true ) ) { return new WP_Error( 'aieo_content_status', 'status: draft, pending, private or publish.', array( 'status' => 400 ) ); } if ( ! $post && ! current_user_can( 'edit_pages' ) ) { return $deny( 'You may not create pages.' ); } if ( $apply && in_array( $status, array( 'publish', 'private' ), true ) && ! current_user_can( 'publish_pages' ) ) { return $deny( 'You may not publish pages.' ); } if ( $apply && ! empty( $spec['front_page'] ) && ! current_user_can( 'manage_options' ) ) { return $deny( 'You may not change the site\'s front page.' ); } return true; } public static function compile( array $spec, array $opts = array() ) { $media = AIEO_Content_Media::resolve( (array) ( $spec['media'] ?? array() ), self::media_opts( $opts, ! empty( $opts['sideload'] ) ) ); $spec = AIEO_Content_Media::tokens( $spec, $media['map'] ); $out = array(); $sections = array_values( (array) $spec['sections'] ); $r1 = AIEO_Content_Rooster_One::NAME === (string) ( $spec['display'] ?? '' ); if ( $r1 ) { $sections = AIEO_Content_Rooster_One::fold( $sections ); } foreach ( $sections as $i => $s ) { $s = AIEO_Content_Sections::with_defer( (array) $s, $i ); if ( ! $r1 && 'hero' === (string) ( $s['kind'] ?? '' ) && isset( $sections[ $i + 1 ] ) && AIEO_Content_Sections::is_strip( (array) $sections[ $i + 1 ] ) ) { $s['strip'] = (array) ( $sections[ $i + 1 ]['items'] ?? $sections[ $i + 1 ]['stats'] ?? array() ); } if ( 'hero' === (string) ( $s['kind'] ?? '' ) && empty( $s['family'] ) && ! empty( $spec['accent'] ) ) { $s['family'] = (string) $spec['accent']; } if ( ! $r1 && $i > 0 && AIEO_Content_Sections::is_strip( $s ) && 'hero' === (string) ( $sections[ $i - 1 ]['kind'] ?? '' ) ) { continue; } if ( in_array( (string) ( $spec['display'] ?? '' ), AIEO_Content_Sections::DISPLAYS, true ) ) { $s['_display'] = (string) $spec['display']; } if ( $i > 0 ) { $s['_prev_kind'] = (string) ( $sections[ $i - 1 ]['kind'] ?? '' ); $s['_prev_layout'] = (string) ( $sections[ $i - 1 ]['layout'] ?? '' ); } if ( ! empty( $spec['accent'] ) ) { $s['_family'] = (string) $spec['accent']; } $markup = AIEO_Content_Sections::render( $s, $media['map'] ); if ( '' !== $markup ) { $out[ (string) $s['key'] ] = $markup; } } return array( 'sections' => $out, 'media' => $media, 'errors' => $media['errors'], 'warnings' => $media['warnings'] ); } public static function diff( array $spec, array $opts = array() ) { $check = self::check( $spec, $opts, false ); if ( is_wp_error( $check ) ) { return $check; } $found = self::find( $spec ); $allowed = self::permitted( $found ? $found['post'] : null, $spec, array( 'dry_run' => true ) ); if ( is_wp_error( $allowed ) ) { return $allowed; } $compiled = self::compile( $spec, self::media_opts( $opts, false ) ); $plan = AIEO_Content_Merge::plan( $found ? (string) $found['post']->post_content : '', $compiled['sections'], $opts['force'] ?? false ); return array( 'key' => (string) $spec['key'], 'page_id' => $found ? (int) $found['post']->ID : 0, 'status' => $found ? (string) $found['post']->post_status : '', 'adopted' => $found ? (bool) $found['adopted'] : false, 'actions' => $plan['actions'], 'conflicts' => $plan['conflicts'], 'kept' => $plan['kept'], 'warnings' => array_merge( $check['warnings'], $compiled['warnings'] ), 'media' => array_keys( array_filter( $compiled['media']['map'], static fn ( $m ) => 0 === (int) $m['id'] ) ), ); } public static function apply( array $spec, array $opts = array() ) { $mode = 'apply' === (string) ( $opts['mode'] ?? 'draft' ) ? 'apply' : 'draft'; $dry = ! empty( $opts['dry_run'] ); $check = self::check( $spec, $opts, ! $dry ); if ( is_wp_error( $check ) ) { return $check; } $found = self::find( $spec ); $post = $found ? $found['post'] : null; $allowed = self::permitted( $post, $spec, array( 'mode' => $mode ) + $opts ); if ( is_wp_error( $allowed ) ) { return $allowed; } $compiled = self::compile( $spec, self::media_opts( $opts, ! $dry ) ); if ( $compiled['errors'] ) { return new WP_Error( 'aieo_content_media', implode( "\n", $compiled['errors'] ) ); } $plan = AIEO_Content_Merge::plan( $post ? (string) $post->post_content : '', $compiled['sections'], $opts['force'] ?? false ); $report = array( 'key' => (string) $spec['key'], 'mode' => $dry ? 'dry-run' : $mode, 'page_id' => $post ? (int) $post->ID : 0, 'created' => false, 'adopted' => $found ? (bool) $found['adopted'] : false, 'status' => $post ? (string) $post->post_status : '', 'actions' => $plan['actions'], 'conflicts' => $plan['conflicts'], 'kept' => $plan['kept'], 'warnings' => array_merge( $check['warnings'], $compiled['warnings'] ), 'sideloaded' => $compiled['media']['sideloaded'], 'sections' => count( $compiled['sections'] ), 'bytes' => strlen( $plan['content'] ), ); if ( $dry ) { $report['media'] = array_keys( array_filter( $compiled['media']['map'], static fn ( $m ) => 0 === (int) $m['id'] ) ); return $report; } $fields = array( 'post_type' => 'page', 'post_title' => AIEO_Content_Inline::plain( (string) $spec['title'] ), 'post_name' => '' !== (string) ( $spec['slug'] ?? '' ) ? (string) $spec['slug'] : (string) $spec['key'], 'post_content' => $plan['content'], 'post_excerpt' => (string) ( $spec['seo']['description'] ?? '' ), 'comment_status' => 'closed', 'ping_status' => 'closed', ); if ( ! $post ) { $status = (string) ( $opts['status'] ?? '' ); $fields['post_status'] = 'publish' === $status && 'apply' === $mode ? 'publish' : 'draft'; $id = wp_insert_post( wp_slash( $fields ), true ); if ( is_wp_error( $id ) ) { return $id; } self::meta( (int) $id, $spec, 'apply' === $mode, $report ); $report['page_id'] = (int) $id; $report['created'] = true; $report['status'] = $fields['post_status']; $report['preview_url'] = (string) get_preview_post_link( (int) $id ); $report['edit_url'] = (string) get_edit_post_link( (int) $id, 'raw' ); return $report; } $id = (int) $post->ID; if ( 'draft' === $mode ) { if ( ! function_exists( 'wp_create_post_autosave' ) ) { require_once ABSPATH . 'wp-admin/includes/post.php'; } $autosave = wp_create_post_autosave( array( 'post_ID' => $id, 'post_type' => 'page', 'post_title' => $fields['post_title'], 'post_content' => $fields['post_content'], 'post_excerpt' => $fields['post_excerpt'], ) ); if ( is_wp_error( $autosave ) ) { return $autosave; } $report['autosave_id'] = (int) $autosave; $report['preview_url'] = add_query_arg( array( 'preview_id' => $id, 'preview_nonce' => wp_create_nonce( 'post_preview_' . $id ), 'preview' => 'true' ), (string) get_permalink( $id ) ); $report['edit_url'] = (string) get_edit_post_link( $id, 'raw' ); $report['note'] = 'The meta (recipe, accent, plugin pages, template, front page) is written on apply.'; return $report; } $changes = 'Rooster\Modules\Abilities\Changes'; if ( class_exists( $changes ) && method_exists( $changes, 'snapshot' ) ) { $before = (int) $changes::snapshot( $id ); } else { $before = wp_save_post_revision( $id ); if ( ! is_int( $before ) || ! $before ) { $latest = wp_get_post_revisions( $id, array( 'posts_per_page' => 1, 'fields' => 'ids' ) ); $before = $latest ? (int) reset( $latest ) : 0; } } if ( ! empty( $opts['status'] ) ) { $fields['post_status'] = (string) $opts['status']; } $fields['ID'] = $id; $result = wp_update_post( wp_slash( $fields ), true ); if ( is_wp_error( $result ) ) { return $result; } self::meta( $id, $spec, true, $report ); $report['status'] = (string) get_post_status( $id ); $report['revision_id'] = is_int( $before ) ? $before : 0; $report['edit_url'] = (string) get_edit_post_link( $id, 'raw' ); if ( class_exists( $changes ) && method_exists( $changes, 'record' ) && $report['revision_id'] ) { $token = $changes::record( 'post', array( 'id' => $id, 'revision' => $report['revision_id'] ), 'Page apply: ' . (string) $spec['key'], 'rooster/apply-page' ); if ( is_string( $token ) && '' !== $token ) { $report['undo_token'] = $token; } } else { $report['note'] = 'No Changes::record() here (the theme 1.1.0 engine is not the live one yet): undo by restoring revision ' . $report['revision_id'] . ' in the editor.'; } return $report; } private static function meta( $id, array $spec, $applied, array &$report ) { update_post_meta( $id, AIEO_Content::META_RECIPE, AIEO_Content::recipe( (string) $spec['key'] ) ); if ( ! empty( $spec['accent'] ) ) { update_post_meta( $id, AIEO_Content::META_ACCENT, sanitize_key( (string) $spec['accent'] ) ); } else { delete_post_meta( $id, AIEO_Content::META_ACCENT ); } if ( array_key_exists( 'plugin_pages', $spec ) ) { $keys = array_values( array_unique( array_filter( array_map( 'sanitize_key', array_filter( (array) $spec['plugin_pages'], 'is_string' ) ) ) ) ); if ( $keys ) { update_post_meta( $id, AIEO_Content::META_PLUGIN_PAGES, $keys ); } else { delete_post_meta( $id, AIEO_Content::META_PLUGIN_PAGES ); } } if ( ! empty( $spec['template'] ) ) { update_post_meta( $id, '_wp_page_template', (string) $spec['template'] ); } if ( ! empty( $spec['front_page'] ) && $applied && 'publish' === get_post_status( $id ) ) { update_option( 'show_on_front', 'page' ); update_option( 'page_on_front', (int) $id ); $report['front_page'] = true; } elseif ( ! empty( $spec['front_page'] ) ) { $report['front_page'] = false; $report['note'] = 'front_page is set once the page is published with --mode=apply.'; } } }