Edit File by line
/home/zeestwma/ajeebong.../wp-conte.../plugins/code-sni.../php/export
File: class-import.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Code_Snippets;
[2] Fix | Delete
[3] Fix | Delete
use DOMDocument;
[4] Fix | Delete
[5] Fix | Delete
/**
[6] Fix | Delete
* Handles importing snippets from export files into the site
[7] Fix | Delete
*
[8] Fix | Delete
* @package Code_Snippets
[9] Fix | Delete
* @since 3.0.0
[10] Fix | Delete
*
[11] Fix | Delete
* phpcs:disable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
[12] Fix | Delete
* phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
[13] Fix | Delete
*/
[14] Fix | Delete
class Import {
[15] Fix | Delete
[16] Fix | Delete
/**
[17] Fix | Delete
* Path to file to import.
[18] Fix | Delete
*
[19] Fix | Delete
* @var string
[20] Fix | Delete
*/
[21] Fix | Delete
private string $file;
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Whether snippets should be imported into the network-wide or site-wide table.
[25] Fix | Delete
*
[26] Fix | Delete
* @var bool
[27] Fix | Delete
*/
[28] Fix | Delete
private bool $multisite;
[29] Fix | Delete
[30] Fix | Delete
/**
[31] Fix | Delete
* Action to take if duplicate snippets are detected. Can be 'skip', 'ignore', or 'replace'.
[32] Fix | Delete
*
[33] Fix | Delete
* @var string
[34] Fix | Delete
*/
[35] Fix | Delete
private string $dup_action;
[36] Fix | Delete
[37] Fix | Delete
/**
[38] Fix | Delete
* Class constructor.
[39] Fix | Delete
*
[40] Fix | Delete
* @param string $file The path to the file to import.
[41] Fix | Delete
* @param bool|null $network Import into network-wide table (true) or site-wide table (false).
[42] Fix | Delete
* @param string $dup_action Action to take if duplicate snippets are detected. Can be 'skip', 'ignore', or 'replace'.
[43] Fix | Delete
*/
[44] Fix | Delete
public function __construct( string $file, ?bool $network = null, string $dup_action = 'ignore' ) {
[45] Fix | Delete
$this->file = $file;
[46] Fix | Delete
$this->multisite = DB::validate_network_param( $network );
[47] Fix | Delete
$this->dup_action = $dup_action;
[48] Fix | Delete
}
[49] Fix | Delete
[50] Fix | Delete
/**
[51] Fix | Delete
* Imports snippets from a JSON file.
[52] Fix | Delete
*
[53] Fix | Delete
* @return array<integer>|bool An array of imported snippet IDs on success, false on failure
[54] Fix | Delete
*/
[55] Fix | Delete
public function import_json() {
[56] Fix | Delete
if ( ! file_exists( $this->file ) || ! is_file( $this->file ) ) {
[57] Fix | Delete
return false;
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
$raw_data = file_get_contents( $this->file );
[61] Fix | Delete
$data = json_decode( $raw_data, true );
[62] Fix | Delete
$snippets = array();
[63] Fix | Delete
[64] Fix | Delete
// Reformat the data into snippet objects.
[65] Fix | Delete
foreach ( $data['snippets'] as $snippet_data ) {
[66] Fix | Delete
$snippet = new Snippet();
[67] Fix | Delete
$snippet->network = $this->multisite;
[68] Fix | Delete
[69] Fix | Delete
$import_fields = [
[70] Fix | Delete
'name',
[71] Fix | Delete
'desc',
[72] Fix | Delete
'description',
[73] Fix | Delete
'code',
[74] Fix | Delete
'tags',
[75] Fix | Delete
'scope',
[76] Fix | Delete
'priority',
[77] Fix | Delete
'shared_network',
[78] Fix | Delete
'modified',
[79] Fix | Delete
'cloud_id',
[80] Fix | Delete
];
[81] Fix | Delete
[82] Fix | Delete
foreach ( $import_fields as $field ) {
[83] Fix | Delete
if ( isset( $snippet_data[ $field ] ) ) {
[84] Fix | Delete
$snippet->set_field( $field, $snippet_data[ $field ] );
[85] Fix | Delete
}
[86] Fix | Delete
}
[87] Fix | Delete
[88] Fix | Delete
$snippets[] = $snippet;
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
$imported = $this->save_snippets( $snippets );
[92] Fix | Delete
[93] Fix | Delete
do_action( 'code_snippets/import/json', $this->file, $this->multisite );
[94] Fix | Delete
return $imported;
[95] Fix | Delete
}
[96] Fix | Delete
[97] Fix | Delete
/**
[98] Fix | Delete
* Imports snippets from an XML file
[99] Fix | Delete
*
[100] Fix | Delete
* @return array<integer>|bool An array of imported snippet IDs on success, false on failure
[101] Fix | Delete
*/
[102] Fix | Delete
public function import_xml() {
[103] Fix | Delete
if ( ! file_exists( $this->file ) || ! is_file( $this->file ) ) {
[104] Fix | Delete
return false;
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
$dom = new DOMDocument( '1.0', get_bloginfo( 'charset' ) );
[108] Fix | Delete
$dom->load( $this->file );
[109] Fix | Delete
[110] Fix | Delete
$snippets_xml = $dom->getElementsByTagName( 'snippet' );
[111] Fix | Delete
$fields = array( 'name', 'description', 'desc', 'code', 'tags', 'scope' );
[112] Fix | Delete
[113] Fix | Delete
$snippets = array();
[114] Fix | Delete
[115] Fix | Delete
foreach ( $snippets_xml as $snippet_xml ) {
[116] Fix | Delete
$snippet = new Snippet();
[117] Fix | Delete
$snippet->network = $this->multisite;
[118] Fix | Delete
[119] Fix | Delete
// Build a snippet object by looping through the field names.
[120] Fix | Delete
foreach ( $fields as $field_name ) {
[121] Fix | Delete
[122] Fix | Delete
// Fetch the field element from the document.
[123] Fix | Delete
$field = $snippet_xml->getElementsByTagName( $field_name )->item( 0 );
[124] Fix | Delete
[125] Fix | Delete
// If the field element exists, add it to the snippet object.
[126] Fix | Delete
if ( isset( $field->nodeValue ) ) {
[127] Fix | Delete
$snippet->set_field( $field_name, $field->nodeValue );
[128] Fix | Delete
}
[129] Fix | Delete
}
[130] Fix | Delete
[131] Fix | Delete
// Get scope from attribute.
[132] Fix | Delete
$scope = $snippet_xml->getAttribute( 'scope' );
[133] Fix | Delete
if ( ! empty( $scope ) ) {
[134] Fix | Delete
$snippet->scope = $scope;
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
$snippets[] = $snippet;
[138] Fix | Delete
}
[139] Fix | Delete
[140] Fix | Delete
$imported = $this->save_snippets( $snippets );
[141] Fix | Delete
do_action( 'code_snippets/import/xml', $this->file, $this->multisite );
[142] Fix | Delete
[143] Fix | Delete
return $imported;
[144] Fix | Delete
}
[145] Fix | Delete
[146] Fix | Delete
/**
[147] Fix | Delete
* Fetch a list of existing snippets for checking duplicates.
[148] Fix | Delete
*
[149] Fix | Delete
* @return array<string, integer>
[150] Fix | Delete
*/
[151] Fix | Delete
private function fetch_existing_snippets(): array {
[152] Fix | Delete
$existing_snippets = array();
[153] Fix | Delete
[154] Fix | Delete
if ( 'replace' === $this->dup_action || 'skip' === $this->dup_action ) {
[155] Fix | Delete
$all_snippets = get_snippets( array(), $this->multisite );
[156] Fix | Delete
[157] Fix | Delete
foreach ( $all_snippets as $snippet ) {
[158] Fix | Delete
if ( $snippet->name ) {
[159] Fix | Delete
$existing_snippets[ $snippet->name ] = $snippet->id;
[160] Fix | Delete
}
[161] Fix | Delete
}
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
return $existing_snippets;
[165] Fix | Delete
}
[166] Fix | Delete
[167] Fix | Delete
/**
[168] Fix | Delete
* Save imported snippets to the database
[169] Fix | Delete
*
[170] Fix | Delete
* @access private
[171] Fix | Delete
*
[172] Fix | Delete
* @param array<Snippet> $snippets List of snippets to save.
[173] Fix | Delete
*
[174] Fix | Delete
* @return array<integer> IDs of imported snippets.
[175] Fix | Delete
*/
[176] Fix | Delete
private function save_snippets( array $snippets ): array {
[177] Fix | Delete
$existing_snippets = $this->fetch_existing_snippets();
[178] Fix | Delete
$imported = array();
[179] Fix | Delete
[180] Fix | Delete
foreach ( $snippets as $snippet ) {
[181] Fix | Delete
[182] Fix | Delete
// Check if the snippet already exists.
[183] Fix | Delete
if ( 'ignore' !== $this->dup_action && isset( $existing_snippets[ $snippet->name ] ) ) {
[184] Fix | Delete
[185] Fix | Delete
// If so, either overwrite the existing ID, or skip this import.
[186] Fix | Delete
if ( 'replace' === $this->dup_action ) {
[187] Fix | Delete
$snippet->id = $existing_snippets[ $snippet->name ];
[188] Fix | Delete
} elseif ( 'skip' === $this->dup_action ) {
[189] Fix | Delete
continue;
[190] Fix | Delete
}
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
// Save the snippet and increase the counter if successful.
[194] Fix | Delete
$saved_snippet = save_snippet( $snippet );
[195] Fix | Delete
[196] Fix | Delete
// Get ID of the saved snippet as save_snippet() returns complete snippet object.
[197] Fix | Delete
$snippet_id = $saved_snippet->id;
[198] Fix | Delete
[199] Fix | Delete
if ( $snippet_id ) {
[200] Fix | Delete
$imported[] = $snippet_id;
[201] Fix | Delete
}
[202] Fix | Delete
}
[203] Fix | Delete
[204] Fix | Delete
return $imported;
[205] Fix | Delete
}
[206] Fix | Delete
}
[207] Fix | Delete
[208] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function