WP_Customize_Manager::prepare_starter_content_attachments()protectedWP 4.7.0

Prepares starter content attachments.

Ensure that the attachments are valid and that they have slugs and file name/path.

Method of the class: WP_Customize_Manager{}

No Hooks.

Return

Array. Prepared attachments.

Usage

// protected - for code of main (parent) or child class
$result = $this->prepare_starter_content_attachments( $attachments );
$attachments(array) (required)
Attachments.

Changelog

Since 4.7.0 Introduced.

WP_Customize_Manager::prepare_starter_content_attachments() code WP 6.5.2

protected function prepare_starter_content_attachments( $attachments ) {
	$prepared_attachments = array();
	if ( empty( $attachments ) ) {
		return $prepared_attachments;
	}

	// Such is The WordPress Way.
	require_once ABSPATH . 'wp-admin/includes/file.php';
	require_once ABSPATH . 'wp-admin/includes/media.php';
	require_once ABSPATH . 'wp-admin/includes/image.php';

	foreach ( $attachments as $symbol => $attachment ) {

		// A file is required and URLs to files are not currently allowed.
		if ( empty( $attachment['file'] ) || preg_match( '#^https?://$#', $attachment['file'] ) ) {
			continue;
		}

		$file_path = null;
		if ( file_exists( $attachment['file'] ) ) {
			$file_path = $attachment['file']; // Could be absolute path to file in plugin.
		} elseif ( is_child_theme() && file_exists( get_stylesheet_directory() . '/' . $attachment['file'] ) ) {
			$file_path = get_stylesheet_directory() . '/' . $attachment['file'];
		} elseif ( file_exists( get_template_directory() . '/' . $attachment['file'] ) ) {
			$file_path = get_template_directory() . '/' . $attachment['file'];
		} else {
			continue;
		}
		$file_name = wp_basename( $attachment['file'] );

		// Skip file types that are not recognized.
		$checked_filetype = wp_check_filetype( $file_name );
		if ( empty( $checked_filetype['type'] ) ) {
			continue;
		}

		// Ensure post_name is set since not automatically derived from post_title for new auto-draft posts.
		if ( empty( $attachment['post_name'] ) ) {
			if ( ! empty( $attachment['post_title'] ) ) {
				$attachment['post_name'] = sanitize_title( $attachment['post_title'] );
			} else {
				$attachment['post_name'] = sanitize_title( preg_replace( '/\.\w+$/', '', $file_name ) );
			}
		}

		$attachment['file_name']         = $file_name;
		$attachment['file_path']         = $file_path;
		$prepared_attachments[ $symbol ] = $attachment;
	}
	return $prepared_attachments;
}