Initial commit: WordPress wp-content (themes, plugins, languages)

- Theme: momentry (custom theme with REST API routes)
- Plugins: code-snippets (contains all API proxies)
- Languages: zh_TW translations
- Excludes: cache, backups, uploads, logs
This commit is contained in:
OpenCode
2026-05-29 19:07:56 +08:00
commit 09ef1f000f
6521 changed files with 867163 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
<?php
namespace Code_Snippets;
/**
* Handles exporting snippets from the site to a downloadable file over HTTP.
*
* @package Code_Snippets
*/
class Export_Attachment extends Export {
/**
* Set up the current page to act like a downloadable file instead of being shown in the browser
*
* @param string $language File format. Used for file extension.
* @param string $mime_type File MIME type. Used for Content-Type header.
*/
private function do_headers( string $language, string $mime_type = 'text/plain' ) {
header( 'Content-Disposition: attachment; filename=' . sanitize_file_name( $this->build_filename( $language ) ) );
header( sprintf( 'Content-Type: %s; charset=%s', sanitize_mime_type( $mime_type ), get_bloginfo( 'charset' ) ) );
}
/**
* Export snippets in JSON format as a downloadable file.
*/
public function download_snippets_json() {
$this->do_headers( 'json', 'application/json' );
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
echo wp_json_encode(
$this->create_export_object(),
apply_filters( 'code_snippets/export/json_encode_options', 0 )
);
exit;
}
/**
* Export snippets in their code file format.
*/
public function download_snippets_code() {
$lang = $this->snippets_list[0]->lang;
$mime_types = [
'php' => 'text/php',
'css' => 'text/css',
'js' => 'text/javascript',
'json' => 'application/json',
];
$this->do_headers( $lang, $mime_types[ $lang ] ?? 'text/plain' );
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
echo $this->export_snippets_code( $this->snippets_list[0]->type );
exit;
}
}

View File

