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