$structure, 'errors' => array( __( 'Enter at least one page name before creating pages.', '53mp-fast-bulk-page-creator' ), ), ) ); self::redirect_back(); } $options = self::sanitize_page_options( wp_unslash( $_POST ) ); if ( 'publish' === $options['status'] && ! current_user_can( 'publish_pages' ) ) { $options['status'] = 'pending'; } if ( 'future' === $options['status'] && empty( $options['post_date'] ) ) { self::store_result( array( 'structure' => $structure, 'errors' => array( __( 'A future date is required when the status is Scheduled.', '53mp-fast-bulk-page-creator' ), ), ) ); self::redirect_back(); } $created = array(); $errors = array(); $created_ids = array(); $menu_order_by_parent = array(); foreach ( $pages as $index => $page ) { $parent_id = 0; if ( null !== $page['parent_index'] && isset( $created_ids[ $page['parent_index'] ] ) ) { $parent_id = (int) $created_ids[ $page['parent_index'] ]; } elseif ( $options['parent'] ) { $parent_id = $options['parent']; } if ( ! isset( $menu_order_by_parent[ $parent_id ] ) ) { $menu_order_by_parent[ $parent_id ] = $options['menu_order']; } $postarr = array( 'post_title' => $page['title'], 'post_type' => 'page', 'post_status' => $options['status'], 'post_author' => $options['author'], 'post_parent' => $parent_id, 'post_content' => '', 'post_excerpt' => $options['excerpt'], 'comment_status' => $options['comment_status'], 'ping_status' => $options['ping_status'], 'menu_order' => $menu_order_by_parent[ $parent_id ], 'post_password' => $options['password'], ); ++$menu_order_by_parent[ $parent_id ]; if ( '' !== $page['slug'] ) { $postarr['post_name'] = $page['slug']; } if ( '' !== $options['post_date'] ) { $postarr['post_date'] = $options['post_date']; $postarr['post_date_gmt'] = get_gmt_from_date( $options['post_date'] ); } $page_id = wp_insert_post( $postarr, true ); if ( is_wp_error( $page_id ) ) { $errors[] = sprintf( /* translators: 1: page title, 2: error message */ __( 'Could not create ā€œ%1$sā€: %2$s', '53mp-fast-bulk-page-creator' ), $page['title'], wp_strip_all_tags( $page_id->get_error_message() ) ); continue; } if ( '' !== $options['template'] ) { update_post_meta( $page_id, '_wp_page_template', $options['template'] ); } $created_ids[ $index ] = $page_id; $created[] = array( 'id' => $page_id, 'title' => $page['title'], ); } self::store_result( array( 'created' => $created, 'errors' => $errors, 'structure' => $errors ? $structure : '', ) ); self::redirect_back(); } /** * Renders the settings page. */ public static function render_settings_page() { if ( ! current_user_can( 'edit_pages' ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', '53mp-fast-bulk-page-creator' ) ); } $result = self::get_result(); $structure = isset( $result['structure'] ) ? (string) $result['structure'] : ''; $authors = get_users( array( 'capability' => 'edit_pages', 'orderby' => 'display_name', 'fields' => array( 'ID', 'display_name' ), ) ); $templates = wp_get_theme()->get_page_templates( null, 'page' ); $comment_on = 'open' === get_default_comment_status( 'page' ); $ping_on = 'open' === get_option( 'default_ping_status' ); $date_value = current_time( 'Y-m-d' ); $time_value = current_time( 'H:i' ); $placeholder = "Home\nAbout (about-us)\n\tTeam\n\tHistory (our-history)\nServices\n\tConsulting\n\t\tEnterprise\nContact"; ?>

*/ public static function parse_structure( $text ) { $pages = array(); $stack = array(); $lines = preg_split( '/\r\n|\r|\n/', (string) $text ); if ( ! is_array( $lines ) ) { return $pages; } foreach ( $lines as $line ) { if ( '' === trim( $line ) ) { continue; } $indent = ''; if ( preg_match( '/^([\t ]+)/', $line, $indent_match ) ) { $indent = $indent_match[1]; } if ( false !== strpos( $indent, "\t" ) ) { $level = substr_count( $indent, "\t" ); } else { $level = (int) floor( strlen( $indent ) / 2 ); } $content = trim( $line ); $title = $content; $slug = ''; if ( preg_match( '/^(.*)\s+\(([^)]+)\)\s*$/', $content, $parts ) && '' !== trim( $parts[1] ) ) { $title = trim( $parts[1] ); $slug = sanitize_title( $parts[2] ); } if ( '' === $title ) { continue; } $parent_index = null; for ( $i = $level - 1; $i >= 0; $i-- ) { if ( isset( $stack[ $i ] ) ) { $parent_index = $stack[ $i ]; break; } } $pages[] = array( 'title' => sanitize_text_field( $title ), 'slug' => $slug, 'level' => $level, 'parent_index' => $parent_index, ); $stack[ $level ] = count( $pages ) - 1; foreach ( array_keys( $stack ) as $key ) { if ( (int) $key > $level ) { unset( $stack[ $key ] ); } } } return $pages; } /** * Groups existing site pages by parent ID for the hierarchy dropdown. * * @return array> */ private static function get_pages_grouped_by_parent() { $pages = get_pages( array( 'sort_column' => 'menu_order,post_title', 'sort_order' => 'ASC', 'hierarchical' => false, 'post_status' => array( 'publish', 'draft', 'pending', 'private', 'future' ), 'number' => 0, ) ); if ( empty( $pages ) ) { return array(); } $ids = array(); foreach ( $pages as $page ) { $ids[ (int) $page->ID ] = true; } $grouped = array(); foreach ( $pages as $page ) { $parent_id = (int) $page->post_parent; if ( $parent_id && ! isset( $ids[ $parent_id ] ) ) { $parent_id = 0; } if ( ! isset( $grouped[ $parent_id ] ) ) { $grouped[ $parent_id ] = array(); } $grouped[ $parent_id ][] = $page; } return $grouped; } /** * Prints hierarchical