@@ -0,0 +1,194 @@
<?php
namespace Code_Snippets;
/**
* Handles exporting snippets from the site in various downloadable formats
*
* @package Code_Snippets
* @since 3.0.0
*/
class Export {
/**
* Array of snippet data fetched from the database
*
* @var Snippet[]
*/
protected array $snippets_list;
/**
* Class constructor
*
* @param array<int> $ids List of snippet IDs to export.
* @param boolean|null $network Whether to fetch snippets from local or network table.
*/
public function __construct( array $ids, ?bool $network = null ) {
$this->snippets_list = get_snippets( $ids, $network );
}
/**
* Build the export filename.
*
* @param string $format File format. Used for file extension.
*
* @return string
*/
public function build_filename( string $format ): string {
if ( 1 === count( $this->snippets_list ) ) {
// If there is only snippet to export, use its name instead of the site name.
$title = strtolower( $this->snippets_list[0]->name );
} else {
// Otherwise, use the site name as set in Settings > General.
$title = strtolower( get_bloginfo( 'name' ) );
}
$filename = "$title.code-snippets.$format";
return apply_filters( 'code_snippets/export/filename', $filename, $title, $this->snippets_list );
}
/**
* Bundle snippets together into JSON format.
*
* @return array<string, string|Snippet[]> Snippets as JSON object.
*/
public function create_export_object(): array {
$snippets = array();
foreach ( $this->snippets_list as $snippet ) {
$snippets[] = array_map(
function ( $value ) {
return is_string( $value ) ?
str_replace( "\r\n", "\n", $value ) :
$value;
},
$snippet->get_modified_fields()
);
}
return array(
'generator' => 'Code Snippets v' . code_snippets()->version,
'date_created' => gmdate( 'Y-m-d H:i' ),
'snippets' => $snippets,
);
}
/**
* Bundle a snippets into a PHP file.
*/
public function export_snippets_php(): string {
$result = "<?php\n";
foreach ( $this->snippets_list as $snippet ) {
$code = trim( $snippet->code );
if ( ( 'php' !== $snippet->type && 'html' !== $snippet->type ) || ! $code ) {
continue;
}
$result .= "\n/**\n * $snippet->display_name\n";
if ( ! empty( $snippet->desc ) ) {
// Convert description to PhpDoc.
$desc = wp_strip_all_tags( str_replace( "\n", "\n * ", $snippet->desc ) );
$result .= " *\n * $desc\n";
}
$result .= " */\n";
if ( 'content' === $snippet->scope ) {
$shortcode_tag = apply_filters( 'code_snippets_export_shortcode_tag', "code_snippets_export_$snippet->id", $snippet );
$code = sprintf(
"add_shortcode( '%s', function () {\n\tob_start();\n\t?>\n\n\t%s\n\n\t<?php\n\treturn ob_get_clean();\n} );",
$shortcode_tag,
str_replace( "\n", "\n\t", $code )
);
}
$result .= "$code\n";
}
return $result;
}
/**
* Export conditions in JSON format.
*
* @return string
*/
public function export_conditions_json(): string {
$conditions_data = [];
$fields_to_copy = [ 'name', 'desc', 'tags' ];
foreach ( $this->snippets_list as $snippet ) {
$condition_data = [];
if ( ! $snippet->code || 'cond' !== $snippet->type ) {
continue;
}
$rules = json_decode( $snippet->code, false );
if ( json_last_error() !== JSON_ERROR_NONE ) {
continue;
}
foreach ( $fields_to_copy as $field ) {
if ( ! empty( $snippet->$field ) ) {
$condition_data[ $field ] = $snippet->$field;
}
}
$condition_data['rules'] = $rules;
$conditions_data[] = $condition_data;
}
return wp_json_encode( 1 === count( $conditions_data ) ? $conditions_data[0] : $conditions_data, JSON_PRETTY_PRINT );
}
/**
* Generate a downloadable CSS or JavaScript file from a list of snippets
*
* @phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
*
* @param string|null $type Snippet type. Supports 'css' or 'js'.
*/
public function export_snippets_code( ?string $type = null ): string {
$result = '';
if ( ! $type ) {
$type = $this->snippets_list[0]->type;
}
if ( 'php' === $type || 'html' === $type ) {
return $this->export_snippets_php();
}
if ( 'cond' === $type ) {
return $this->export_conditions_json();
}
foreach ( $this->snippets_list as $snippet ) {
$snippet = new Snippet( $snippet );
if ( $snippet->type !== $type ) {
continue;
}
$result .= "\n/*\n";
if ( $snippet->name ) {
$result .= wp_strip_all_tags( $snippet->name ) . "\n\n";
}
if ( ! empty( $snippet->desc ) ) {
$result .= wp_strip_all_tags( $snippet->desc ) . "\n";
}
$result .= "*/\n\n$snippet->code\n\n";
}
return $result;
}
}

View File

