delete_theme()

最后更新于:2021-11-26 01:11:20

delete_theme( string$stylesheet, string$redirect=”)

Remove a theme

参数

$stylesheet

(string) (Required) Stylesheet of the theme to delete.

$redirect

(string) (Optional) Redirect to page when complete.

Default value: ”

响应

(bool|null|GC_Error) True on success, false if $stylesheet is empty, GC_Error on failure. Null if filesystem credentials are required to proceed.

源文件

文件: gc-admin/includes/theme.php

function delete_theme( $stylesheet, $redirect = '' ) {
	global $gc_filesystem;

	if ( empty( $stylesheet ) ) {
		return false;
	}

	if ( empty( $redirect ) ) {
		$redirect = gc_nonce_url( 'themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet );
	}

	ob_start();
	$credentials = request_filesystem_credentials( $redirect );
	$data        = ob_get_clean();

	if ( false === $credentials ) {
		if ( ! empty( $data ) ) {
			require_once ABSPATH . 'gc-admin/admin-header.php';
			echo $data;
			require_once ABSPATH . 'gc-admin/admin-footer.php';
			exit;
		}
		return;
	}

	if ( ! GC_Filesystem( $credentials ) ) {
		ob_start();
		// Failed to connect. Error and request again.
		request_filesystem_credentials( $redirect, '', true );
		$data = ob_get_clean();

		if ( ! empty( $data ) ) {
			require_once ABSPATH . 'gc-admin/admin-header.php';
			echo $data;
			require_once ABSPATH . 'gc-admin/admin-footer.php';
			exit;
		}
		return;
	}

	if ( ! is_object( $gc_filesystem ) ) {
		return new GC_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
	}

	if ( is_gc_error( $gc_filesystem->errors ) && $gc_filesystem->errors->has_errors() ) {
		return new GC_Error( 'fs_error', __( 'Filesystem error.' ), $gc_filesystem->errors );
	}

	// Get the base plugin folder.
	$themes_dir = $gc_filesystem->gc_themes_dir();
	if ( empty( $themes_dir ) ) {
		return new GC_Error( 'fs_no_themes_dir', __( 'Unable to locate GeChiUI theme directory.' ) );
	}

	/**
	 * Fires immediately before a theme deletion attempt.
	 *
	 * @since 5.8.0
	 *
	 * @param string $stylesheet Stylesheet of the theme to delete.
	 */
	do_action( 'delete_theme', $stylesheet );

	$themes_dir = trailingslashit( $themes_dir );
	$theme_dir  = trailingslashit( $themes_dir . $stylesheet );
	$deleted    = $gc_filesystem->delete( $theme_dir, true );

	/**
	 * Fires immediately after a theme deletion attempt.
	 *
	 * @since 5.8.0
	 *
	 * @param string $stylesheet Stylesheet of the theme to delete.
	 * @param bool   $deleted    Whether the theme deletion was successful.
	 */
	do_action( 'deleted_theme', $stylesheet, $deleted );

	if ( ! $deleted ) {
		return new GC_Error(
			'could_not_remove_theme',
			/* translators: %s: Theme name. */
			sprintf( __( 'Could not fully remove the theme %s.' ), $stylesheet )
		);
	}

	$theme_translations = gc_get_installed_translations( 'themes' );

	// Remove language files, silently.
	if ( ! empty( $theme_translations[ $stylesheet ] ) ) {
		$translations = $theme_translations[ $stylesheet ];

		foreach ( $translations as $translation => $data ) {
			$gc_filesystem->delete( GC_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.po' );
			$gc_filesystem->delete( GC_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.mo' );

			$json_translation_files = glob( GC_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '-*.json' );
			if ( $json_translation_files ) {
				array_map( array( $gc_filesystem, 'delete' ), $json_translation_files );
			}
		}
	}

	// Remove the theme from allowed themes on the network.
	if ( is_multisite() ) {
		GC_Theme::network_disable_theme( $stylesheet );
	}

	// Force refresh of theme update information.
	delete_site_transient( 'update_themes' );

	return true;
}