| 1 |
<?php |
| 2 |
namespace Starter_Sites; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
class Activate { |
| 7 |
|
| 8 |
/** |
| 9 |
* Returns plugin page base link according to user setting. |
| 10 |
* |
| 11 |
* @return string |
| 12 |
*/ |
| 13 |
public function base_link() { |
| 14 |
$settings = get_option( 'starter_sites_settings' ); |
| 15 |
$base_link = 'admin.php'; |
| 16 |
if ( isset($settings['is_minimal']) && 'yes' === $settings['is_minimal'] ) { |
| 17 |
$base_link = 'options-general.php'; |
| 18 |
} elseif ( isset($settings['menu_location']) ) { |
| 19 |
if ( 'appearance' === $settings['menu_location'] ) { |
| 20 |
$base_link = 'themes.php'; |
| 21 |
} elseif ( 'tools' === $settings['menu_location'] ) { |
| 22 |
$base_link = 'tools.php'; |
| 23 |
} |
| 24 |
} |
| 25 |
return $base_link; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Returns appropriate starter site content file. |
| 30 |
* |
| 31 |
* @param string $site_slug |
| 32 |
* @return string path to saved file, false otherwise. |
| 33 |
*/ |
| 34 |
public function get_import_file( $site_slug ) { |
| 35 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 36 |
$file_url = STARTER_SITES_HOME_URL . 'sites/' . $site_slug . '/content.xml'; |
| 37 |
$temp_file = download_url( $file_url, 60 ); |
| 38 |
if ( is_wp_error($temp_file) ) { |
| 39 |
return false; |
| 40 |
} |
| 41 |
$file = array( |
| 42 |
'name' => $site_slug . '-content.xml', |
| 43 |
'type' => 'text/xml', |
| 44 |
'tmp_name' => $temp_file, |
| 45 |
'error' => 0, |
| 46 |
'size' => filesize( $temp_file ), |
| 47 |
); |
| 48 |
$overrides = array( |
| 49 |
'test_form' => false, |
| 50 |
'test_type' => false |
| 51 |
); |
| 52 |
$file_uploaded = wp_handle_sideload( $file, $overrides ); |
| 53 |
if ( isset($file_uploaded['file']) ) { |
| 54 |
return $file_uploaded['file']; |
| 55 |
} else { |
| 56 |
return false; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Processes extensions (theme and plugins). |
| 62 |
* |
| 63 |
* @param array $site_values demo site parameters. |
| 64 |
* Redirects back to plugin page if successful, outputs error message otherwise. |
| 65 |
*/ |
| 66 |
public function site_extensions( $site_values ) { |
| 67 |
$site_slug = sanitize_key( $site_values['starter_site_activate'] ); |
| 68 |
$site_nonce = $site_values['_wpnonce']; |
| 69 |
if ( isset( $site_values['starter_sites_file'] ) ) { |
| 70 |
$import_file = $site_values['starter_sites_file']; |
| 71 |
} else { |
| 72 |
$import_file = $this->get_import_file( $site_slug ); |
| 73 |
} |
| 74 |
if ( file_exists( $import_file ) ) { |
| 75 |
// Process theme |
| 76 |
$process_theme_log = $this->process_theme( $site_values ); |
| 77 |
if ( isset( $process_theme_log['is_error'] ) && $process_theme_log['is_error'] == true ) { |
| 78 |
$process_theme_log['time_end'] = current_time( 'timestamp' ); |
| 79 |
wp_insert_post( |
| 80 |
array( |
| 81 |
'post_content' => maybe_serialize( wp_unslash($process_theme_log) ), |
| 82 |
'post_title' => __( 'Starter Sites Error', 'starter-sites' ), |
| 83 |
'post_status' => 'private', |
| 84 |
'comment_status' => 'closed', |
| 85 |
'ping_status' => 'closed', |
| 86 |
'post_name' => 'starter-sites-error', |
| 87 |
'post_type' => 'starter_sites_error' |
| 88 |
) |
| 89 |
); |
| 90 |
if ( isset( $process_theme_log['error_code'] ) ) { |
| 91 |
?> |
| 92 |
<div class="starter-sites-error"><p><?php echo esc_html( $this->error_codes( $process_theme_log['error_code'] ) );?></p></div> |
| 93 |
<?php |
| 94 |
} else { |
| 95 |
?> |
| 96 |
<div class="starter-sites-error"><p><?php echo esc_html__( 'There was an unknown error with the required theme.', 'starter-sites' );?></p></div> |
| 97 |
<?php |
| 98 |
} |
| 99 |
} else { |
| 100 |
// Process plugin(s) |
| 101 |
$process_plugins_log = $this->process_plugins( $process_theme_log, $site_values ); |
| 102 |
$process_plugins_log['time_end'] = current_time( 'timestamp' ); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
/*if ( isset( $process_plugins_log['is_error'] ) && $process_plugins_log['is_error'] == true ) { |
| 107 |
wp_insert_post( |
| 108 |
array( |
| 109 |
'post_content' => maybe_serialize( wp_unslash($process_plugins_log) ), |
| 110 |
'post_title' => __( 'Starter Sites Error', 'starter-sites' ), |
| 111 |
'post_status' => 'private', |
| 112 |
'comment_status' => 'closed', |
| 113 |
'ping_status' => 'closed', |
| 114 |
'post_name' => 'starter-sites-error', |
| 115 |
'post_type' => 'starter_sites_error' |
| 116 |
) |
| 117 |
); |
| 118 |
if ( isset( $process_plugins_log['error_code'] ) ) { |
| 119 |
?> |
| 120 |
<div class="starter-sites-error"><p><?php echo esc_html( $this->error_codes( $process_plugins_log['error_code'] ) );?></p></div> |
| 121 |
<?php |
| 122 |
} else { |
| 123 |
?> |
| 124 |
<div class="starter-sites-error"><p><?php echo esc_html__( 'There was an unknown error with a required plugin.', 'starter-sites' );?></p></div> |
| 125 |
<?php |
| 126 |
} |
| 127 |
} else {*/ |
| 128 |
$extensions_log_id = wp_insert_post( |
| 129 |
array( |
| 130 |
'post_content' => maybe_serialize( wp_unslash($process_plugins_log) ), // wp_insert_post expects unslashed content, prevents unserializing offset errors |
| 131 |
'post_title' => __( 'Starter Sites Log', 'starter-sites' ), |
| 132 |
'post_status' => 'private', |
| 133 |
'comment_status' => 'closed', |
| 134 |
'ping_status' => 'closed', |
| 135 |
'post_name' => 'starter-sites-log', |
| 136 |
'post_type' => 'starter_sites_log', |
| 137 |
'meta_input' => array( 'starter_sites_file' => wp_normalize_path( $import_file ) ) |
| 138 |
) |
| 139 |
); |
| 140 |
// as we may need functionality from installed plugin(s) we will redirect back to plugin page, then process content |
| 141 |
wp_safe_redirect( add_query_arg( [ 'page' => 'starter-sites', 'activate' => $site_slug, 'process' => 'content', 'id' => $extensions_log_id ], admin_url( $this->base_link() ) ) ); |
| 142 |
exit; |
| 143 |
//} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
} |
| 148 |
} else { |
| 149 |
?> |
| 150 |
<div class="starter-sites-error"><p><?php echo esc_html( $this->error_codes( 1 ) );?></p></div> |
| 151 |
<?php |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Processes demo site content and outputs result. |
| 157 |
* |
| 158 |
* @param array $process_plugins_log_id WP post id. |
| 159 |
*/ |
| 160 |
public function site_content( $process_plugins_log_id ) { |
| 161 |
$log_post = get_post($process_plugins_log_id); |
| 162 |
if ( $log_post && $log_post->post_type === 'starter_sites_log' ) { |
| 163 |
$process_plugins_log = maybe_unserialize( wp_unslash( $log_post->post_content ) ); |
| 164 |
$site_slug = $process_plugins_log['site']['demo_slug']; |
| 165 |
$import_file = get_post_meta( $process_plugins_log_id, 'starter_sites_file', true ); |
| 166 |
if ( file_exists( $import_file ) ) { |
| 167 |
// Process content |
| 168 |
$process_content_log = $this->process_content( $process_plugins_log, $import_file ); |
| 169 |
// Mapping |
| 170 |
(new Mapping)->content( $process_content_log ); |
| 171 |
$process_content_log['time_end'] = current_time( 'timestamp' ); |
| 172 |
$log_id = wp_update_post( |
| 173 |
array( |
| 174 |
'ID' => $process_plugins_log_id, |
| 175 |
'post_content' => maybe_serialize( wp_unslash($process_content_log) ), // wp_update_post expects unslashed content, prevents unserializing offset errors |
| 176 |
) |
| 177 |
); |
| 178 |
?> |
| 179 |
<p><?php echo sprintf( |
| 180 |
/* translators: %s = title of the activated starter site */ |
| 181 |
esc_html__( 'Congratulations. You have successfully activated the %s starter site!', 'starter-sites' ), |
| 182 |
'<span class="success-site-title">' . esc_html( $process_content_log['site']['demo_title'] ) . '</span>' |
| 183 |
);?></p> |
| 184 |
<ul class="text-list"> |
| 185 |
<li><a class="text-link" href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php esc_html_e( 'View your new site', 'starter-sites' );?></a></li> |
| 186 |
<li><a class="text-link" href="<?php echo esc_url( admin_url( 'site-editor.php' ) ); ?>"><?php esc_html_e( 'Edit your new site', 'starter-sites' );?></a></li> |
| 187 |
<?php if ( $log_id ) { |
| 188 |
?> |
| 189 |
<li><a class="text-link" href="<?php echo esc_url( admin_url( $this->base_link() . '?page=starter-sites&tab=logs&log_id=' . $log_id ) ); ?>"><?php esc_html_e( 'View the activation log', 'starter-sites' );?></a></li> |
| 190 |
<?php |
| 191 |
} |
| 192 |
?> |
| 193 |
</ul> |
| 194 |
<?php |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
if ( isset($process_content_log['plugins']) && !empty($process_content_log['plugins']) ) { |
| 199 |
foreach ( $process_content_log['plugins'] as $plugin_log ) { |
| 200 |
if ( isset($plugin_log['error_msg']) && $plugin_log['error_msg'] !== '' ) { |
| 201 |
?> |
| 202 |
<div class="starter-sites-error"> |
| 203 |
<p> |
| 204 |
<?php echo sprintf( |
| 205 |
/* translators: %1$s = title of plugin, %2$s = error message */ |
| 206 |
esc_html__( 'There was an error with the %1$s plugin: %2$s', 'starter-sites' ), |
| 207 |
'<strong>' . $plugin_log['title'] . '</strong>', |
| 208 |
$plugin_log['result'] |
| 209 |
);?> |
| 210 |
<br /> |
| 211 |
<?php echo esc_html( $plugin_log['error_msg'] );?> |
| 212 |
</p> |
| 213 |
</div> |
| 214 |
<?php |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
} else { |
| 222 |
?> |
| 223 |
<div class="starter-sites-error"><p><?php echo esc_html( $this->error_codes( 1 ) );?></p></div> |
| 224 |
<?php |
| 225 |
} |
| 226 |
} else { |
| 227 |
?> |
| 228 |
<div class="starter-sites-error"><p><?php echo esc_html( $this->error_codes( 4 ) );?></p></div> |
| 229 |
<?php |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Processes theme. |
| 235 |
* |
| 236 |
* @param array $site_values demo site parameters. |
| 237 |
* @return array $log activation log. |
| 238 |
*/ |
| 239 |
public function process_theme( $site_values ) { |
| 240 |
$site_slug = $site_values['starter_site_activate']; |
| 241 |
$site_title = $site_slug; |
| 242 |
$demo_list = starter_sites_demo_list(); |
| 243 |
$theme_list = starter_sites_theme_list(); |
| 244 |
if ( isset($demo_list[$site_slug]['title']) && $demo_list[$site_slug]['title'] !== '' ) { |
| 245 |
$site_title = $demo_list[$site_slug]['title']; |
| 246 |
} |
| 247 |
$log = array( |
| 248 |
'user_id' => get_current_user_id(), |
| 249 |
'time_start' => current_time( 'timestamp' ), |
| 250 |
'site' => array( |
| 251 |
'demo_slug' => $site_slug, |
| 252 |
), |
| 253 |
'theme' => array(), |
| 254 |
'theme_parent' => '', |
| 255 |
'plugins' => array(), |
| 256 |
'terms' => array(), |
| 257 |
'content' => array(), |
| 258 |
'design' => array(), |
| 259 |
'other' => array(), |
| 260 |
); |
| 261 |
// if child theme is selected |
| 262 |
if ( isset($site_values['theme']) && isset($site_values['child_theme_slug']) && isset($site_values['child_theme_name']) && $site_values['theme'] === $site_values['child_theme_slug'] ) { |
| 263 |
$log['theme_parent'] = $site_values['parent_theme_slug']; |
| 264 |
$theme = $site_values['child_theme_slug']; |
| 265 |
$theme_title = $site_values['child_theme_name']; |
| 266 |
} else { |
| 267 |
if ( isset($demo_list[$site_slug]['theme']) && $demo_list[$site_slug]['theme'] !== '' ) { |
| 268 |
$theme = $demo_list[$site_slug]['theme']; |
| 269 |
} else { |
| 270 |
$theme = STARTER_SITES_THEME_DEFAULT; |
| 271 |
} |
| 272 |
if ( isset($theme_list[$theme]['title']) && $theme_list[$theme]['title'] !== '' ) { |
| 273 |
$theme_title = $theme_list[$theme]['title']; |
| 274 |
} else { |
| 275 |
$theme_title = $theme; |
| 276 |
} |
| 277 |
$log['theme_parent'] = $theme; |
| 278 |
} |
| 279 |
$active_theme = wp_get_theme(); |
| 280 |
$active_theme_slug = $active_theme->get_stylesheet(); |
| 281 |
// check if required theme is already active |
| 282 |
if ( $theme === $active_theme_slug ) { |
| 283 |
$log['theme'] = array( |
| 284 |
'slug' => $theme, |
| 285 |
'title' => wp_slash($theme_title), |
| 286 |
'pre_status' => 'active', |
| 287 |
'result' => 'none' |
| 288 |
); |
| 289 |
} else { |
| 290 |
if ( wp_get_theme( $theme )->exists() ) { |
| 291 |
$theme_activate = switch_theme( $theme ); |
| 292 |
$log['theme'] = array( |
| 293 |
'slug' => $theme, |
| 294 |
'title' => wp_slash($theme_title), |
| 295 |
'pre_status' => 'inactive', |
| 296 |
'result' => 'activated' |
| 297 |
); |
| 298 |
$this->do_extension_options( $theme ); |
| 299 |
} else { |
| 300 |
// try to install required theme |
| 301 |
$theme_install = $this->install_theme( $theme ); |
| 302 |
if ( $theme_install['success'] ) { |
| 303 |
$theme_activate = switch_theme( $theme ); |
| 304 |
$log['theme'] = array( |
| 305 |
'slug' => $theme, |
| 306 |
'title' => wp_slash($theme_title), |
| 307 |
'pre_status' => 'not installed', |
| 308 |
'result' => 'installed and activated' |
| 309 |
); |
| 310 |
$this->do_extension_options( $theme ); |
| 311 |
} else { |
| 312 |
$log['is_error'] = true; |
| 313 |
$log['error_code'] = 2; |
| 314 |
$log['theme'] = array( |
| 315 |
'slug' => $theme, |
| 316 |
'title' => wp_slash($theme_title), |
| 317 |
'pre_status' => 'not installed', |
| 318 |
'result' => 'not installed' |
| 319 |
); |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
return $log; |
| 324 |
} |
| 325 |
|
| 326 |
/* |
| 327 |
* Install theme from wp.org |
| 328 |
*/ |
| 329 |
public function install_theme( $slug ) { |
| 330 |
$status = [ |
| 331 |
'success' => false, |
| 332 |
]; |
| 333 |
if ( ! current_user_can( 'install_themes' ) ) { |
| 334 |
if ( is_multisite() ) { |
| 335 |
$status['errorMessage'] = __( 'Please ask your network administrator to enable theme installation capabilities for your user account.', 'starter-sites' ); |
| 336 |
} else { |
| 337 |
$status['errorMessage'] = __( 'Please ask your site administrator to enable theme installation capabilities for your user account.', 'starter-sites' ); |
| 338 |
} |
| 339 |
return $status; |
| 340 |
} |
| 341 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 342 |
include_once ABSPATH . 'wp-admin/includes/theme.php'; |
| 343 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 344 |
include_once ABSPATH . 'wp-admin/includes/theme-install.php'; |
| 345 |
$api = themes_api( |
| 346 |
'theme_information', |
| 347 |
[ |
| 348 |
'slug' => $slug, |
| 349 |
'fields' => [ 'sections' => false ], |
| 350 |
] |
| 351 |
); |
| 352 |
if ( is_wp_error( $api ) ) { |
| 353 |
$status['errorMessage'] = $api->get_error_message(); |
| 354 |
return $status; |
| 355 |
} |
| 356 |
$skin = new \WP_Ajax_Upgrader_Skin(); |
| 357 |
$upgrader = new \Theme_Upgrader( $skin ); |
| 358 |
$result = $upgrader->install( $api->download_link ); |
| 359 |
if ( is_wp_error( $result ) ) { |
| 360 |
$status['errorCode'] = $result->get_error_code(); |
| 361 |
$status['errorMessage'] = $result->get_error_message(); |
| 362 |
return $status; |
| 363 |
} elseif ( is_wp_error( $skin->result ) ) { |
| 364 |
$status['errorCode'] = $skin->result->get_error_code(); |
| 365 |
$status['errorMessage'] = $skin->result->get_error_message(); |
| 366 |
return $status; |
| 367 |
} elseif ( $skin->get_errors()->has_errors() ) { |
| 368 |
$status['errorMessage'] = $skin->get_error_messages(); |
| 369 |
return $status; |
| 370 |
} elseif ( is_null( $result ) ) { |
| 371 |
global $wp_filesystem; |
| 372 |
$status['errorCode'] = 'unable_to_connect_to_filesystem'; |
| 373 |
$status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.', 'starter-sites' ); |
| 374 |
// Pass through the error from WP_Filesystem if one was raised. |
| 375 |
if ( $wp_filesystem instanceof \WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) { |
| 376 |
$status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); |
| 377 |
} |
| 378 |
return $status; |
| 379 |
} |
| 380 |
$status['themeName'] = wp_get_theme( $slug )->get( 'Name' ); |
| 381 |
$status['success'] = true; |
| 382 |
return $status; |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* Processes plugins. |
| 387 |
* |
| 388 |
* @param array $log activation log. |
| 389 |
* @param array $site_values demo site parameters. |
| 390 |
* @return array $log appended activation log. |
| 391 |
*/ |
| 392 |
public function process_plugins( $log, $site_values ) { |
| 393 |
$site_slug = $site_values['starter_site_activate']; |
| 394 |
$demo_list = starter_sites_demo_list(); |
| 395 |
$plugin_list = starter_sites_plugin_list(); |
| 396 |
if ( isset($demo_list[$site_slug]['plugins']) && $demo_list[$site_slug]['plugins'] !== '' ) { |
| 397 |
$plugins = $demo_list[$site_slug]['plugins']; |
| 398 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 399 |
foreach ( $plugins as $plugin ) { |
| 400 |
if ( isset($plugin_list[$plugin]['title']) && $plugin_list[$plugin]['title'] !== '' ) { |
| 401 |
$plugin_title = $plugin_list[$plugin]['title']; |
| 402 |
} else { |
| 403 |
$plugin_title = $plugin; |
| 404 |
} |
| 405 |
if ( isset($plugin_list[$plugin]['file']) && $plugin_list[$plugin]['file'] !== '' ) { |
| 406 |
$plugin_file = $plugin_list[$plugin]['file']; |
| 407 |
if ( is_plugin_active( $plugin_file ) ) { |
| 408 |
$log['plugins'][$plugin] = array( |
| 409 |
'title' => wp_slash($plugin_title), |
| 410 |
'pre_status' => 'active', |
| 411 |
'result' => 'none' |
| 412 |
); |
| 413 |
} else { |
| 414 |
// try to install plugin (also checks if installed but not active) |
| 415 |
$plugin_install = $this->install_plugin( $plugin_file ); |
| 416 |
if ( $plugin_install['success'] ) { |
| 417 |
if ( $plugin_install['pre_status'] === 'installed' ) { |
| 418 |
$result = 'activated'; |
| 419 |
} else { |
| 420 |
$result = 'installed and activated'; |
| 421 |
} |
| 422 |
$log['plugins'][$plugin] = array( |
| 423 |
'title' => wp_slash($plugin_title), |
| 424 |
'pre_status' => $plugin_install['pre_status'], |
| 425 |
'result' => $result |
| 426 |
); |
| 427 |
$this->do_extension_options( $plugin ); |
| 428 |
} else { |
| 429 |
if ( isset($plugin_install['errorMessage']) ) { |
| 430 |
$error_msg = $plugin_install['errorMessage']; |
| 431 |
} else { |
| 432 |
$error_msg = ''; |
| 433 |
} |
| 434 |
$log['is_error'] = true; |
| 435 |
$log['error_code'] = 3; |
| 436 |
$log['plugins'][$plugin] = array( |
| 437 |
'title' => wp_slash($plugin_title), |
| 438 |
'pre_status' => 'not installed', |
| 439 |
'result' => 'not installed', |
| 440 |
'error_msg' => $error_msg |
| 441 |
); |
| 442 |
} |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
return $log; |
| 448 |
} |
| 449 |
|
| 450 |
public function do_extension_options( $name ) { |
| 451 |
if ( 'eternal' === $name ) { |
| 452 |
update_user_meta( get_current_user_id(), 'eternal_admin_notice_dismiss', 1 ); |
| 453 |
} |
| 454 |
if ( 'wp-map-block' === $name ) { |
| 455 |
update_option( 'wpmapblock_ablocks_install_notice_hidden', true, false ); |
| 456 |
} |
| 457 |
if ( 'gutena-forms' === $name ) { |
| 458 |
update_option( 'gutena_forms_dismiss_notices', array( 'gutena-forms-view-dashboard-notice' ) ); |
| 459 |
} |
| 460 |
return; |
| 461 |
} |
| 462 |
|
| 463 |
/* |
| 464 |
* Activate a single plugin if user has required permission. |
| 465 |
*/ |
| 466 |
public function activate_plugin( $file ) { |
| 467 |
if ( current_user_can( 'activate_plugin', $file ) && is_plugin_inactive( $file ) ) { |
| 468 |
$result = activate_plugin( $file, false, false ); |
| 469 |
if ( is_wp_error( $result ) ) { |
| 470 |
return $result; |
| 471 |
} else { |
| 472 |
return true; |
| 473 |
} |
| 474 |
} |
| 475 |
return false; |
| 476 |
} |
| 477 |
|
| 478 |
/* |
| 479 |
* Install plugin from wp.org |
| 480 |
*/ |
| 481 |
public function install_plugin( $plugin_file ) { |
| 482 |
$status = array(); |
| 483 |
$plugin_slug = dirname( $plugin_file ); |
| 484 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 485 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 486 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 487 |
include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 488 |
$all_plugins = get_plugins(); |
| 489 |
$is_plugin_already_installed = false; |
| 490 |
foreach ( array_keys( $all_plugins ) as $plugin ) { |
| 491 |
if ( strpos( $plugin, $plugin_file ) !== false ) { |
| 492 |
$is_plugin_already_installed = true; |
| 493 |
} |
| 494 |
} |
| 495 |
if ( $is_plugin_already_installed ) { |
| 496 |
$activate_status = $this->activate_plugin( $plugin_file ); |
| 497 |
$status['pre_status'] = 'installed'; |
| 498 |
} else { |
| 499 |
$status['success'] = false; |
| 500 |
$api = plugins_api( |
| 501 |
'plugin_information', |
| 502 |
array( |
| 503 |
'slug' => sanitize_key( wp_unslash( $plugin_slug ) ), |
| 504 |
'fields' => array( |
| 505 |
'sections' => false, |
| 506 |
'requires' => false, |
| 507 |
'requires_php' => false, |
| 508 |
), |
| 509 |
) |
| 510 |
); |
| 511 |
if ( is_wp_error( $api ) ) { |
| 512 |
$status['errorMessage'] = $api->get_error_message(); |
| 513 |
return $status; |
| 514 |
} |
| 515 |
$status['pluginName'] = $api->name; |
| 516 |
$skin = new \WP_Ajax_Upgrader_Skin(); |
| 517 |
$upgrader = new \Plugin_Upgrader( $skin ); |
| 518 |
$result = $upgrader->install( $api->download_link ); |
| 519 |
if ( is_wp_error( $result ) ) { |
| 520 |
$status['errorCode'] = $result->get_error_code(); |
| 521 |
$status['errorMessage'] = $result->get_error_message(); |
| 522 |
return $status; |
| 523 |
} elseif ( is_wp_error( $skin->result ) ) { |
| 524 |
$status['errorCode'] = $skin->result->get_error_code(); |
| 525 |
$status['errorMessage'] = $skin->result->get_error_message(); |
| 526 |
return $status; |
| 527 |
} elseif ( $skin->get_errors()->has_errors() ) { |
| 528 |
$status['errorMessage'] = $skin->get_error_messages(); |
| 529 |
return $status; |
| 530 |
} elseif ( is_null( $result ) ) { |
| 531 |
global $wp_filesystem; |
| 532 |
$status['errorCode'] = 'unable_to_connect_to_filesystem'; |
| 533 |
$status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.', 'starter-sites' ); |
| 534 |
// Pass through the error from WP_Filesystem if one was raised. |
| 535 |
if ( $wp_filesystem instanceof \WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) { |
| 536 |
$status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); |
| 537 |
} |
| 538 |
return $status; |
| 539 |
} |
| 540 |
$install_status = install_plugin_install_status( $api ); |
| 541 |
$activate_status = $this->activate_plugin( $install_status['file'] ); |
| 542 |
$status['pre_status'] = 'not installed'; |
| 543 |
} |
| 544 |
if ( $activate_status && ! is_wp_error( $activate_status ) ) { |
| 545 |
$status['success'] = true; |
| 546 |
} |
| 547 |
return $status; |
| 548 |
} |
| 549 |
|
| 550 |
/** |
| 551 |
* Processes content from xml file. |
| 552 |
* |
| 553 |
* @param array $log activation log. |
| 554 |
* @param array $file xml file. |
| 555 |
* @return array $log appended activation log. |
| 556 |
*/ |
| 557 |
public function process_content( $log, $file ) { |
| 558 |
set_time_limit( 0 ); |
| 559 |
$site_slug = $log['site']['demo_slug']; |
| 560 |
$theme = $log['theme']['slug']; |
| 561 |
$theme_parent = $log['theme_parent']; |
| 562 |
if ( $theme === $theme_parent ) { |
| 563 |
$is_child_theme = false; |
| 564 |
} else { |
| 565 |
$is_child_theme = true; |
| 566 |
} |
| 567 |
$orig_site_title = get_bloginfo('name'); |
| 568 |
$orig_site_tagline = get_bloginfo('description'); |
| 569 |
$xml = simplexml_load_file( $file ); |
| 570 |
$namespaces = $xml->getDocNamespaces(); |
| 571 |
if ( ! isset( $namespaces['wp'] ) ) { |
| 572 |
$namespaces['wp'] = 'http://wordpress.org/export/1.2/'; |
| 573 |
} |
| 574 |
if ( ! isset( $namespaces['dc'] ) ) { |
| 575 |
$namespaces['dc'] = 'http://purl.org/dc/elements/1.1/'; |
| 576 |
} |
| 577 |
if ( ! isset( $namespaces['content'] ) ) { |
| 578 |
$namespaces['content'] = 'http://purl.org/rss/1.0/modules/content/'; |
| 579 |
} |
| 580 |
if ( ! isset( $namespaces['excerpt'] ) ) { |
| 581 |
$namespaces['excerpt'] = 'http://wordpress.org/export/1.2/excerpt/'; |
| 582 |
} |
| 583 |
if ( ! isset( $namespaces['wfw'] ) ) { |
| 584 |
$namespaces['wfw'] = 'http://wellformedweb.org/CommentAPI/'; |
| 585 |
} |
| 586 |
if ( ! isset( $namespaces['wc'] ) ) { |
| 587 |
$namespaces['wc'] = 'http://example.org/export/woocommerce/'; |
| 588 |
} |
| 589 |
$channel_namespace = $xml->channel->children( $namespaces['wp'] ); |
| 590 |
// Site Title |
| 591 |
$site_title = (string) $xml->channel->title; |
| 592 |
$log['site']['demo_title'] = wp_slash($site_title); |
| 593 |
$log['site']['orig_title'] = wp_slash( get_bloginfo('name') ); |
| 594 |
if ( $site_title === $orig_site_title ) { |
| 595 |
$log['site']['title_result'] = 'none'; |
| 596 |
} else { |
| 597 |
update_option( 'blogname', wp_unslash( $site_title ) ); |
| 598 |
$log['site']['title_result'] = 'updated'; |
| 599 |
} |
| 600 |
// Site Link |
| 601 |
$site_link = (string) $xml->channel->link; |
| 602 |
$log['site']['demo_url'] = wp_slash( trailingslashit( $site_link ) ); |
| 603 |
$log['site']['new_url'] = wp_slash( trailingslashit( get_bloginfo('url') ) ); |
| 604 |
// Site Description |
| 605 |
$site_description = (string) $xml->channel->description; |
| 606 |
$log['site']['demo_tagline'] = wp_slash($site_description); |
| 607 |
$log['site']['orig_tagline'] = wp_slash( get_bloginfo('description') ); |
| 608 |
if ( $site_description === $orig_site_tagline ) { |
| 609 |
$log['site']['tagline_result'] = 'none'; |
| 610 |
} else { |
| 611 |
update_option( 'blogdescription', wp_unslash( $site_description ) ); |
| 612 |
$log['site']['tagline_result'] = 'updated'; |
| 613 |
} |
| 614 |
// Site Options |
| 615 |
$site_options = $channel_namespace->options; |
| 616 |
// Block Visibility settings |
| 617 |
$block_visibility_settings = maybe_unserialize( $this->sanitize_data( $site_options->block_visibility_settings, 'block_visibility_settings' ) ); |
| 618 |
if ( is_array($block_visibility_settings) && !empty($block_visibility_settings) ) { |
| 619 |
update_option( 'block_visibility_settings', $block_visibility_settings ); |
| 620 |
} |
| 621 |
$log['map_options'] = array( |
| 622 |
'site_logo' => $this->sanitize_data( $site_options->site_logo, 'site_logo' ), |
| 623 |
'site_icon' => $this->sanitize_data( $site_options->site_icon, 'site_icon' ), |
| 624 |
'show_on_front' => $this->sanitize_data( $site_options->show_on_front, 'show_on_front' ), |
| 625 |
'page_on_front' => $this->sanitize_data( $site_options->page_on_front, 'page_on_front' ), |
| 626 |
'page_for_posts' => $this->sanitize_data( $site_options->page_for_posts, 'page_for_posts' ) |
| 627 |
); |
| 628 |
// WooCommerce product attributes |
| 629 |
if ( class_exists( 'WooCommerce' ) ) { |
| 630 |
foreach ( $channel_namespace->wc_attribute as $wc_attribute ) { |
| 631 |
$wc_attribute_namespace = $wc_attribute->children( $namespaces['wp'] ); |
| 632 |
$wc_attribute_id = $this->sanitize_data( $wc_attribute_namespace->wc_attribute_id, 'wc_attribute_id' ); |
| 633 |
$wc_attribute_name = $this->sanitize_data( $wc_attribute_namespace->wc_attribute_name, 'wc_attribute_name' ); |
| 634 |
$wc_attribute_label = $this->sanitize_data( $wc_attribute_namespace->wc_attribute_label, 'wc_attribute_label' ); |
| 635 |
$wc_attribute_type = $this->sanitize_data( $wc_attribute_namespace->wc_attribute_type, 'wc_attribute_type' ); |
| 636 |
$wc_attribute_orderby = $this->sanitize_data( $wc_attribute_namespace->wc_attribute_orderby, 'wc_attribute_orderby' ); |
| 637 |
$wc_attribute_public = $this->sanitize_data( $wc_attribute_namespace->wc_attribute_public, 'wc_attribute_public' ); |
| 638 |
$exists_wc_attribute_id = wc_attribute_taxonomy_id_by_name( $wc_attribute_name ); |
| 639 |
if ( $exists_wc_attribute_id ) { |
| 640 |
$log['wc_taxonomies'][$wc_attribute_id] = array( |
| 641 |
'title' => wp_slash($wc_attribute_label), |
| 642 |
'slug' => $wc_attribute_name, |
| 643 |
'new_id' => $exists_wc_attribute_id, |
| 644 |
'pre_status' => 'exists', |
| 645 |
'result' => 'none' |
| 646 |
); |
| 647 |
} else { |
| 648 |
$args = array( |
| 649 |
'name' => $wc_attribute_label, |
| 650 |
'slug' => $wc_attribute_name, |
| 651 |
'type' => $wc_attribute_type, |
| 652 |
'order_by' => $wc_attribute_orderby, |
| 653 |
'has_archives' => (int) $wc_attribute_public |
| 654 |
); |
| 655 |
$new_wc_attribute_id = wc_create_attribute( $args ); |
| 656 |
if ( !is_wp_error($new_wc_attribute_id) ) { |
| 657 |
$taxonomy_name = wc_attribute_taxonomy_name( $wc_attribute_name ); |
| 658 |
register_taxonomy( |
| 659 |
$taxonomy_name, |
| 660 |
apply_filters( 'woocommerce_taxonomy_objects_' . $taxonomy_name, array( 'product' ) ), |
| 661 |
apply_filters( |
| 662 |
'woocommerce_taxonomy_args_' . $taxonomy_name, |
| 663 |
array( |
| 664 |
'labels' => array( |
| 665 |
'name' => $wc_attribute_label, |
| 666 |
), |
| 667 |
'hierarchical' => true, |
| 668 |
'show_ui' => false, |
| 669 |
'query_var' => true, |
| 670 |
'rewrite' => false, |
| 671 |
) |
| 672 |
) |
| 673 |
); |
| 674 |
$log['wc_taxonomies'][$wc_attribute_id] = array( |
| 675 |
'title' => wp_slash($wc_attribute_label), |
| 676 |
'slug' => $wc_attribute_name, |
| 677 |
'new_id' => $new_wc_attribute_id, |
| 678 |
'pre_status' => 'new', |
| 679 |
'result' => 'added' |
| 680 |
); |
| 681 |
} else { |
| 682 |
$log['wc_taxonomies'][$wc_attribute_id] = array( |
| 683 |
'title' => wp_slash($wc_attribute_label), |
| 684 |
'slug' => $wc_attribute_name, |
| 685 |
'new_id' => null, |
| 686 |
'pre_status' => 'new', |
| 687 |
'result' => 'not added', |
| 688 |
'wp_error' => $new_wc_attribute_id |
| 689 |
); |
| 690 |
} |
| 691 |
} |
| 692 |
} |
| 693 |
} |
| 694 |
// START - terms |
| 695 |
foreach ( $channel_namespace->term as $term ) { |
| 696 |
$term_namespace = $term->children( $namespaces['wp'] ); |
| 697 |
$term_id = $this->sanitize_data( $term_namespace->term_id, 'term_id' ); |
| 698 |
$term_taxonomy = $this->sanitize_data( $term_namespace->term_taxonomy, 'term_taxonomy' ); |
| 699 |
$term_slug = $this->sanitize_data( $term_namespace->term_slug, 'term_slug' ); |
| 700 |
$term_parent = $this->sanitize_data( $term_namespace->term_parent, 'term_parent' ); |
| 701 |
$term_link = $this->sanitize_data( $term_namespace->term_link, 'term_link' ); |
| 702 |
$term_name = $this->sanitize_data( $term_namespace->term_name, 'term_name' ); |
| 703 |
$term_description = $this->sanitize_data( $term_namespace->term_description, 'term_description' ); |
| 704 |
|
| 705 |
$term_meta = array(); |
| 706 |
if ( !empty($term_namespace->termmeta)) { |
| 707 |
foreach ($term_namespace->termmeta as $meta) { |
| 708 |
$meta_key = $this->sanitize_data( $meta->meta_key, 'meta_key' ); |
| 709 |
$meta_value = $this->sanitize_data( $meta->meta_value, 'meta_value' ); |
| 710 |
$meta_array = array( |
| 711 |
$meta_key => $meta_value |
| 712 |
); |
| 713 |
$term_meta = array_merge( $term_meta, $meta_array ); |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
// if child theme selected |
| 718 |
if ( $is_child_theme ) { |
| 719 |
$term_slug = $theme; |
| 720 |
$term_name = $theme; |
| 721 |
} |
| 722 |
$term_exists = term_exists( $term_slug, $term_taxonomy ); |
| 723 |
if ( $term_exists ) { |
| 724 |
$log['terms'][$term_id] = array( |
| 725 |
'title' => wp_slash($term_name), |
| 726 |
'slug' => $term_slug, |
| 727 |
'taxonomy' => $term_taxonomy, |
| 728 |
'new_id' => $term_exists['term_id'], |
| 729 |
'pre_status' => 'exists', |
| 730 |
'result' => 'none' |
| 731 |
); |
| 732 |
|
| 733 |
if ( !empty($term_meta) ) { |
| 734 |
foreach ($term_meta as $key => $value) { |
| 735 |
update_term_meta( $term_exists['term_id'], $key, $value ); |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
if ( $term_link !== '' ) { |
| 740 |
$log['map_terms'][$term_id] = array( |
| 741 |
'taxonomy' => $term_taxonomy, |
| 742 |
'new_id' => $term_exists['term_id'], |
| 743 |
'demo_url' => $term_link, |
| 744 |
'new_url' => get_term_link( (int) $term_exists['term_id'] ) |
| 745 |
); |
| 746 |
} else { |
| 747 |
$log['map_terms'][$term_id] = array( |
| 748 |
'taxonomy' => $term_taxonomy, |
| 749 |
'new_id' => $term_exists['term_id'] |
| 750 |
); |
| 751 |
} |
| 752 |
$log['map_term_names'][$term_slug] = array( |
| 753 |
'taxonomy' => $term_taxonomy, |
| 754 |
'new_id' => $term_exists['term_id'] |
| 755 |
); |
| 756 |
} else { |
| 757 |
$term_args = array( |
| 758 |
'alias_of' => '', |
| 759 |
'description' => wp_slash( $term_description ), |
| 760 |
'parent' => '', |
| 761 |
'slug' => $term_slug |
| 762 |
); |
| 763 |
if ( taxonomy_exists($term_taxonomy) ) { |
| 764 |
$insert_term = wp_insert_term( wp_slash( $term_name ), $term_taxonomy, $term_args ); |
| 765 |
if ( !is_wp_error($insert_term) ) { |
| 766 |
$log['terms'][$term_id] = array( |
| 767 |
'title' => wp_slash($term_name), |
| 768 |
'slug' => $term_slug, |
| 769 |
'taxonomy' => $term_taxonomy, |
| 770 |
'new_id' => $insert_term['term_id'], |
| 771 |
'pre_status' => 'new', |
| 772 |
'result' => 'added' |
| 773 |
); |
| 774 |
|
| 775 |
if ( !empty($term_meta) ) { |
| 776 |
foreach ($term_meta as $key => $value) { |
| 777 |
update_term_meta( $insert_term['term_id'], $key, $value ); |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
if ( $term_link !== '' ) { |
| 782 |
$log['map_terms'][$term_id] = array( |
| 783 |
'taxonomy' => $term_taxonomy, |
| 784 |
'new_id' => $insert_term['term_id'], |
| 785 |
'demo_url' => $term_link, |
| 786 |
'new_url' => get_term_link( (int) $insert_term['term_id'] ) |
| 787 |
); |
| 788 |
} else { |
| 789 |
$log['map_terms'][$term_id] = array( |
| 790 |
'taxonomy' => $term_taxonomy, |
| 791 |
'new_id' => $insert_term['term_id'] |
| 792 |
); |
| 793 |
} |
| 794 |
$log['map_term_names'][$term_slug] = array( |
| 795 |
'taxonomy' => $term_taxonomy, |
| 796 |
'new_id' => $insert_term['term_id'] |
| 797 |
); |
| 798 |
} else { |
| 799 |
$log['terms'][$term_id] = array( |
| 800 |
'title' => wp_slash($term_name), |
| 801 |
'slug' => $term_slug, |
| 802 |
'taxonomy' => $term_taxonomy, |
| 803 |
'new_id' => null, |
| 804 |
'pre_status' => 'new', |
| 805 |
'result' => 'not added', |
| 806 |
'wp_error' => $insert_term |
| 807 |
); |
| 808 |
} |
| 809 |
} |
| 810 |
} |
| 811 |
} |
| 812 |
// END - terms |
| 813 |
// START - items (content) |
| 814 |
foreach ( $xml->channel->item as $item ) { |
| 815 |
$namespace_wp = $item->children( $namespaces['wp'] ); |
| 816 |
$namespace_content = $item->children( $namespaces['content'] ); |
| 817 |
$namespace_excerpt = $item->children( $namespaces['excerpt'] ); |
| 818 |
$post_title = $this->sanitize_data( $item->title, 'title' ); |
| 819 |
$post_url = $this->sanitize_data( $item->link, 'link' ); |
| 820 |
$post_id = $this->sanitize_data( $namespace_wp->post_id, 'post_id' ); |
| 821 |
$post_parent = $this->sanitize_data( $namespace_wp->post_parent, 'post_parent' ); |
| 822 |
$post_menu_order = $this->sanitize_data( $namespace_wp->menu_order, 'menu_order' ); |
| 823 |
$post_name = $this->sanitize_data( $namespace_wp->post_name, 'post_name' ); |
| 824 |
$post_type = $this->sanitize_data( $namespace_wp->post_type, 'post_type' ); |
| 825 |
$post_content = $this->sanitize_data( $namespace_content->encoded, 'post_content', false ); |
| 826 |
$post_excerpt = $this->sanitize_data( $namespace_excerpt->encoded, 'post_excerpt', false ); |
| 827 |
$post_status = $this->sanitize_data( $namespace_wp->status, 'post_status' ); |
| 828 |
// if child theme selected, then adjust template part args in content |
| 829 |
if ( $is_child_theme ) { |
| 830 |
$post_content = str_replace( '"theme":"'.$theme_parent.'"', '"theme":"'.$theme.'"', $post_content ); |
| 831 |
} |
| 832 |
$post_terms = array(); |
| 833 |
foreach ( $item->category as $c ) { |
| 834 |
$att = $c->attributes(); |
| 835 |
if ( isset( $att['nicename'] ) ) { |
| 836 |
// if 'domain' is 'wp_theme' and child theme selected, then change 'nicename' |
| 837 |
if ( $att['domain'] === 'wp_theme' && $is_child_theme ) { |
| 838 |
$att['nicename'] = $theme; |
| 839 |
} |
| 840 |
$post_terms[(string) $att['nicename']] = (string) $att['domain']; |
| 841 |
} |
| 842 |
} |
| 843 |
$post_meta = array(); |
| 844 |
if ( !empty($namespace_wp->postmeta)) { |
| 845 |
foreach ($namespace_wp->postmeta as $meta) { |
| 846 |
$meta_key = $this->sanitize_data( $meta->meta_key, 'meta_key' ); |
| 847 |
$meta_value = $this->sanitize_data( $meta->meta_value, 'meta_value' ); |
| 848 |
$meta_array = array( |
| 849 |
$meta_key => $meta_value |
| 850 |
); |
| 851 |
$post_meta = array_merge( $post_meta, $meta_array ); |
| 852 |
} |
| 853 |
} |
| 854 |
// start - page, post, product |
| 855 |
if ( 'page' === $post_type || 'post' === $post_type || 'product' === $post_type ) { |
| 856 |
$log_post_id = $this->post_slug_exists( $post_name, $post_type ); |
| 857 |
if ( $log_post_id ) { |
| 858 |
$post_args = array( |
| 859 |
'ID' => $log_post_id, |
| 860 |
'post_content' => $post_content, |
| 861 |
'post_title' => $post_title, |
| 862 |
'post_excerpt' => $post_excerpt, |
| 863 |
'post_status' => $post_status, |
| 864 |
'post_parent' => $post_parent, |
| 865 |
'menu_order' => $post_menu_order, |
| 866 |
//'tax_input' => $tax_input, |
| 867 |
'meta_input' => $post_meta |
| 868 |
); |
| 869 |
wp_update_post( wp_slash( $post_args ), true ); |
| 870 |
// insert post terms |
| 871 |
if ( !empty($post_terms) ) { |
| 872 |
$terms_insert = array(); |
| 873 |
$terms_list = ''; |
| 874 |
foreach ( $post_terms as $term => $taxonomy ) { |
| 875 |
if ( isset( $log['map_term_names'][$term] ) ) { |
| 876 |
$terms_list .= $log['map_term_names'][$term]['new_id'] . ' '; |
| 877 |
} |
| 878 |
$terms_insert[$taxonomy] = array_map( 'intval', explode( ' ', trim($terms_list) ) ); |
| 879 |
} |
| 880 |
foreach ( $terms_insert as $taxonomy => $term_ids ) { |
| 881 |
wp_set_object_terms( $log_post_id, $term_ids, $taxonomy ); |
| 882 |
} |
| 883 |
} |
| 884 |
$log['content'][$post_id] = array( |
| 885 |
'title' => wp_slash($post_title), |
| 886 |
'slug' => $post_name, |
| 887 |
'post_type' => $post_type, |
| 888 |
'new_id' => $log_post_id, |
| 889 |
'pre_status' => 'exists', |
| 890 |
'result' => 'updated' |
| 891 |
); |
| 892 |
$log['map_posts'][$post_id] = array( |
| 893 |
'new_id' => $log_post_id, |
| 894 |
'demo_url' => $post_url, |
| 895 |
'new_url' => get_permalink($log_post_id) |
| 896 |
); |
| 897 |
} else { |
| 898 |
if ( 'post' === $post_type || 'product' === $post_type ) { |
| 899 |
$post_date = wp_date( 'Y-m-d H:i:s', time() - wp_rand(1,864000) ); |
| 900 |
} else { |
| 901 |
$post_date = ''; |
| 902 |
} |
| 903 |
$post_args = array( |
| 904 |
'post_date' => $post_date, |
| 905 |
'post_content' => $post_content, |
| 906 |
'post_title' => $post_title, |
| 907 |
'post_excerpt' => $post_excerpt, |
| 908 |
'post_status' => $post_status, |
| 909 |
'post_name' => $post_name, |
| 910 |
'post_parent' => $post_parent, |
| 911 |
'menu_order' => $post_menu_order, |
| 912 |
'post_type' => $post_type, |
| 913 |
//'tax_input' => $tax_input, |
| 914 |
'meta_input' => $post_meta |
| 915 |
); |
| 916 |
$log_post_id = wp_insert_post( wp_slash( $post_args ), true ); |
| 917 |
// insert post terms |
| 918 |
if ( !empty($post_terms) ) { |
| 919 |
$terms_insert = array(); |
| 920 |
$terms_list = ''; |
| 921 |
foreach ( $post_terms as $term => $taxonomy ) { |
| 922 |
if ( isset( $log['map_term_names'][$term] ) ) { |
| 923 |
$terms_list .= $log['map_term_names'][$term]['new_id'] . ' '; |
| 924 |
} |
| 925 |
$terms_insert[$taxonomy] = array_map( 'intval', explode( ' ', trim($terms_list) ) ); |
| 926 |
} |
| 927 |
foreach ( $terms_insert as $taxonomy => $term_ids ) { |
| 928 |
wp_set_object_terms( $log_post_id, $term_ids, $taxonomy ); |
| 929 |
} |
| 930 |
} |
| 931 |
$log['content'][$post_id] = array( |
| 932 |
'title' => wp_slash($post_title), |
| 933 |
'slug' => $post_name, |
| 934 |
'post_type' => $post_type, |
| 935 |
'new_id' => $log_post_id, |
| 936 |
'pre_status' => 'none', |
| 937 |
'result' => 'added' |
| 938 |
); |
| 939 |
$log['map_posts'][$post_id] = array( |
| 940 |
'new_id' => $log_post_id, |
| 941 |
'demo_url' => $post_url, |
| 942 |
'new_url' => get_permalink($log_post_id) |
| 943 |
); |
| 944 |
} |
| 945 |
// sub-start - product - extra processing |
| 946 |
if ( 'product' === $post_type && !empty($post_terms) && isset($post_meta['_product_attributes']) ) { |
| 947 |
$product_attributes = maybe_unserialize($post_meta['_product_attributes']); |
| 948 |
$term_attributes = array(); |
| 949 |
// build an array of taxonomy (product attribute) => terms (options) |
| 950 |
foreach ( $post_terms as $term => $taxonomy ) { |
| 951 |
if ( str_starts_with($taxonomy ?? '', 'pa_') ) { |
| 952 |
if ( isset($term_attributes[$taxonomy]) ) { |
| 953 |
$term_attributes[$taxonomy] .= '|' . $term; |
| 954 |
} else { |
| 955 |
$term_attributes[$taxonomy] = $term; |
| 956 |
} |
| 957 |
} |
| 958 |
} |
| 959 |
foreach ( $term_attributes as $key => $value ) { |
| 960 |
// if a product attribute (from the post_meta '_product_attributes' value) |
| 961 |
// e.g. 'pa_color' matches with a taxonomy of the post terms |
| 962 |
if ( isset($product_attributes[$key]) ) { |
| 963 |
$value = str_replace( '-', ' ', $value ); |
| 964 |
$product_attributes[$key]['options'] = explode('|', $value); |
| 965 |
} |
| 966 |
} |
| 967 |
// **save this until after all content, and before adding variations** |
| 968 |
$log['product_attrs_to_add'][$log_post_id] = $product_attributes; |
| 969 |
} |
| 970 |
// sub-end - product - extra processing |
| 971 |
} |
| 972 |
// end - page, post, product |
| 973 |
// start - product variation |
| 974 |
elseif ( 'product_variation' === $post_type ) { |
| 975 |
if ( isset($log['map_posts'][$post_parent]['new_id']) ) { |
| 976 |
$new_prod_var_parent_id = $log['map_posts'][$post_parent]['new_id']; |
| 977 |
$prod_var_props = $post_meta; |
| 978 |
$prod_var_props['parent_id'] = $new_prod_var_parent_id; |
| 979 |
$prod_var_props['status'] = $post_status; |
| 980 |
$prod_var_props['menu_order'] = $post_menu_order; |
| 981 |
$prod_var_attrs = array(); |
| 982 |
foreach ($post_meta as $key => $value) { |
| 983 |
//if key starts with attribute_ then add it to $prod_var_attrs |
| 984 |
if ( str_starts_with($key ?? '', 'attribute_') ) { |
| 985 |
$prod_var_attrs[$key] = $value; |
| 986 |
} |
| 987 |
} |
| 988 |
$prod_var_props['attributes'] = $prod_var_attrs; |
| 989 |
$prod_var_props['display_slug'] = $post_name; |
| 990 |
$prod_var_props['display_excerpt'] = $post_excerpt; |
| 991 |
$prod_var_props['demo_url'] = $post_url; |
| 992 |
// **save this until after all content as we need to make sure it is processed AFTER the parent product** |
| 993 |
$log['product_vars_to_add'][$post_id] = $prod_var_props; |
| 994 |
} |
| 995 |
} |
| 996 |
// end - product variation |
| 997 |
// start - patterns |
| 998 |
elseif ( 'wp_block' === $post_type ) { |
| 999 |
$exists_post_id = $this->post_slug_exists( $post_name, $post_type ); |
| 1000 |
if ( $exists_post_id ) { |
| 1001 |
$post_args = array( |
| 1002 |
'ID' => $exists_post_id, |
| 1003 |
'post_content' => $post_content, |
| 1004 |
'post_title' => $post_title, |
| 1005 |
'post_excerpt' => $post_excerpt, |
| 1006 |
'post_status' => $post_status, |
| 1007 |
'post_parent' => $post_parent, |
| 1008 |
'menu_order' => $post_menu_order, |
| 1009 |
'meta_input' => $post_meta |
| 1010 |
); |
| 1011 |
wp_update_post( wp_slash( $post_args ), true ); |
| 1012 |
// insert post terms |
| 1013 |
if ( !empty($post_terms) ) { |
| 1014 |
$terms_insert = array(); |
| 1015 |
$terms_list = ''; |
| 1016 |
foreach ( $post_terms as $term => $taxonomy ) { |
| 1017 |
if ( isset( $log['map_term_names'][$term] ) ) { |
| 1018 |
$terms_list .= $log['map_term_names'][$term]['new_id'] . ' '; |
| 1019 |
} |
| 1020 |
$terms_insert[$taxonomy] = array_map( 'intval', explode( ' ', trim($terms_list) ) ); |
| 1021 |
} |
| 1022 |
foreach ( $terms_insert as $taxonomy => $term_ids ) { |
| 1023 |
wp_set_object_terms( $exists_post_id, $term_ids, $taxonomy ); |
| 1024 |
} |
| 1025 |
} |
| 1026 |
$log['design'][$post_id] = array( |
| 1027 |
'title' => wp_slash($post_title), |
| 1028 |
'slug' => $post_name, |
| 1029 |
'post_type' => $post_type, |
| 1030 |
'new_id' => $exists_post_id, |
| 1031 |
'pre_status' => 'exists', |
| 1032 |
'result' => 'updated' |
| 1033 |
); |
| 1034 |
$log['map_patterns'][$post_id] = array( |
| 1035 |
'new_id' => $exists_post_id |
| 1036 |
); |
| 1037 |
} else { |
| 1038 |
$post_args = array( |
| 1039 |
'post_content' => $post_content, |
| 1040 |
'post_title' => $post_title, |
| 1041 |
'post_excerpt' => $post_excerpt, |
| 1042 |
'post_status' => $post_status, |
| 1043 |
'post_name' => $post_name, |
| 1044 |
'post_parent' => $post_parent, |
| 1045 |
'menu_order' => $post_menu_order, |
| 1046 |
'post_type' => $post_type, |
| 1047 |
'meta_input' => $post_meta |
| 1048 |
); |
| 1049 |
$new_post_id = wp_insert_post( wp_slash( $post_args ), true ); |
| 1050 |
// insert post terms |
| 1051 |
if ( !empty($post_terms) ) { |
| 1052 |
$terms_insert = array(); |
| 1053 |
$terms_list = ''; |
| 1054 |
foreach ( $post_terms as $term => $taxonomy ) { |
| 1055 |
if ( isset( $log['map_term_names'][$term] ) ) { |
| 1056 |
$terms_list .= $log['map_term_names'][$term]['new_id'] . ' '; |
| 1057 |
} |
| 1058 |
$terms_insert[$taxonomy] = array_map( 'intval', explode( ' ', trim($terms_list) ) ); |
| 1059 |
} |
| 1060 |
foreach ( $terms_insert as $taxonomy => $term_ids ) { |
| 1061 |
wp_set_object_terms( $new_post_id, $term_ids, $taxonomy ); |
| 1062 |
} |
| 1063 |
} |
| 1064 |
$log['design'][$post_id] = array( |
| 1065 |
'title' => wp_slash($post_title), |
| 1066 |
'slug' => $post_name, |
| 1067 |
'post_type' => $post_type, |
| 1068 |
'new_id' => $new_post_id, |
| 1069 |
'pre_status' => 'none', |
| 1070 |
'result' => 'added' |
| 1071 |
); |
| 1072 |
$log['map_patterns'][$post_id] = array( |
| 1073 |
'new_id' => $new_post_id |
| 1074 |
); |
| 1075 |
} |
| 1076 |
} |
| 1077 |
// end - patterns |
| 1078 |
// start - wp_navigation |
| 1079 |
elseif ( 'wp_navigation' === $post_type ) { |
| 1080 |
$exists_post_id = $this->post_slug_exists( $post_name, $post_type ); |
| 1081 |
if ( $exists_post_id ) { |
| 1082 |
$post_args = array( |
| 1083 |
'ID' => $exists_post_id, |
| 1084 |
'post_content' => $post_content, |
| 1085 |
'post_title' => $post_title, |
| 1086 |
'post_excerpt' => $post_excerpt, |
| 1087 |
'post_status' => $post_status, |
| 1088 |
'post_parent' => $post_parent, |
| 1089 |
'menu_order' => $post_menu_order, |
| 1090 |
'meta_input' => $post_meta |
| 1091 |
); |
| 1092 |
wp_update_post( wp_slash( $post_args ), true ); |
| 1093 |
$log['design'][$post_id] = array( |
| 1094 |
'title' => wp_slash($post_title), |
| 1095 |
'slug' => $post_name, |
| 1096 |
'post_type' => $post_type, |
| 1097 |
'new_id' => $exists_post_id, |
| 1098 |
'pre_status' => 'exists', |
| 1099 |
'result' => 'updated' |
| 1100 |
); |
| 1101 |
$log['map_navigation'][$post_id] = array( |
| 1102 |
'new_id' => $exists_post_id |
| 1103 |
); |
| 1104 |
} else { |
| 1105 |
$post_args = array( |
| 1106 |
'post_content' => $post_content, |
| 1107 |
'post_title' => $post_title, |
| 1108 |
'post_excerpt' => $post_excerpt, |
| 1109 |
'post_status' => $post_status, |
| 1110 |
'post_name' => $post_name, |
| 1111 |
'post_parent' => $post_parent, |
| 1112 |
'menu_order' => $post_menu_order, |
| 1113 |
'post_type' => $post_type, |
| 1114 |
'meta_input' => $post_meta |
| 1115 |
); |
| 1116 |
$new_post_id = wp_insert_post( wp_slash( $post_args ), true ); |
| 1117 |
$log['design'][$post_id] = array( |
| 1118 |
'title' => wp_slash($post_title), |
| 1119 |
'slug' => $post_name, |
| 1120 |
'post_type' => $post_type, |
| 1121 |
'new_id' => $new_post_id, |
| 1122 |
'pre_status' => 'none', |
| 1123 |
'result' => 'added' |
| 1124 |
); |
| 1125 |
$log['map_navigation'][$post_id] = array( |
| 1126 |
'new_id' => $new_post_id |
| 1127 |
); |
| 1128 |
} |
| 1129 |
} |
| 1130 |
// end - wp_navigation |
| 1131 |
// start - wp_global_styles, custom_css |
| 1132 |
elseif ( 'wp_global_styles' === $post_type || 'custom_css' === $post_type ) { |
| 1133 |
// if child theme selected, then change $post_title and $post_name values |
| 1134 |
if ( $is_child_theme ) { |
| 1135 |
$post_title = str_replace( $theme_parent, $theme, $post_title ); |
| 1136 |
$post_name = str_replace( $theme_parent, $theme, $post_name ); |
| 1137 |
} |
| 1138 |
$tax_input = array(); |
| 1139 |
if ( $item->category ) { |
| 1140 |
foreach ( $item->category as $category ) { |
| 1141 |
$category_domain = $this->sanitize_data( $category['domain'], '' ); |
| 1142 |
$category_slug = $this->sanitize_data( $category['nicename'], '' ); |
| 1143 |
// if child theme selected, then change 'nicename' |
| 1144 |
if ( 'wp_theme' === $category_domain && $theme_parent === $category_slug && $is_child_theme ) { |
| 1145 |
$tax_input[$category_domain] = $theme; |
| 1146 |
} else { |
| 1147 |
$tax_input[$category_domain] = $category_slug; |
| 1148 |
} |
| 1149 |
} |
| 1150 |
} |
| 1151 |
$exists_post_id = $this->post_slug_exists( $post_name, $post_type ); |
| 1152 |
if ( $exists_post_id ) { |
| 1153 |
$post_args = array( |
| 1154 |
'ID' => $exists_post_id, |
| 1155 |
'post_content' => $post_content, |
| 1156 |
'post_title' => $post_title, |
| 1157 |
'post_excerpt' => $post_excerpt, |
| 1158 |
'post_status' => $post_status, |
| 1159 |
'post_parent' => $post_parent, |
| 1160 |
'menu_order' => $post_menu_order, |
| 1161 |
'tax_input' => $tax_input, |
| 1162 |
'meta_input' => $post_meta |
| 1163 |
); |
| 1164 |
wp_update_post( wp_slash( $post_args ), true ); |
| 1165 |
$log['design'][$post_id] = array( |
| 1166 |
'title' => wp_slash($post_title), |
| 1167 |
'slug' => $post_name, |
| 1168 |
'post_type' => $post_type, |
| 1169 |
'new_id' => $exists_post_id, |
| 1170 |
'pre_status' => 'exists', |
| 1171 |
'result' => 'updated' |
| 1172 |
); |
| 1173 |
} else { |
| 1174 |
$post_args = array( |
| 1175 |
'post_content' => $post_content, |
| 1176 |
'post_title' => $post_title, |
| 1177 |
'post_excerpt' => $post_excerpt, |
| 1178 |
'post_status' => $post_status, |
| 1179 |
'post_name' => $post_name, |
| 1180 |
'post_parent' => $post_parent, |
| 1181 |
'menu_order' => $post_menu_order, |
| 1182 |
'post_type' => $post_type, |
| 1183 |
'tax_input' => $tax_input, |
| 1184 |
'meta_input' => $post_meta |
| 1185 |
); |
| 1186 |
$new_post_id = wp_insert_post( wp_slash( $post_args ), true ); |
| 1187 |
$log['design'][$post_id] = array( |
| 1188 |
'title' => wp_slash($post_title), |
| 1189 |
'slug' => $post_name, |
| 1190 |
'post_type' => $post_type, |
| 1191 |
'new_id' => $new_post_id, |
| 1192 |
'pre_status' => 'none', |
| 1193 |
'result' => 'added' |
| 1194 |
); |
| 1195 |
} |
| 1196 |
} |
| 1197 |
// end - wp_global_styles, custom_css |
| 1198 |
// start - wp_template, wp_template_part |
| 1199 |
elseif ( 'wp_template' === $post_type || 'wp_template_part' === $post_type ) { |
| 1200 |
// Process templates & template parts, tied to the theme |
| 1201 |
$tax_input = array(); |
| 1202 |
if ( $item->category ) { |
| 1203 |
foreach ( $item->category as $category ) { |
| 1204 |
$category_domain = $this->sanitize_data( $category['domain'], '' ); |
| 1205 |
$category_slug = $this->sanitize_data( $category['nicename'], '' ); |
| 1206 |
// if child theme selected, then change 'nicename' |
| 1207 |
if ( 'wp_theme' === $category_domain && $theme_parent === $category_slug && $is_child_theme ) { |
| 1208 |
$tax_input[$category_domain] = $theme; |
| 1209 |
} else { |
| 1210 |
$tax_input[$category_domain] = $category_slug; |
| 1211 |
} |
| 1212 |
} |
| 1213 |
} |
| 1214 |
$exists_post_id = $this->theme_template_in_db( $post_name, $post_type, $theme ); |
| 1215 |
if ( $exists_post_id ) { |
| 1216 |
$post_args = array( |
| 1217 |
'ID' => $exists_post_id, |
| 1218 |
'post_content' => $post_content, |
| 1219 |
'post_title' => $post_title, |
| 1220 |
'post_excerpt' => $post_excerpt, |
| 1221 |
'post_status' => $post_status, |
| 1222 |
'post_parent' => $post_parent, |
| 1223 |
'menu_order' => $post_menu_order, |
| 1224 |
'tax_input' => $tax_input, |
| 1225 |
'meta_input' => $post_meta |
| 1226 |
); |
| 1227 |
wp_update_post( wp_slash( $post_args ), true ); |
| 1228 |
$log['design'][$post_id] = array( |
| 1229 |
'title' => wp_slash($post_title), |
| 1230 |
'slug' => $post_name, |
| 1231 |
'post_type' => $post_type, |
| 1232 |
'new_id' => $exists_post_id, |
| 1233 |
'pre_status' => 'exists', |
| 1234 |
'result' => 'updated' |
| 1235 |
); |
| 1236 |
} else { |
| 1237 |
// is template/part included in theme? |
| 1238 |
if ( get_block_template( $theme.'//'.$post_name, $post_type ) ) { |
| 1239 |
$log_result = 'updated'; |
| 1240 |
} else { |
| 1241 |
$log_result = 'added'; |
| 1242 |
} |
| 1243 |
$post_args = array( |
| 1244 |
'post_content' => $post_content, |
| 1245 |
'post_title' => $post_title, |
| 1246 |
'post_excerpt' => $post_excerpt, |
| 1247 |
'post_status' => $post_status, |
| 1248 |
'post_name' => $post_name, |
| 1249 |
'post_parent' => $post_parent, |
| 1250 |
'menu_order' => $post_menu_order, |
| 1251 |
'post_type' => $post_type, |
| 1252 |
'tax_input' => $tax_input, |
| 1253 |
'meta_input' => $post_meta |
| 1254 |
); |
| 1255 |
$new_post_id = wp_insert_post( wp_slash( $post_args ), true ); |
| 1256 |
$log['design'][$post_id] = array( |
| 1257 |
'title' => wp_slash($post_title), |
| 1258 |
'slug' => $post_name, |
| 1259 |
'post_type' => $post_type, |
| 1260 |
'new_id' => $new_post_id, |
| 1261 |
'pre_status' => 'none', |
| 1262 |
'result' => $log_result |
| 1263 |
); |
| 1264 |
$log['fix_slugs'][$new_post_id] = $post_name; |
| 1265 |
} |
| 1266 |
} |
| 1267 |
// end - wp_template, wp_template_part |
| 1268 |
// start - font families |
| 1269 |
elseif ( 'wp_font_family' === $post_type ) { |
| 1270 |
$exists_post_id = $this->post_slug_exists( $post_name, $post_type ); |
| 1271 |
if ( $exists_post_id ) { |
| 1272 |
$post_args = array( |
| 1273 |
'ID' => $exists_post_id, |
| 1274 |
'post_content' => $post_content, |
| 1275 |
'post_title' => $post_title, |
| 1276 |
'post_excerpt' => $post_excerpt, |
| 1277 |
'post_status' => $post_status, |
| 1278 |
'post_parent' => $post_parent, |
| 1279 |
'menu_order' => $post_menu_order, |
| 1280 |
'meta_input' => $post_meta |
| 1281 |
); |
| 1282 |
wp_update_post( wp_slash( $post_args ), true ); |
| 1283 |
$log['design'][$post_id] = array( |
| 1284 |
'title' => wp_slash($post_title), |
| 1285 |
'slug' => $post_name, |
| 1286 |
'post_type' => $post_type, |
| 1287 |
'new_id' => $exists_post_id, |
| 1288 |
'pre_status' => 'exists', |
| 1289 |
'result' => 'none' |
| 1290 |
); |
| 1291 |
$log['map_fonts'][$post_id] = array( |
| 1292 |
'new_id' => $exists_post_id |
| 1293 |
); |
| 1294 |
} else { |
| 1295 |
$post_args = array( |
| 1296 |
'post_content' => $post_content, |
| 1297 |
'post_title' => $post_title, |
| 1298 |
'post_excerpt' => $post_excerpt, |
| 1299 |
'post_status' => $post_status, |
| 1300 |
'post_name' => $post_name, |
| 1301 |
'post_parent' => $post_parent, |
| 1302 |
'menu_order' => $post_menu_order, |
| 1303 |
'post_type' => $post_type, |
| 1304 |
'meta_input' => $post_meta |
| 1305 |
); |
| 1306 |
$new_post_id = wp_insert_post( wp_slash( $post_args ), true ); |
| 1307 |
$log['design'][$post_id] = array( |
| 1308 |
'title' => wp_slash($post_title), |
| 1309 |
'slug' => $post_name, |
| 1310 |
'post_type' => $post_type, |
| 1311 |
'new_id' => $new_post_id, |
| 1312 |
'pre_status' => 'none', |
| 1313 |
'result' => 'added' |
| 1314 |
); |
| 1315 |
$log['map_fonts'][$post_id] = array( |
| 1316 |
'new_id' => $new_post_id |
| 1317 |
); |
| 1318 |
} |
| 1319 |
} |
| 1320 |
// end - font families |
| 1321 |
// start - font faces |
| 1322 |
elseif ( 'wp_font_face' === $post_type ) { |
| 1323 |
$content_decoded = json_decode( $post_content, true ); |
| 1324 |
if ( isset( $content_decoded['src'] ) && $content_decoded['src'] !== '' ) { |
| 1325 |
$font_face_url = $content_decoded['src']; |
| 1326 |
$exists_post_id = $this->font_face_exists( $font_face_url, $post_name ); |
| 1327 |
if ( $exists_post_id ) { |
| 1328 |
$log['design'][$post_id] = array( |
| 1329 |
'title' => wp_slash($post_title), |
| 1330 |
'slug' => $post_name, |
| 1331 |
'post_type' => $post_type, |
| 1332 |
'new_id' => $exists_post_id, |
| 1333 |
'pre_status' => 'exists', |
| 1334 |
'result' => 'none' |
| 1335 |
); |
| 1336 |
$log['map_font_faces'][$post_id] = array( |
| 1337 |
'new_id' => $exists_post_id, |
| 1338 |
'demo_url' => $font_face_url, |
| 1339 |
'new_url' => $this->font_face_new_url( $font_face_url ) |
| 1340 |
); |
| 1341 |
} else { |
| 1342 |
$post = array( |
| 1343 |
'title' => $post_title, |
| 1344 |
'slug' => $post_name |
| 1345 |
); |
| 1346 |
// add new font face |
| 1347 |
$font_face_upload = $this->font_face_upload( $font_face_url, $post_title ); |
| 1348 |
if ( $font_face_upload ) { |
| 1349 |
$post_args = array( |
| 1350 |
'post_content' => $post_content, |
| 1351 |
'post_title' => $post_title, |
| 1352 |
'post_excerpt' => $post_excerpt, |
| 1353 |
'post_status' => $post_status, |
| 1354 |
'post_name' => $post_name, |
| 1355 |
'post_parent' => $post_parent, |
| 1356 |
'menu_order' => $post_menu_order, |
| 1357 |
'post_type' => $post_type, |
| 1358 |
'meta_input' => $post_meta |
| 1359 |
); |
| 1360 |
$new_post_id = wp_insert_post( wp_slash( $post_args ), true ); |
| 1361 |
$log['design'][$post_id] = array( |
| 1362 |
'title' => wp_slash($post_title), |
| 1363 |
'slug' => $post_name, |
| 1364 |
'post_type' => $post_type, |
| 1365 |
'new_id' => $new_post_id, |
| 1366 |
'pre_status' => 'none', |
| 1367 |
'result' => 'added' |
| 1368 |
); |
| 1369 |
$log['map_font_faces'][$post_id] = array( |
| 1370 |
'new_id' => $new_post_id, |
| 1371 |
'demo_url' => $font_face_url, |
| 1372 |
'new_url' => $font_face_upload['url'] |
| 1373 |
); |
| 1374 |
} |
| 1375 |
} |
| 1376 |
} |
| 1377 |
} |
| 1378 |
// end - font faces |
| 1379 |
// start - attachment |
| 1380 |
elseif ( 'attachment' === $post_type ) { |
| 1381 |
$attachment_url = $this->sanitize_data( $namespace_wp->attachment_url, 'attachment_url' ); |
| 1382 |
$attachment_mime_type = $this->sanitize_data( $namespace_wp->post_mime_type, 'post_mime_type' ); |
| 1383 |
$exists_post_id = $this->attachment_exists( $attachment_url, $attachment_mime_type ); |
| 1384 |
if ( $exists_post_id ) { |
| 1385 |
$log['attachments'][$post_id] = array( |
| 1386 |
'title' => wp_slash($post_title), |
| 1387 |
'slug' => $post_name, |
| 1388 |
'post_type' => $post_type, |
| 1389 |
'new_id' => $exists_post_id, |
| 1390 |
'pre_status' => 'exists', |
| 1391 |
'result' => 'none' |
| 1392 |
); |
| 1393 |
$new_attachment_url = wp_get_attachment_image_url( $exists_post_id, 'full' ); |
| 1394 |
$log['map_attachments'][$post_id] = array( |
| 1395 |
'new_id' => $exists_post_id, |
| 1396 |
'demo_link' => $post_url, |
| 1397 |
'demo_url' => $attachment_url, |
| 1398 |
'new_link' => get_permalink($exists_post_id), |
| 1399 |
'new_url' => $new_attachment_url, |
| 1400 |
'sizes' => $this->attachment_sizes_map( $exists_post_id, $attachment_url, $new_attachment_url ,$post_meta ) |
| 1401 |
); |
| 1402 |
} else { |
| 1403 |
// add new attachment |
| 1404 |
$new_post_id = $this->attachment_upload( $attachment_url, $post_title ); |
| 1405 |
$log['attachments'][$post_id] = array( |
| 1406 |
'title' => wp_slash($post_title), |
| 1407 |
'slug' => $post_name, |
| 1408 |
'post_type' => $post_type, |
| 1409 |
'new_id' => $new_post_id, |
| 1410 |
'pre_status' => 'none', |
| 1411 |
'result' => 'added' |
| 1412 |
); |
| 1413 |
$new_attachment_url = wp_get_attachment_image_url( $new_post_id, 'full' ); |
| 1414 |
$log['map_attachments'][$post_id] = array( |
| 1415 |
'new_id' => $new_post_id, |
| 1416 |
'demo_link' => $post_url, |
| 1417 |
'demo_url' => $attachment_url, |
| 1418 |
'new_link' => get_permalink($new_post_id), |
| 1419 |
'new_url' => $new_attachment_url, |
| 1420 |
'sizes' => $this->attachment_sizes_map( $new_post_id, $attachment_url, $new_attachment_url ,$post_meta ) |
| 1421 |
); |
| 1422 |
} |
| 1423 |
} |
| 1424 |
// end - attachment |
| 1425 |
// start - other |
| 1426 |
else { |
| 1427 |
$exists_post_id = $this->post_slug_exists( $post_name, $post_type ); |
| 1428 |
if ( $exists_post_id ) { |
| 1429 |
$post_args = array( |
| 1430 |
'ID' => $exists_post_id, |
| 1431 |
'post_content' => $post_content, |
| 1432 |
'post_title' => $post_title, |
| 1433 |
'post_excerpt' => $post_excerpt, |
| 1434 |
'post_status' => $post_status, |
| 1435 |
'post_parent' => $post_parent, |
| 1436 |
'menu_order' => $post_menu_order, |
| 1437 |
'meta_input' => $post_meta |
| 1438 |
); |
| 1439 |
wp_update_post( wp_slash( $post_args ), true ); |
| 1440 |
// insert post terms |
| 1441 |
if ( !empty($post_terms) ) { |
| 1442 |
$terms_insert = array(); |
| 1443 |
$terms_list = ''; |
| 1444 |
foreach ( $post_terms as $term => $taxonomy ) { |
| 1445 |
if ( isset( $log['map_term_names'][$term] ) ) { |
| 1446 |
$terms_list .= $log['map_term_names'][$term]['new_id'] . ' '; |
| 1447 |
} |
| 1448 |
$terms_insert[$taxonomy] = array_map( 'intval', explode( ' ', trim($terms_list) ) ); |
| 1449 |
} |
| 1450 |
foreach ( $terms_insert as $taxonomy => $term_ids ) { |
| 1451 |
wp_set_object_terms( $exists_post_id, $term_ids, $taxonomy ); |
| 1452 |
} |
| 1453 |
} |
| 1454 |
$log['other'][$post_id] = array( |
| 1455 |
'title' => wp_slash($post_title), |
| 1456 |
'slug' => $post_name, |
| 1457 |
'post_type' => $post_type, |
| 1458 |
'new_id' => $exists_post_id, |
| 1459 |
'pre_status' => 'exists', |
| 1460 |
'result' => 'updated' |
| 1461 |
); |
| 1462 |
$log['map_other'][$post_id] = array( |
| 1463 |
'new_id' => $exists_post_id, |
| 1464 |
'demo_url' => $post_url, |
| 1465 |
'new_url' => get_permalink($exists_post_id) |
| 1466 |
); |
| 1467 |
} else { |
| 1468 |
$post_args = array( |
| 1469 |
'post_content' => $post_content, |
| 1470 |
'post_title' => $post_title, |
| 1471 |
'post_excerpt' => $post_excerpt, |
| 1472 |
'post_status' => $post_status, |
| 1473 |
'post_name' => $post_name, |
| 1474 |
'post_parent' => $post_parent, |
| 1475 |
'menu_order' => $post_menu_order, |
| 1476 |
'post_type' => $post_type, |
| 1477 |
'meta_input' => $post_meta |
| 1478 |
); |
| 1479 |
$new_post_id = wp_insert_post( wp_slash( $post_args ), true ); |
| 1480 |
// insert post terms |
| 1481 |
if ( !empty($post_terms) ) { |
| 1482 |
$terms_insert = array(); |
| 1483 |
$terms_list = ''; |
| 1484 |
foreach ( $post_terms as $term => $taxonomy ) { |
| 1485 |
if ( isset( $log['map_term_names'][$term] ) ) { |
| 1486 |
$terms_list .= $log['map_term_names'][$term]['new_id'] . ' '; |
| 1487 |
} |
| 1488 |
$terms_insert[$taxonomy] = array_map( 'intval', explode( ' ', trim($terms_list) ) ); |
| 1489 |
} |
| 1490 |
foreach ( $terms_insert as $taxonomy => $term_ids ) { |
| 1491 |
wp_set_object_terms( $new_post_id, $term_ids, $taxonomy ); |
| 1492 |
} |
| 1493 |
} |
| 1494 |
$log['other'][$post_id] = array( |
| 1495 |
'title' => wp_slash($post_title), |
| 1496 |
'slug' => $post_name, |
| 1497 |
'post_type' => $post_type, |
| 1498 |
'new_id' => $new_post_id, |
| 1499 |
'pre_status' => 'none', |
| 1500 |
'result' => 'added' |
| 1501 |
); |
| 1502 |
$log['map_other'][$post_id] = array( |
| 1503 |
'new_id' => $new_post_id, |
| 1504 |
'demo_url' => $post_url, |
| 1505 |
'new_url' => get_permalink($new_post_id) |
| 1506 |
); |
| 1507 |
} |
| 1508 |
} |
| 1509 |
// end - other |
| 1510 |
} |
| 1511 |
// END - items (content) |
| 1512 |
// start - process product attributes and variations |
| 1513 |
if ( class_exists('WooCommerce') ) { |
| 1514 |
// add the product attributes |
| 1515 |
if ( isset($log['product_attrs_to_add']) ) { |
| 1516 |
foreach ( $log['product_attrs_to_add'] as $key => $value ) { |
| 1517 |
foreach ( $value as $attrs ) { |
| 1518 |
$this->set_product_attribute( $key, $attrs ); |
| 1519 |
} |
| 1520 |
} |
| 1521 |
} |
| 1522 |
// add the product variations |
| 1523 |
if ( isset($log['product_vars_to_add']) ) { |
| 1524 |
foreach ( $log['product_vars_to_add'] as $key => $props ) { |
| 1525 |
$product_variations = $this->get_product_variations( $props['parent_id'] ); |
| 1526 |
if ( array_key_exists( $props['display_slug'], $product_variations ) ) { |
| 1527 |
$variation_id = $product_variations[$props['display_slug']]; |
| 1528 |
$log['product_variations'][$props['parent_id']][$key] = array( |
| 1529 |
'title' => wp_slash($props['display_excerpt']), |
| 1530 |
'slug' => $props['display_slug'], |
| 1531 |
'new_id' => $variation_id, |
| 1532 |
'pre_status' => 'exists', |
| 1533 |
'result' => 'none' |
| 1534 |
); |
| 1535 |
$log['map_posts'][$key] = array( |
| 1536 |
'new_id' => $variation_id, |
| 1537 |
'demo_url' => $props['demo_url'], |
| 1538 |
'new_url' => get_permalink($variation_id) |
| 1539 |
); |
| 1540 |
} else { |
| 1541 |
$variation_id = $this->set_product_variation( $props ); |
| 1542 |
if ( !is_wp_error($variation_id) ) { |
| 1543 |
$log['product_variations'][$props['parent_id']][$key] = array( |
| 1544 |
'title' => wp_slash($props['display_excerpt']), |
| 1545 |
'slug' => $props['display_slug'], |
| 1546 |
'new_id' => $variation_id, |
| 1547 |
'pre_status' => 'none', |
| 1548 |
'result' => 'added' |
| 1549 |
); |
| 1550 |
$log['map_posts'][$key] = array( |
| 1551 |
'new_id' => $variation_id, |
| 1552 |
'demo_url' => $props['demo_url'], |
| 1553 |
'new_url' => get_permalink($variation_id) |
| 1554 |
); |
| 1555 |
} else { |
| 1556 |
$log['product_variations'][$props['parent_id']][$key] = array( |
| 1557 |
'title' => wp_slash($props['display_excerpt']), |
| 1558 |
'slug' => $props['display_slug'], |
| 1559 |
'pre_status' => 'none', |
| 1560 |
'result' => 'not added', |
| 1561 |
'wp_error' => $variation_id |
| 1562 |
); |
| 1563 |
} |
| 1564 |
} |
| 1565 |
} |
| 1566 |
} |
| 1567 |
} |
| 1568 |
// end - process product attributes and variations |
| 1569 |
return $log; |
| 1570 |
} |
| 1571 |
|
| 1572 |
/** |
| 1573 |
* Save product attribute. |
| 1574 |
* |
| 1575 |
* @param int $product_id WP post id. |
| 1576 |
* @param array $attrs product attributes. |
| 1577 |
*/ |
| 1578 |
public function set_product_attribute( $product_id, $attrs ) { |
| 1579 |
$product = wc_get_product($product_id); |
| 1580 |
if ( $product ) { |
| 1581 |
$attribute = new \WC_Product_Attribute(); |
| 1582 |
if ( $attrs['is_taxonomy'] ) { |
| 1583 |
$attribute->set_id( wc_attribute_taxonomy_id_by_name( $attrs['name'] ) ); |
| 1584 |
} |
| 1585 |
$attribute->set_name( $attrs['name'] ); |
| 1586 |
$attribute->set_options( $attrs['options'] ); |
| 1587 |
$attribute->set_position( $attrs['position'] ); |
| 1588 |
$attribute->set_visible( $attrs['is_visible'] ); |
| 1589 |
$attribute->set_variation( $attrs['is_variation'] ); |
| 1590 |
$product->set_attributes( array( $attribute ) ); |
| 1591 |
$product->save(); |
| 1592 |
} |
| 1593 |
} |
| 1594 |
|
| 1595 |
/** |
| 1596 |
* Get product variations slugs. |
| 1597 |
* |
| 1598 |
* @param int $product_id WP post id. |
| 1599 |
* @return array |
| 1600 |
*/ |
| 1601 |
public function get_product_variations( $product_id ) { |
| 1602 |
$product_variations = array(); |
| 1603 |
$variations = wc_get_products( array( |
| 1604 |
'parent' => $product_id, |
| 1605 |
'type' => 'variation', |
| 1606 |
) ); |
| 1607 |
if ( $variations ) { |
| 1608 |
foreach ( $variations as $variation ) { |
| 1609 |
$product_variations[$variation->get_slug()] = $variation->get_id(); |
| 1610 |
} |
| 1611 |
} |
| 1612 |
return $product_variations; |
| 1613 |
} |
| 1614 |
|
| 1615 |
/** |
| 1616 |
* Save product variation. |
| 1617 |
* |
| 1618 |
* @param array $props variation properties. |
| 1619 |
* @return int $variation_id WP post id. |
| 1620 |
*/ |
| 1621 |
public function set_product_variation( $props ) { |
| 1622 |
$variation = new \WC_Product_Variation(); |
| 1623 |
foreach ( $props as $key => $value ) { |
| 1624 |
if ( $key === 'parent_id' ) { |
| 1625 |
$variation->set_parent_id( $value ); |
| 1626 |
} elseif ( $key === 'status' ) { |
| 1627 |
$variation->set_status( $value ); |
| 1628 |
} elseif ( $key === 'menu_order' ) { |
| 1629 |
$variation->set_menu_order( $value ); |
| 1630 |
} elseif ( $key === '_price' ) { |
| 1631 |
$variation->set_price( $value ); |
| 1632 |
} elseif ( $key === '_regular_price' ) { |
| 1633 |
$variation->set_regular_price( $value ); |
| 1634 |
} elseif ( $key === '_sale_price' ) { |
| 1635 |
$variation->set_sale_price( $value ); |
| 1636 |
} elseif ( $key === '_virtual' ) { |
| 1637 |
$variation->set_virtual( $value ); |
| 1638 |
} elseif ( $key === '_downloadable' ) { |
| 1639 |
$variation->set_downloadable( $value ); |
| 1640 |
} elseif ( $key === '_sku' ) { |
| 1641 |
$variation->set_sku( $value ); |
| 1642 |
} elseif ( $key === '_weight' ) { |
| 1643 |
$variation->set_weight( $value ); |
| 1644 |
} elseif ( $key === '_length' ) { |
| 1645 |
$variation->set_length( $value ); |
| 1646 |
} elseif ( $key === '_width' ) { |
| 1647 |
$variation->set_width( $value ); |
| 1648 |
} elseif ( $key === '_height' ) { |
| 1649 |
$variation->set_height( $value ); |
| 1650 |
} /*elseif ( $key === '_product_image_gallery' ) { |
| 1651 |
$variation->set_gallery_image_ids( $value );//???? |
| 1652 |
}*/ elseif ( $key === '_variation_description' ) { |
| 1653 |
$variation->set_description( $value ); |
| 1654 |
} elseif ( $key === '_thumbnail_id' ) { |
| 1655 |
$variation->set_image_id( $value ); |
| 1656 |
} elseif ( $key === 'attributes' ) { |
| 1657 |
$variation->set_attributes( $value ); |
| 1658 |
} |
| 1659 |
} |
| 1660 |
$variation_id = $variation->save(); |
| 1661 |
return $variation_id; |
| 1662 |
} |
| 1663 |
|
| 1664 |
/** |
| 1665 |
* Upload and save media attachment. |
| 1666 |
* |
| 1667 |
* @param string $url media URI. |
| 1668 |
* @param string $title media title. |
| 1669 |
* @return int WP post id. |
| 1670 |
*/ |
| 1671 |
public function attachment_upload( $url, $title ) { |
| 1672 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 1673 |
set_time_limit( 150 ); |
| 1674 |
wp_raise_memory_limit( 'image' ); |
| 1675 |
$temp_file = download_url( $url ); |
| 1676 |
if( is_wp_error( $temp_file ) ) { |
| 1677 |
return false; |
| 1678 |
} |
| 1679 |
$file = array( |
| 1680 |
'name' => wp_basename( $url ), |
| 1681 |
'type' => wp_check_filetype( $temp_file ), |
| 1682 |
'tmp_name' => $temp_file, |
| 1683 |
'size' => filesize( $temp_file ), |
| 1684 |
); |
| 1685 |
$sideload = wp_handle_sideload( |
| 1686 |
$file, |
| 1687 |
array( |
| 1688 |
'test_form' => false |
| 1689 |
) |
| 1690 |
); |
| 1691 |
if( ! empty( $sideload[ 'error' ] ) ) { |
| 1692 |
return false; |
| 1693 |
} |
| 1694 |
if ( $title === '' ) { |
| 1695 |
$title = wp_basename( $sideload[ 'file' ] ); |
| 1696 |
} |
| 1697 |
$attachment_id = wp_insert_attachment( |
| 1698 |
array( |
| 1699 |
'guid' => $sideload[ 'url' ], |
| 1700 |
'post_mime_type' => $sideload[ 'type' ], |
| 1701 |
'post_title' => $title, |
| 1702 |
'post_content' => '', |
| 1703 |
'post_status' => 'inherit', |
| 1704 |
), |
| 1705 |
$sideload[ 'file' ] |
| 1706 |
); |
| 1707 |
if( is_wp_error( $attachment_id ) || ! $attachment_id ) { |
| 1708 |
return false; |
| 1709 |
} |
| 1710 |
require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
| 1711 |
wp_update_attachment_metadata( |
| 1712 |
$attachment_id, |
| 1713 |
wp_generate_attachment_metadata( $attachment_id, $sideload[ 'file' ] ) |
| 1714 |
); |
| 1715 |
return $attachment_id; |
| 1716 |
} |
| 1717 |
|
| 1718 |
/** |
| 1719 |
* Check if a given attachment exists. |
| 1720 |
* |
| 1721 |
* @param string $attachment_url attachment URI. |
| 1722 |
* @param string $mime_type mime type |
| 1723 |
* @return int/bool WP post id if exists, bool false otherwise. |
| 1724 |
*/ |
| 1725 |
public function attachment_exists( $attachment_url, $mime_type = 'image/jpeg' ) { |
| 1726 |
$uploads_info = wp_get_upload_dir(); |
| 1727 |
$file_name = wp_basename( $attachment_url ); |
| 1728 |
$check_file = trailingslashit( $uploads_info['url'] ) . $file_name; |
| 1729 |
global $wpdb; |
| 1730 |
$result = $wpdb->get_results( |
| 1731 |
$wpdb->prepare( |
| 1732 |
"SELECT ID FROM $wpdb->posts WHERE guid = '%1s' AND post_type = 'attachment' AND post_mime_type = '%2s' LIMIT 1", |
| 1733 |
$check_file, |
| 1734 |
$mime_type |
| 1735 |
) |
| 1736 |
); |
| 1737 |
if ( $result ) { |
| 1738 |
return $result[0]->ID; |
| 1739 |
} else { |
| 1740 |
return false; |
| 1741 |
} |
| 1742 |
} |
| 1743 |
|
| 1744 |
public function attachment_sizes_map( $id, $demo_attachment_url, $new_attachment_url, $post_meta ) { |
| 1745 |
if ( isset( $post_meta['_wp_attachment_metadata'] ) && !empty( $post_meta['_wp_attachment_metadata'] ) ) { |
| 1746 |
$demo_img_dir = trailingslashit( dirname( $demo_attachment_url ) ); |
| 1747 |
$attachment_sizes = $post_meta['_wp_attachment_metadata']; |
| 1748 |
$demo_img_meta = maybe_unserialize($attachment_sizes); |
| 1749 |
$image_map = array(); |
| 1750 |
$image_map[$demo_attachment_url] = $new_attachment_url; |
| 1751 |
$new_img_meta = wp_get_attachment_metadata( $id ); |
| 1752 |
if ( isset( $demo_img_meta['sizes'] ) && is_array( $demo_img_meta['sizes'] ) ) { |
| 1753 |
foreach ( $demo_img_meta['sizes'] as $size => $size_data ) { |
| 1754 |
if ( isset( $size_data['file'] ) && '' !== $size_data['file'] ) { |
| 1755 |
if ( isset( $new_img_meta['sizes'][$size]['file'] ) && '' !== $new_img_meta['sizes'][$size]['file'] ) { |
| 1756 |
$image_map[$demo_img_dir . $size_data['file']] = wp_get_attachment_image_url( $id, $size ); |
| 1757 |
} else { |
| 1758 |
$image_map[$demo_img_dir . $size_data['file']] = $new_attachment_url; |
| 1759 |
} |
| 1760 |
} |
| 1761 |
} |
| 1762 |
|
| 1763 |
} |
| 1764 |
} else { |
| 1765 |
$image_map = ''; |
| 1766 |
} |
| 1767 |
return $image_map; |
| 1768 |
} |
| 1769 |
|
| 1770 |
public function handle_font_file_upload_error( $file, $message ) { |
| 1771 |
$status = 500; |
| 1772 |
$code = 'rest_font_upload_unknown_error'; |
| 1773 |
if ( __( 'Sorry, you are not allowed to upload this file type.', 'starter-sites' ) === $message ) { |
| 1774 |
$status = 400; |
| 1775 |
$code = 'rest_font_upload_invalid_file_type'; |
| 1776 |
} |
| 1777 |
return new \WP_Error( $code, $message, array( 'status' => $status ) ); |
| 1778 |
} |
| 1779 |
|
| 1780 |
public function handle_font_file_upload( $file ) { |
| 1781 |
add_filter( 'upload_mimes', array( 'WP_Font_Utils', 'get_allowed_font_mime_types' ) ); |
| 1782 |
// Filter the upload directory to return the fonts directory. |
| 1783 |
add_filter( 'upload_dir', '_wp_filter_font_directory' ); |
| 1784 |
$overrides = array( |
| 1785 |
'upload_error_handler' => array( $this, 'handle_font_file_upload_error' ), |
| 1786 |
'action' => 'wp_handle_font_upload', |
| 1787 |
'test_form' => false, |
| 1788 |
// Only allow uploading font files for this request. |
| 1789 |
'mimes' => \WP_Font_Utils::get_allowed_font_mime_types(), |
| 1790 |
); |
| 1791 |
$uploaded_file = wp_handle_upload( $file, $overrides ); |
| 1792 |
remove_filter( 'upload_dir', '_wp_filter_font_directory' ); |
| 1793 |
remove_filter( 'upload_mimes', array( 'WP_Font_Utils', 'get_allowed_font_mime_types' ) ); |
| 1794 |
return $uploaded_file; |
| 1795 |
} |
| 1796 |
|
| 1797 |
/** |
| 1798 |
* Upload a font face. |
| 1799 |
* |
| 1800 |
* @param string $url font URI. |
| 1801 |
* @param string $title font title. |
| 1802 |
* @return int/bool WP post id, false otherwise. |
| 1803 |
*/ |
| 1804 |
public function font_face_upload( $url, $title ) { |
| 1805 |
$temp_file = download_url( $url ); |
| 1806 |
if ( is_wp_error( $temp_file ) ) { |
| 1807 |
return false; |
| 1808 |
} else { |
| 1809 |
$file = array( |
| 1810 |
'name' => wp_basename( $url ), |
| 1811 |
'type' => wp_check_filetype( $temp_file ), |
| 1812 |
'tmp_name' => $temp_file, |
| 1813 |
'size' => filesize( $temp_file ), |
| 1814 |
); |
| 1815 |
$file_upload = $this->handle_font_file_upload( $file ); |
| 1816 |
return $file_upload; |
| 1817 |
} |
| 1818 |
} |
| 1819 |
|
| 1820 |
/** |
| 1821 |
* Check if a font face exists. |
| 1822 |
* |
| 1823 |
* @param string $font_face_url font URI. |
| 1824 |
* @param string $post_name WP post name. |
| 1825 |
* @return int/bool WP post id if exists, false otherwise. |
| 1826 |
*/ |
| 1827 |
public function font_face_exists( $font_face_url, $post_name ) { |
| 1828 |
$font_dir = wp_get_font_dir(); |
| 1829 |
$file_name = wp_basename( $font_face_url ); |
| 1830 |
$check_file = trailingslashit( $font_dir['basedir'] ) . $file_name; |
| 1831 |
if ( file_exists($check_file) ) { |
| 1832 |
$post_id = $this->post_slug_exists( $post_name, 'wp_font_face' ); |
| 1833 |
if ( $post_id ) { |
| 1834 |
return $post_id; |
| 1835 |
} else { |
| 1836 |
return false; |
| 1837 |
} |
| 1838 |
} else { |
| 1839 |
return false; |
| 1840 |
} |
| 1841 |
} |
| 1842 |
|
| 1843 |
/** |
| 1844 |
* Returns a renovated font face URI. |
| 1845 |
* |
| 1846 |
* @param string $font_face_url font URI. |
| 1847 |
* @return string modified font URI, demo site URI replaced with this installation's font upload directory. |
| 1848 |
*/ |
| 1849 |
public function font_face_new_url( $font_face_url ) { |
| 1850 |
$font_dir = wp_get_font_dir(); |
| 1851 |
$file_name = wp_basename( $font_face_url ); |
| 1852 |
return trailingslashit( $font_dir['baseurl'] ) . $file_name; |
| 1853 |
} |
| 1854 |
|
| 1855 |
/** |
| 1856 |
* Check if a post exists. |
| 1857 |
* |
| 1858 |
* @param string $slug WP post name. |
| 1859 |
* @param string $type WP post type. |
| 1860 |
* @return int/bool WP post id if exists, false otherwise. |
| 1861 |
*/ |
| 1862 |
public function post_slug_exists( $slug, $type = 'post' ) { |
| 1863 |
$args = array( |
| 1864 |
'post_type' => $type, |
| 1865 |
'name' => $slug, |
| 1866 |
'posts_per_page' => 1, |
| 1867 |
); |
| 1868 |
$posts = new \WP_Query( $args ); |
| 1869 |
if ( $posts->have_posts() ) { |
| 1870 |
$posts->the_post(); |
| 1871 |
return $posts->post->ID; |
| 1872 |
} else { |
| 1873 |
return false; |
| 1874 |
} |
| 1875 |
} |
| 1876 |
|
| 1877 |
/** |
| 1878 |
* Check if a theme template/part exists as a user customized post (an edited template/part). |
| 1879 |
* |
| 1880 |
* @param string $slug WP post name. |
| 1881 |
* @param string $type WP post type. |
| 1882 |
* @param string $theme_slug theme slug. |
| 1883 |
* @return int WP post id if exists. |
| 1884 |
*/ |
| 1885 |
public function theme_template_in_db( $slug = '', $type = '', $theme_slug = '' ) { |
| 1886 |
$post_id = 0; |
| 1887 |
$args = array( |
| 1888 |
'post_type' => $type, |
| 1889 |
'name' => $slug, // Returns all templates with this slug (from all themes) whether using or not using 'tax_query' |
| 1890 |
//'post_name' => $slug, // Returns all templates of the theme if using 'tax_query' || Returns all templates from all themes if not using 'tax_query' |
| 1891 |
'nopaging' => true, |
| 1892 |
'posts_per_page' => -1, |
| 1893 |
/*'tax_query' => array( |
| 1894 |
array( |
| 1895 |
'taxonomy' => 'wp_theme', |
| 1896 |
'field' => 'slug', |
| 1897 |
'terms' => array( $theme_slug ) |
| 1898 |
) |
| 1899 |
)*/ // Leave these comments, as helps explain the weirdness above. |
| 1900 |
); |
| 1901 |
$posts = new \WP_Query( $args ); |
| 1902 |
if ( $posts->have_posts() ) { |
| 1903 |
while ( $posts->have_posts() ) { |
| 1904 |
$posts->the_post(); |
| 1905 |
$terms = get_the_terms( $posts->post->ID, 'wp_theme' ); |
| 1906 |
if ( $terms && $terms[0]->slug === $theme_slug ) { |
| 1907 |
$post_id = $posts->post->ID; |
| 1908 |
break; |
| 1909 |
} |
| 1910 |
} |
| 1911 |
} |
| 1912 |
return $post_id; |
| 1913 |
} |
| 1914 |
|
| 1915 |
/** |
| 1916 |
* Sanitize data, and return strings that can be used for conditional checks. |
| 1917 |
*/ |
| 1918 |
public function sanitize_data( $input, $field, $unslash = true ) { |
| 1919 |
$input = str_replace( [ '<![CDATA[', ']]>' ], '', $input ); |
| 1920 |
if ( $unslash ) { |
| 1921 |
$output = wp_unslash( sanitize_post_field( $field, $input, 0, 'db' ) ); |
| 1922 |
} else { |
| 1923 |
$output = $input; |
| 1924 |
} |
| 1925 |
return $output; |
| 1926 |
} |
| 1927 |
|
| 1928 |
/** |
| 1929 |
* Returns an error text string. |
| 1930 |
* |
| 1931 |
* @param int $id |
| 1932 |
* @return string |
| 1933 |
*/ |
| 1934 |
public function error_codes( $id = 0 ) { |
| 1935 |
$text_strings = array( |
| 1936 |
0 => __( 'There was an unknown error.', 'starter-sites' ), |
| 1937 |
1 => __( 'The requested import file could not be found.', 'starter-sites' ), |
| 1938 |
2 => __( 'The required theme could not be installed.', 'starter-sites' ), |
| 1939 |
3 => __( 'A required plugin could not be installed.', 'starter-sites' ), |
| 1940 |
4 => __( 'This is not a valid starter site.', 'starter-sites' ), |
| 1941 |
); |
| 1942 |
return $text_strings[$id]; |
| 1943 |
} |
| 1944 |
|
| 1945 |
/** |
| 1946 |
* Include files. |
| 1947 |
*/ |
| 1948 |
public function includes() { |
| 1949 |
require_once STARTER_SITES_PATH . 'inc/mapping.php'; |
| 1950 |
} |
| 1951 |
|
| 1952 |
public function __construct() { |
| 1953 |
$this->includes(); |
| 1954 |
} |
| 1955 |
|
| 1956 |
} |
| 1957 |
|