@@ -0,0 +1,208 @@
<?php
namespace Code_Snippets;
use DOMDocument;
/**
* Handles importing snippets from export files into the site
*
* @package Code_Snippets
* @since 3.0.0
*
* phpcs:disable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
* phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
*/
class Import {
/**
* Path to file to import.
*
* @var string
*/
private string $file;
/**
* Whether snippets should be imported into the network-wide or site-wide table.
*
* @var bool
*/
private bool $multisite;
/**
* Action to take if duplicate snippets are detected. Can be 'skip', 'ignore', or 'replace'.
*
* @var string
*/
private string $dup_action;
/**
* Class constructor.
*
* @param string $file The path to the file to import.
* @param bool|null $network Import into network-wide table (true) or site-wide table (false).
* @param string $dup_action Action to take if duplicate snippets are detected. Can be 'skip', 'ignore', or 'replace'.
*/
public function __construct( string $file, ?bool $network = null, string $dup_action = 'ignore' ) {
$this->file = $file;
$this->multisite = DB::validate_network_param( $network );
$this->dup_action = $dup_action;
}
/**
* Imports snippets from a JSON file.
*
* @return array<integer>|bool An array of imported snippet IDs on success, false on failure
*/
public function import_json() {
if ( ! file_exists( $this->file ) || ! is_file( $this->file ) ) {
return false;
}
$raw_data = file_get_contents( $this->file );
$data = json_decode( $raw_data, true );
$snippets = array();
// Reformat the data into snippet objects.
foreach ( $data['snippets'] as $snippet_data ) {
$snippet = new Snippet();
$snippet->network = $this->multisite;
$import_fields = [
'name',
'desc',
'description',
'code',
'tags',
'scope',
'priority',
'shared_network',
'modified',
'cloud_id',
];
foreach ( $import_fields as $field ) {
if ( isset( $snippet_data[ $field ] ) ) {
$snippet->set_field( $field, $snippet_data[ $field ] );
}
}
$snippets[] = $snippet;
}
$imported = $this->save_snippets( $snippets );
do_action( 'code_snippets/import/json', $this->file, $this->multisite );
return $imported;
}
/**
* Imports snippets from an XML file
*
* @return array<integer>|bool An array of imported snippet IDs on success, false on failure
*/
public function import_xml() {
if ( ! file_exists( $this->file ) || ! is_file( $this->file ) ) {
return false;
}
$dom = new DOMDocument( '1.0', get_bloginfo( 'charset' ) );
$dom->load( $this->file );
$snippets_xml = $dom->getElementsByTagName( 'snippet' );
$fields = array( 'name', 'description', 'desc', 'code', 'tags', 'scope' );
$snippets = array();
foreach ( $snippets_xml as $snippet_xml ) {
$snippet = new Snippet();
$snippet->network = $this->multisite;
// Build a snippet object by looping through the field names.
foreach ( $fields as $field_name ) {
// Fetch the field element from the document.
$field = $snippet_xml->getElementsByTagName( $field_name )->item( 0 );
// If the field element exists, add it to the snippet object.
if ( isset( $field->nodeValue ) ) {
$snippet->set_field( $field_name, $field->nodeValue );
}
}
// Get scope from attribute.
$scope = $snippet_xml->getAttribute( 'scope' );
if ( ! empty( $scope ) ) {
$snippet->scope = $scope;
}
$snippets[] = $snippet;
}
$imported = $this->save_snippets( $snippets );
do_action( 'code_snippets/import/xml', $this->file, $this->multisite );
return $imported;
}
/**
* Fetch a list of existing snippets for checking duplicates.
*
* @return array<string, integer>
*/
private function fetch_existing_snippets(): array {
$existing_snippets = array();
if ( 'replace' === $this->dup_action || 'skip' === $this->dup_action ) {
$all_snippets = get_snippets( array(), $this->multisite );
foreach ( $all_snippets as $snippet ) {
if ( $snippet->name ) {
$existing_snippets[ $snippet->name ] = $snippet->id;
}
}
}
return $existing_snippets;
}
/**
* Save imported snippets to the database
*
* @access private
*
* @param array<Snippet> $snippets List of snippets to save.
*
* @return array<integer> IDs of imported snippets.
*/
private function save_snippets( array $snippets ): array {
$existing_snippets = $this->fetch_existing_snippets();
$imported = array();
foreach ( $snippets as $snippet ) {
// Check if the snippet already exists.
if ( 'ignore' !== $this->dup_action && isset( $existing_snippets[ $snippet->name ] ) ) {
// If so, either overwrite the existing ID, or skip this import.
if ( 'replace' === $this->dup_action ) {
$snippet->id = $existing_snippets[ $snippet->name ];
} elseif ( 'skip' === $this->dup_action ) {
continue;
}
}
// Save the snippet and increase the counter if successful.
$saved_snippet = save_snippet( $snippet );
// Get ID of the saved snippet as save_snippet() returns complete snippet object.
$snippet_id = $saved_snippet->id;
if ( $snippet_id ) {
$imported[] = $snippet_id;
}
}
return $imported;
}
}