| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer; |
| 4 |
|
| 5 |
use EbStyleHandler; |
| 6 |
use Exception; |
| 7 |
use Templately\Utils\Base; |
| 8 |
use Templately\Utils\Helper; |
| 9 |
use Templately\Utils\Installer; |
| 10 |
use Templately\Utils\Options; |
| 11 |
|
| 12 |
class FullSiteImport extends Base { |
| 13 |
use LogHelper; |
| 14 |
|
| 15 |
const SESSION_OPTION_KEY = 'templately_import_session'; |
| 16 |
public $manifest; |
| 17 |
protected $export; |
| 18 |
|
| 19 |
private $version = '1.0.0'; |
| 20 |
|
| 21 |
public $session_id; |
| 22 |
public $dir_path; |
| 23 |
protected $filePath; |
| 24 |
protected $tmp_dir = null; |
| 25 |
protected $dev_mode = false; |
| 26 |
protected $api_key = ''; |
| 27 |
public $request_params = []; |
| 28 |
protected $documents_data = []; |
| 29 |
protected $dependency_data = []; |
| 30 |
|
| 31 |
public function __construct() { |
| 32 |
$this->dev_mode = defined( 'TEMPLATELY_DEV' ) && TEMPLATELY_DEV; |
| 33 |
$this->api_key = Options::get_instance()->get( 'api_key' ); |
| 34 |
|
| 35 |
add_action( 'wp_ajax_templately_import_settings', [ $this, 'import_settings' ] ); |
| 36 |
add_action( 'wp_ajax_templately_pack_import', [ $this, 'import' ] ); |
| 37 |
|
| 38 |
if ( $this->dev_mode ) { |
| 39 |
add_filter( 'http_request_host_is_external', '__return_true' ); |
| 40 |
add_filter( 'http_request_args', function ( $args ) { |
| 41 |
$args['sslverify'] = false; |
| 42 |
|
| 43 |
return $args; |
| 44 |
} ); |
| 45 |
} |
| 46 |
add_action( 'eb_frontend_assets', [ $this, 'enqueue_template_assets' ], 10, 2 ); |
| 47 |
add_filter( 'the_content', [$this, 'filter_content'], 10 ,1); |
| 48 |
} |
| 49 |
|
| 50 |
public function import_settings() { |
| 51 |
if( ! current_user_can( 'edit_posts' ) ) { |
| 52 |
wp_send_json_error( __( 'Unauthorized action.', 'templately' ) ); |
| 53 |
} |
| 54 |
|
| 55 |
$data = wp_unslash( $_POST ); |
| 56 |
|
| 57 |
// Upload the logo and add its URL to the data |
| 58 |
$logo_url = $this->upload_logo(); |
| 59 |
if ($logo_url !== null) { |
| 60 |
$data['logo'] = $logo_url; |
| 61 |
} |
| 62 |
|
| 63 |
wp_send_json_success( update_option( self::SESSION_OPTION_KEY, $data ) ); |
| 64 |
} |
| 65 |
|
| 66 |
private function update_session_data( $data ): bool { |
| 67 |
$old_data = get_option( self::SESSION_OPTION_KEY, [] ); |
| 68 |
|
| 69 |
return update_option( self::SESSION_OPTION_KEY, wp_parse_args( $data, $old_data ) ); |
| 70 |
} |
| 71 |
|
| 72 |
public function get_session_data(): array { |
| 73 |
$data = get_option( self::SESSION_OPTION_KEY, [] ); |
| 74 |
|
| 75 |
$options = []; |
| 76 |
|
| 77 |
if ( is_array( $data ) && ! empty( $data ) ) { |
| 78 |
foreach ( $data as $key => $value ) { |
| 79 |
$json = json_decode( $value, true ); |
| 80 |
$options[ $key ] = $json !== null ? $json : $value; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return $options; |
| 85 |
} |
| 86 |
|
| 87 |
private function clear_session_data(): bool { |
| 88 |
return delete_site_option( self::SESSION_OPTION_KEY ); |
| 89 |
} |
| 90 |
|
| 91 |
public function import() { |
| 92 |
if ( ! $this->dev_mode && ! wp_doing_ajax() ) { |
| 93 |
exit; |
| 94 |
} |
| 95 |
|
| 96 |
header( "Cache-Control: no-store, no-cache" ); |
| 97 |
header( 'Content-Type: text/event-stream, charset=UTF-8' ); |
| 98 |
header( "Connection: Keep-Alive" ); |
| 99 |
|
| 100 |
if ( $GLOBALS['is_nginx'] ) { |
| 101 |
header( 'X-Accel-Buffering: no' ); |
| 102 |
header( 'Content-Encoding: none' ); |
| 103 |
} |
| 104 |
|
| 105 |
register_shutdown_function( [ $this, 'register_shutdown' ] ); |
| 106 |
|
| 107 |
// Ignore user aborts and allow the script |
| 108 |
// to run forever |
| 109 |
ignore_user_abort(true); |
| 110 |
|
| 111 |
// Time to run the import! |
| 112 |
set_time_limit( 0 ); |
| 113 |
|
| 114 |
flush(); |
| 115 |
wp_ob_end_flush_all(); |
| 116 |
|
| 117 |
try { |
| 118 |
// TODO: Need to check if user is connected or not |
| 119 |
|
| 120 |
$this->request_params = $this->get_session_data(); |
| 121 |
|
| 122 |
$_id = isset( $this->request_params['id'] ) ? (int) $this->request_params['id'] : null; |
| 123 |
|
| 124 |
if ( $_id === null ) { |
| 125 |
$this->throw( __( 'Invalid Pack ID.', 'templately' ) ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Check Writing Permission |
| 130 |
*/ |
| 131 |
$this->check_writing_permission(); |
| 132 |
|
| 133 |
/** |
| 134 |
* Download the zip |
| 135 |
*/ |
| 136 |
$this->download_zip( $_id ); |
| 137 |
|
| 138 |
/** |
| 139 |
* Reading Manifest File |
| 140 |
*/ |
| 141 |
$this->read_manifest(); |
| 142 |
|
| 143 |
/** |
| 144 |
* Version Check |
| 145 |
*/ |
| 146 |
if ( ! empty( $this->manifest['version'] ) && version_compare( $this->manifest['version'], $this->version, '>' ) ) { |
| 147 |
/** |
| 148 |
* FIXME: The message should be re-written (by content/support team). |
| 149 |
*/ |
| 150 |
$this->throw( __( 'Please update the templately plugin.', 'templately' ) ); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Checking & Installing Plugin Dependencies |
| 155 |
*/ |
| 156 |
$this->install_dependencies(); |
| 157 |
|
| 158 |
/** |
| 159 |
* Should Revert Old Data |
| 160 |
*/ |
| 161 |
$this->revert(); |
| 162 |
|
| 163 |
/** |
| 164 |
* Platform Based Templates Import |
| 165 |
*/ |
| 166 |
$this->start_content_import(); |
| 167 |
} catch ( Exception $e ) { |
| 168 |
$this->sse_message( [ |
| 169 |
'action' => 'error', |
| 170 |
'status' => 'error', |
| 171 |
'type' => "error", |
| 172 |
'title' => __("Oops!", "templately"), |
| 173 |
'message' => $e->getMessage() |
| 174 |
] ); |
| 175 |
} |
| 176 |
|
| 177 |
// TODO: cleanup |
| 178 |
$this->clear_session_data(); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* @throws Exception |
| 183 |
*/ |
| 184 |
private function throw( $message, $code = 0 ) { |
| 185 |
if ( $this->dev_mode ) { |
| 186 |
error_log( print_r( $message, 1 ) ); |
| 187 |
} |
| 188 |
throw new Exception( $message ); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* @throws Exception |
| 193 |
*/ |
| 194 |
private function check_writing_permission() { |
| 195 |
$upload_dir = wp_upload_dir(); |
| 196 |
|
| 197 |
if ( ! is_writable( $upload_dir['basedir'] ) ) { |
| 198 |
$this->throw( __( 'Upload directory is not writable.', 'templately' ) ); |
| 199 |
} |
| 200 |
|
| 201 |
$this->tmp_dir = trailingslashit( $upload_dir['basedir'] ) . 'templately' . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR; |
| 202 |
|
| 203 |
if ( ! is_dir( $this->tmp_dir ) ) { |
| 204 |
wp_mkdir_p( $this->tmp_dir ); |
| 205 |
} |
| 206 |
|
| 207 |
$this->sse_log( 'writing_permission_check', __( 'Permission Passed', 'templately' ), 100 ); |
| 208 |
} |
| 209 |
|
| 210 |
private function get_api_url( $id ): string { |
| 211 |
return $this->dev_mode ? 'https://app.templately.dev/api/v1/import/pack/' . $id : 'https://app.templately.com/api/v1/import/pack/' . $id; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* @throws Exception |
| 216 |
*/ |
| 217 |
private function download_zip( $id ) { |
| 218 |
// $this->sse_log( 'download', __( 'Downloading Template Pack', 'templately' ), 1 ); |
| 219 |
$response = wp_remote_get( $this->get_api_url( $id ), [ |
| 220 |
'timeout' => 30, |
| 221 |
'headers' => [ |
| 222 |
'Authorization' => 'Bearer ' . $this->api_key, |
| 223 |
'x-templately-ip' => Helper::get_ip(), |
| 224 |
'x-templately-url' => home_url( '/' ) |
| 225 |
] |
| 226 |
] ); |
| 227 |
|
| 228 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 229 |
$content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
| 230 |
|
| 231 |
if ( is_wp_error( $response ) ) { |
| 232 |
$this->throw( __( 'Template pack download failed', 'templately' ) . $response->get_error_message() ); |
| 233 |
} |
| 234 |
else if ($response_code != 200) { |
| 235 |
if (strpos($content_type, 'application/json') !== false) { |
| 236 |
// Retrieve Data from Response Body. |
| 237 |
$response_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 238 |
|
| 239 |
// If the response body is JSON and it contains an error, throw an exception with the error message |
| 240 |
if (isset($response_body['status']) && $response_body['status'] === 'error') { |
| 241 |
$this->throw( $response_body['message'] ); |
| 242 |
} |
| 243 |
} |
| 244 |
$this->throw( __( 'Template pack download failed with response code: ', 'templately' ) . $response_code ); |
| 245 |
} |
| 246 |
|
| 247 |
$this->sse_log( 'download', __( 'Template is getting ready', 'templately' ), 57 ); |
| 248 |
|
| 249 |
$session_id = uniqid(); |
| 250 |
$this->dir_path = $this->tmp_dir . $session_id . DIRECTORY_SEPARATOR; |
| 251 |
$this->filePath = $this->tmp_dir . "{$session_id}.zip"; |
| 252 |
$this->session_id = $session_id; |
| 253 |
|
| 254 |
$this->update_session_data( [ 'session_id' => $this->session_id ] ); |
| 255 |
|
| 256 |
if ( file_put_contents( $this->filePath, $response['body'] ) ) { // phpcs:ignore |
| 257 |
$this->sse_log( 'download', __( 'Template is getting ready', 'templately' ), 100 ); |
| 258 |
|
| 259 |
$this->unzip(); |
| 260 |
} else { |
| 261 |
$this->throw( __( 'Downloading Failed. Please try again', 'templately' ) ); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* @throws Exception |
| 267 |
*/ |
| 268 |
protected function unzip() { |
| 269 |
if ( ! WP_Filesystem() ) { |
| 270 |
$this->throw( __( 'WP_Filesystem cannot be initialized', 'templately' ) ); |
| 271 |
} |
| 272 |
|
| 273 |
if(defined('TEMPLATELY_ZIP_ARCHIVE') && TEMPLATELY_ZIP_ARCHIVE){ |
| 274 |
$unzip = $this->unzip_file( $this->filePath, $this->dir_path ); |
| 275 |
} |
| 276 |
else{ |
| 277 |
$unzip = unzip_file( $this->filePath, $this->dir_path ); |
| 278 |
} |
| 279 |
|
| 280 |
if ( is_wp_error( $unzip ) ) { |
| 281 |
$this->throw( sprintf( __( 'Unzipping failed: %s', 'templately' ), $unzip->get_error_message() ) ); |
| 282 |
} |
| 283 |
|
| 284 |
if ( $unzip ) { |
| 285 |
unlink( $this->filePath ); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Unzip a specified ZIP file to a location on the Filesystem. |
| 291 |
* |
| 292 |
* @param string $file Full path and filename of ZIP archive. |
| 293 |
* @param string $to Full path on the filesystem to extract archive to. |
| 294 |
* @return true|WP_Error True on success, WP_Error on failure. |
| 295 |
*/ |
| 296 |
function unzip_file($file, $to) { |
| 297 |
$zip = new \ZipArchive; |
| 298 |
|
| 299 |
$res = $zip->open($file); |
| 300 |
if ($res === TRUE) { |
| 301 |
$zip->extractTo($to); |
| 302 |
$zip->close(); |
| 303 |
|
| 304 |
return true; |
| 305 |
} else { |
| 306 |
return new \WP_Error('Could not unzip file: ' . $zip->getStatusString()); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* @throws Exception |
| 312 |
*/ |
| 313 |
private function read_manifest() { |
| 314 |
$manifest_content = file_get_contents( $this->dir_path . DIRECTORY_SEPARATOR . 'manifest.json' ); |
| 315 |
if ( empty( $manifest_content ) ) { |
| 316 |
$this->throw( __( 'Cannot be imported, as the manifest file is corrupted', 'templately' ) ); |
| 317 |
} |
| 318 |
|
| 319 |
$this->manifest = json_decode( $manifest_content, true ); |
| 320 |
$this->removeLog( 'temp' ); |
| 321 |
|
| 322 |
// TODO: Read & Broadcast the LOG for waiting list |
| 323 |
// $this->sse_log( 'plugin', 'Installing required plugins', '--', 'updateLog', 'processing' ); |
| 324 |
// // $this->sse_log( 'extra-content', 'Import Extra Contents (i.e: Forms)', '--', 'updateLog', 'processing' ); |
| 325 |
// $this->sse_log( 'templates', 'Import Templates (i.e: Header, Footer etc)', '--', 'updateLog', 'processing' ); |
| 326 |
// // $this->sse_log( 'content', 'Import Pages, Posts etc', '--', 'updateLog', 'processing' ); |
| 327 |
// $this->sse_log( 'wp-content', 'Importing Pages, Posts, Navigation, etc', '--', 'updateLog', 'processing' ); |
| 328 |
// $this->sse_log( 'finalize', 'Finalizing Your Imports', '--', 'updateLog', 'processing' ); |
| 329 |
} |
| 330 |
|
| 331 |
private function skipped_plugin(): bool { |
| 332 |
return empty( $this->request_params['plugins'] ) || ! is_array( $this->request_params['plugins'] ); |
| 333 |
} |
| 334 |
|
| 335 |
private function install_dependencies() { |
| 336 |
if ( ! empty( $this->request_params['theme'] ) && is_array( $this->request_params['theme'] ) ) { |
| 337 |
// activate theme |
| 338 |
$theme = $this->request_params['theme']; |
| 339 |
|
| 340 |
$this->before_install_hook(); |
| 341 |
|
| 342 |
if (isset($theme['stylesheet'])) { |
| 343 |
// do_action('before_theme_activation', $theme); // Trigger action before theme activation |
| 344 |
$this->sse_log( 'theme', 'Installing and activating theme: ' . $theme['name'], 0 ); |
| 345 |
$plugin_status = Installer::get_instance()->install_and_activate_theme( $theme['stylesheet'] ); |
| 346 |
|
| 347 |
if (!$plugin_status['success']) { |
| 348 |
$this->sse_message( [ |
| 349 |
'action' => 'updateLog', |
| 350 |
'status' => 'error', |
| 351 |
'message' => "Failed to activate theme: " . $theme['name'], |
| 352 |
'type' => "theme", |
| 353 |
'progress' => 0 |
| 354 |
] ); |
| 355 |
$this->dependency_data['theme'] = [ |
| 356 |
'success' => false, |
| 357 |
'name' => $theme['name'], |
| 358 |
'slug' => $theme['stylesheet'], |
| 359 |
'message' => $plugin_status['message'] ?? '' |
| 360 |
]; |
| 361 |
} else { |
| 362 |
// do_action('after_theme_activation', $theme); // Trigger action after theme activation |
| 363 |
|
| 364 |
$this->sse_message( [ |
| 365 |
'action' => 'updateLog', |
| 366 |
'status' => 'complete', |
| 367 |
'message' => "Activated theme: " . $theme['name'], |
| 368 |
'type' => "theme", |
| 369 |
'progress' => 100 |
| 370 |
] ); |
| 371 |
$this->dependency_data['theme'] = [ |
| 372 |
'success' => true, |
| 373 |
'name' => $theme['name'], |
| 374 |
'slug' => $theme['stylesheet'], |
| 375 |
'message' => $plugin_status['message'] ?? '' |
| 376 |
]; |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
$this->after_install_hook(); |
| 381 |
} |
| 382 |
else { |
| 383 |
$this->removeLog( 'theme' ); |
| 384 |
} |
| 385 |
if ( ! empty( $this->request_params['plugins'] ) && is_array( $this->request_params['plugins'] ) ) { |
| 386 |
// $this->sse_log( 'plugin', 'Installing Plugins', 1 ); |
| 387 |
$total_plugin = count( $this->request_params['plugins'] ); |
| 388 |
|
| 389 |
$total_plugin_installed = $total_plugin; |
| 390 |
$_installed_plugins = 0; |
| 391 |
|
| 392 |
$this->before_install_hook(); |
| 393 |
|
| 394 |
foreach ( $this->request_params['plugins'] as $dependency ) { |
| 395 |
$this->sse_log( 'plugin', 'Installing required plugins: ' . $dependency['name'], floor( ( 100 * $_installed_plugins / $total_plugin ) ) ); |
| 396 |
|
| 397 |
$dependency['slug'] = $dependency['plugin_original_slug']; |
| 398 |
$plugin_status = Installer::get_instance()->install( $dependency ); |
| 399 |
|
| 400 |
if ( ! $plugin_status['success'] ) { |
| 401 |
$this->sse_message( [ |
| 402 |
'position' => 'plugin', |
| 403 |
'action' => 'updateLog', |
| 404 |
'status' => 'error', |
| 405 |
'message' => 'Installation Failed: ' . $dependency['name'] . ' (' . ( $plugin_status['message'] ?? '' ) . ')', |
| 406 |
'type' => "plugin_{$dependency['plugin_original_slug']}", |
| 407 |
'progress' => 0 |
| 408 |
] ); |
| 409 |
|
| 410 |
if(isset($dependency['mustHave']) && $dependency['mustHave']){ |
| 411 |
$this->removeLog( 'plugin' ); |
| 412 |
$this->throw( 'Installation Failed: ' . $dependency['name'] . ' (' . ( $plugin_status['message'] ?? '' ) . ')' ); |
| 413 |
} |
| 414 |
|
| 415 |
$this->dependency_data['plugins']['failed'][] = [ |
| 416 |
'name' => $dependency['name'], |
| 417 |
'slug' => $dependency['slug'], |
| 418 |
'link' => $dependency['link'], |
| 419 |
'message' => $plugin_status['message'] ?? '' |
| 420 |
]; |
| 421 |
} else { |
| 422 |
$_installed_plugins++; |
| 423 |
$total_plugin_installed--; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
$this->after_install_hook(); |
| 428 |
|
| 429 |
$this->sse_message( [ |
| 430 |
'action' => 'updateLog', |
| 431 |
'status' => 'complete', |
| 432 |
'message' => "Installed required plugins ($_installed_plugins/$total_plugin)", |
| 433 |
'type' => "plugin", |
| 434 |
'progress' => 100 |
| 435 |
] ); |
| 436 |
$this->dependency_data['plugins']['total'] = $total_plugin; |
| 437 |
$this->dependency_data['plugins']['succeed'] = $_installed_plugins; |
| 438 |
} else { |
| 439 |
$this->removeLog( 'plugin' ); |
| 440 |
} |
| 441 |
|
| 442 |
// $this->sse_log( 'plugin', 'Skipped Installing Plugins', '--', 'updateLog', 'skipped' ); |
| 443 |
} |
| 444 |
|
| 445 |
private function before_install_hook() { |
| 446 |
remove_all_actions( 'wp_loaded' ); |
| 447 |
remove_all_actions( 'after_setup_theme' ); |
| 448 |
remove_all_actions( 'plugins_loaded' ); |
| 449 |
remove_all_actions( 'init' ); |
| 450 |
|
| 451 |
// making sure so that no redirection happens during plugin installation and hooks triggered bellow. |
| 452 |
add_filter('wp_redirect', '__return_false', 999); |
| 453 |
} |
| 454 |
|
| 455 |
private function after_install_hook() { |
| 456 |
do_action( 'wp_loaded' ); |
| 457 |
do_action( 'after_setup_theme' ); |
| 458 |
do_action( 'plugins_loaded' ); |
| 459 |
do_action( 'init' ); |
| 460 |
} |
| 461 |
|
| 462 |
/** |
| 463 |
* @throws Exception |
| 464 |
*/ |
| 465 |
private function start_content_import() { |
| 466 |
add_filter('upload_mimes', array($this, 'allow_svg_upload')); |
| 467 |
add_filter('elementor/files/allow_unfiltered_upload', '__return_true'); |
| 468 |
|
| 469 |
$import = new Import( $this ); |
| 470 |
$imported_data = $import->run(); |
| 471 |
|
| 472 |
$this->sse_message( [ |
| 473 |
'type' => 'complete', |
| 474 |
'action' => 'complete', |
| 475 |
'results' => $this->normalize_imported_data( $imported_data ) |
| 476 |
] ); |
| 477 |
} |
| 478 |
|
| 479 |
private function normalize_imported_data( $data ) { |
| 480 |
$templates = ! empty( $data['templates']['succeed'] ) ? count( $data['templates']['succeed'] ) : 0; |
| 481 |
$template_types = ! empty( $data['templates']['template_types'] ) ? $data['templates']['template_types'] : []; |
| 482 |
|
| 483 |
$post_types = []; |
| 484 |
$content_templates = []; |
| 485 |
if ( ! empty( $data['content'] ) && is_array( $data['content'] ) ) { |
| 486 |
foreach ( $data['content'] as $type => $type_data ) { |
| 487 |
$content_templates[ $type ] = ! empty( $type_data['succeed'] ) ? count( $type_data['succeed'] ) : 0; |
| 488 |
$post_types[] = $this->get_post_type_label_by_slug($type); |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
$contents = []; |
| 493 |
if ( ! empty( $data['wp-content'] ) && is_array( $data['wp-content'] ) ) { |
| 494 |
foreach ( $data['wp-content'] as $type => $type_data ) { |
| 495 |
$contents[ $type ] = ! empty( $type_data['succeed'] ) ? count( $type_data['succeed'] ) : 0; |
| 496 |
if(!in_array($type, ['wp_navigation', 'nav_menu_item'])){ |
| 497 |
$post_types[] = $this->get_post_type_label_by_slug($type); |
| 498 |
} |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
Helper::log( $data ); |
| 503 |
|
| 504 |
return [ |
| 505 |
'templates' => $templates, |
| 506 |
'contents' => $content_templates, |
| 507 |
'wp-content' => $contents, |
| 508 |
'post_types' => $post_types, |
| 509 |
'template_types' => $template_types, |
| 510 |
'dependency_data' => $this->dependency_data, |
| 511 |
]; |
| 512 |
} |
| 513 |
|
| 514 |
public function get_request_params() { |
| 515 |
return $this->request_params; |
| 516 |
} |
| 517 |
|
| 518 |
private function revert() { |
| 519 |
// $request = $this->get_request_params(); |
| 520 |
// if ( isset( $request['revert'] ) && $request['revert'] ) { |
| 521 |
// // TODO: Implement the Revert Process. |
| 522 |
// } |
| 523 |
} |
| 524 |
|
| 525 |
public function redirect_for_archives( $link, $post_id ) { |
| 526 |
$archive_settings = get_option( 'templately_post_archive' ); |
| 527 |
if ( ! empty( $archive_settings ) && intval( $archive_settings['post_id'] ) === intval( $post_id ) ) { |
| 528 |
$link = str_replace( $post_id, $archive_settings['archive_id'], $link ); |
| 529 |
} |
| 530 |
|
| 531 |
return $link; |
| 532 |
} |
| 533 |
|
| 534 |
public function enqueue_template_assets( $path, $url ) { |
| 535 |
$using_templately_builder = get_query_var( 'using_templately_template' ); |
| 536 |
if ( $using_templately_builder && function_exists( 'templately' ) ) { |
| 537 |
$template_locations = [ 'header', 'footer', 'archive', 'single' ]; |
| 538 |
foreach ( $template_locations as $location ) { |
| 539 |
$template = templately()->theme_builder::$conditions_manager->get_templates_by_location( $location ); |
| 540 |
if ( empty( $template ) ) { |
| 541 |
return; |
| 542 |
} |
| 543 |
$template = array_pop( $template ); |
| 544 |
if ( $template->platform == 'gutenberg' ) { |
| 545 |
$template = is_array( $template ) ? array_pop( $template ) : $template; |
| 546 |
if ( ! file_exists( $path . 'eb-style-' . $template->get_main_id() . '.min.css' ) ) { |
| 547 |
$st = EbStyleHandler::init(); |
| 548 |
$post = get_post( $template->get_main_id() ); |
| 549 |
$st->eb_write_css_from_content( $post, $post->ID, parse_blocks( $post->post_content ) ); |
| 550 |
} |
| 551 |
wp_enqueue_style( 'templately-' . $location . '-' . $template->get_main_id(), $url . 'eb-style-' . $template->get_main_id() . '.min.css', [], substr( md5( microtime( true ) ), 0, 10 ) ); |
| 552 |
} |
| 553 |
} |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
public function upload_logo() { |
| 558 |
// Check if the upload file exists |
| 559 |
if (isset($_FILES['logo'])) { |
| 560 |
// Require the needed files if not already loaded |
| 561 |
if (!function_exists('wp_handle_upload')) { |
| 562 |
require_once(ABSPATH . 'wp-admin/includes/file.php'); |
| 563 |
} |
| 564 |
|
| 565 |
// Manually upload the file |
| 566 |
$uploadedfile = $_FILES['logo']; |
| 567 |
$upload_overrides = array('test_form' => false); |
| 568 |
$movefile = wp_handle_upload($uploadedfile, $upload_overrides); |
| 569 |
|
| 570 |
if ($movefile && !isset($movefile['error'])) { |
| 571 |
// The file was uploaded successfully, now we need to resize it |
| 572 |
if (!function_exists('wp_get_image_editor')) { |
| 573 |
require_once(ABSPATH . 'wp-admin/includes/image.php'); |
| 574 |
} |
| 575 |
|
| 576 |
$filetype = wp_check_filetype($movefile['file']); |
| 577 |
|
| 578 |
if ($filetype['ext'] == 'jpg' || $filetype['ext'] == 'jpeg' || $filetype['ext'] == 'png') { |
| 579 |
$image = wp_get_image_editor($movefile['file']); |
| 580 |
if (!is_wp_error($image)) { |
| 581 |
$size = $image->get_size(); |
| 582 |
if ($size['width'] > 200 || $size['height'] > 80) { |
| 583 |
$image->resize(200, 80, false); |
| 584 |
$image->save($movefile['file']); |
| 585 |
} |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
// Prepare an array of post data for the attachment |
| 590 |
$attachment = array( |
| 591 |
'guid' => $movefile['url'], |
| 592 |
'post_mime_type' => $movefile['type'], |
| 593 |
'post_title' => preg_replace('/\.[^.]+$/', '', basename($movefile['file'])), |
| 594 |
'post_content' => '', |
| 595 |
'post_status' => 'inherit' |
| 596 |
); |
| 597 |
|
| 598 |
// Insert the attachment |
| 599 |
$attach_id = wp_insert_attachment($attachment, $movefile['file']); |
| 600 |
|
| 601 |
// Generate the metadata for the attachment and update the database record |
| 602 |
$attach_data = wp_generate_attachment_metadata($attach_id, $movefile['file']); |
| 603 |
wp_update_attachment_metadata($attach_id, $attach_data); |
| 604 |
|
| 605 |
// Return the URL of the uploaded file |
| 606 |
return json_encode( [ |
| 607 |
'id' => $attach_id, |
| 608 |
'url' => $movefile['url'] |
| 609 |
]); |
| 610 |
} else { |
| 611 |
/** |
| 612 |
* Error generated by _wp_handle_upload() |
| 613 |
* @see _wp_handle_upload() in wp-admin/includes/file.php |
| 614 |
*/ |
| 615 |
// return $movefile['error']; |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
return null; |
| 620 |
} |
| 621 |
|
| 622 |
public function allow_svg_upload($mimes) { |
| 623 |
// Allow SVG |
| 624 |
$mimes['svg'] = 'image/svg+xml'; |
| 625 |
return $mimes; |
| 626 |
} |
| 627 |
|
| 628 |
public function register_shutdown(){ |
| 629 |
$status = connection_status(); |
| 630 |
$last_error = error_get_last(); |
| 631 |
if ($status != CONNECTION_NORMAL && $last_error && $last_error['type'] === E_ERROR) { |
| 632 |
if (defined('WP_DEBUG') && WP_DEBUG && !empty($last_error['message'])) { |
| 633 |
$full_message = $last_error['message']; |
| 634 |
|
| 635 |
// Split the message at the first newline to remove the stack trace |
| 636 |
$message_parts = preg_split('/\n/', $full_message); |
| 637 |
|
| 638 |
// The error message is the first part |
| 639 |
$error_message = $message_parts[0]; |
| 640 |
} else { |
| 641 |
// Generic error message |
| 642 |
$error_message = sprintf(__("It seems we're experiencing technical difficulties. Please try again or contact <a href='%s' target='_blank'>support</a>.", "templately"), 'https://wpdeveloper.com/support'); |
| 643 |
} |
| 644 |
|
| 645 |
// Handle the error, e.g. log it or display a message to the user |
| 646 |
$this->sse_message( [ |
| 647 |
'action' => 'error', |
| 648 |
'status' => 'error', |
| 649 |
'type' => "error", |
| 650 |
'title' => __("Oops!", "templately"), |
| 651 |
'message' => $error_message, |
| 652 |
// 'position' => 'plugin', |
| 653 |
// 'progress' => '--', |
| 654 |
] ); |
| 655 |
} |
| 656 |
|
| 657 |
Helper::log( "Shutdown:....." ); |
| 658 |
Helper::log( "connection_status: " . $this->getConnectionStatusText() ); |
| 659 |
Helper::log( $last_error ); |
| 660 |
} |
| 661 |
|
| 662 |
protected function getConnectionStatusText() { |
| 663 |
$status = connection_status(); |
| 664 |
switch ($status) { |
| 665 |
case CONNECTION_NORMAL: |
| 666 |
return "Normal"; |
| 667 |
case CONNECTION_ABORTED: |
| 668 |
return "Aborted"; |
| 669 |
case CONNECTION_TIMEOUT: |
| 670 |
return "Timeout"; |
| 671 |
default: |
| 672 |
return "Unknown"; |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
protected function get_post_type_label_by_slug($slug) { |
| 677 |
$post_type_obj = get_post_type_object($slug); |
| 678 |
if($post_type_obj) { |
| 679 |
return $post_type_obj->label; |
| 680 |
} |
| 681 |
return null; |
| 682 |
} |
| 683 |
|
| 684 |
public function filter_content( $content ) { |
| 685 |
if(is_singular()){ |
| 686 |
global $post; |
| 687 |
if(!empty($post) && $post->post_type === 'templately_library'){ |
| 688 |
if(in_array(get_post_meta($post->ID, '_templately_template_type', true), ['header', 'footer'])){ |
| 689 |
return ''; |
| 690 |
} |
| 691 |
} |
| 692 |
} |
| 693 |
return $content; |
| 694 |
} |
| 695 |
} |