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