compatibility
1 year ago
class-astra-sites-elementor-images.php
6 years ago
class-astra-sites-elementor-pages.php
2 years ago
class-astra-sites-error-handler.php
2 years ago
class-astra-sites-file-system.php
2 years ago
class-astra-sites-importer-log.php
2 years ago
class-astra-sites-importer.php
1 year ago
class-astra-sites-nps-notice.php
1 year ago
class-astra-sites-page.php
1 year ago
class-astra-sites-update.php
1 year ago
class-astra-sites-utils.php
2 years ago
class-astra-sites-white-label.php
2 years ago
class-astra-sites-wp-cli.php
2 years ago
class-astra-sites.php
1 year ago
functions.php
1 year ago
class-astra-sites-importer-log.php
538 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Astra Sites Importer Log |
| 4 | * |
| 5 | * @since 1.1.0 |
| 6 | * @package Astra Sites |
| 7 | */ |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | if ( ! class_exists( 'Astra_Sites_Importer_Log' ) ) : |
| 14 | |
| 15 | /** |
| 16 | * Astra Sites Importer |
| 17 | */ |
| 18 | class Astra_Sites_Importer_Log { |
| 19 | |
| 20 | /** |
| 21 | * Instance |
| 22 | * |
| 23 | * @since 1.1.0 |
| 24 | * @var (Object) Class object |
| 25 | */ |
| 26 | private static $instance = null; |
| 27 | |
| 28 | /** |
| 29 | * Log File |
| 30 | * |
| 31 | * @since 1.1.0 |
| 32 | * @var (Object) Class object |
| 33 | */ |
| 34 | private static $log_file = null; |
| 35 | |
| 36 | /** |
| 37 | * Set Instance |
| 38 | * |
| 39 | * @since 1.1.0 |
| 40 | * |
| 41 | * @return object Class object. |
| 42 | */ |
| 43 | public static function get_instance() { |
| 44 | if ( ! isset( self::$instance ) ) { |
| 45 | self::$instance = new self(); |
| 46 | } |
| 47 | |
| 48 | return self::$instance; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Constructor. |
| 53 | * |
| 54 | * @since 1.1.0 |
| 55 | */ |
| 56 | private function __construct() { |
| 57 | |
| 58 | // Check file read/write permissions. |
| 59 | if ( current_user_can( 'edit_posts' ) ) { |
| 60 | add_action( 'admin_init', array( $this, 'has_file_read_write' ) ); |
| 61 | } |
| 62 | |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Check file read/write permissions and process. |
| 67 | * |
| 68 | * @since 1.1.0 |
| 69 | * @return null |
| 70 | */ |
| 71 | public function has_file_read_write() { |
| 72 | |
| 73 | $upload_dir = self::log_dir(); |
| 74 | |
| 75 | $file_created = Astra_Sites::get_instance()->get_filesystem()->put_contents( $upload_dir['path'] . 'index.html', '' ); |
| 76 | if ( ! $file_created ) { |
| 77 | add_action( 'admin_notices', array( $this, 'file_permission_notice' ) ); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | // Set log file. |
| 82 | self::set_log_file(); |
| 83 | |
| 84 | // Initial AJAX Import Hooks. |
| 85 | add_action( 'astra_sites_import_start', array( $this, 'start' ), 10, 2 ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * File Permission Notice |
| 90 | * |
| 91 | * @since 2.0.0 |
| 92 | * @return void |
| 93 | */ |
| 94 | public function file_permission_notice() { |
| 95 | $upload_dir = self::log_dir(); |
| 96 | $plugin_name = ASTRA_SITES_NAME; |
| 97 | if ( is_callable( 'Astra_Sites_White_Label::get_instance' ) ) { |
| 98 | $is_white_label = Astra_Sites_White_Label::get_instance()->is_white_labeled(); |
| 99 | $plugin_name = $is_white_label ? Astra_Sites_White_Label::get_instance()->get_white_label_name() : ASTRA_SITES_NAME; |
| 100 | } |
| 101 | /* translators: %1$s refers to the plugin name */ |
| 102 | $notice = sprintf( __( 'Required File Permissions to import the templates from %s are missing.', 'astra-sites' ), $plugin_name ); |
| 103 | ?> |
| 104 | <div class="notice notice-error astra-sites-must-notices astra-sites-file-permission-issue"> |
| 105 | <p><?php echo esc_html( $notice ); ?></p> |
| 106 | <?php if ( defined( 'FS_METHOD' ) ) { ?> |
| 107 | <p><?php esc_html_e( 'This is usually due to inconsistent file permissions.', 'astra-sites' ); ?></p> |
| 108 | <p><code><?php echo esc_html( $upload_dir['path'] ); ?></code></p> |
| 109 | <?php } else { ?> |
| 110 | <p><?php esc_html_e( 'You can easily update permissions by adding the following code into the wp-config.php file.', 'astra-sites' ); ?></p> |
| 111 | <p><code>define( 'FS_METHOD', 'direct' );</code></p> |
| 112 | <?php } ?> |
| 113 | </div> |
| 114 | <?php |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Add log file URL in UI response. |
| 119 | * |
| 120 | * @since 1.1.0 |
| 121 | */ |
| 122 | public static function add_log_file_url() { |
| 123 | |
| 124 | $upload_dir = self::log_dir(); |
| 125 | $upload_path = trailingslashit( $upload_dir['url'] ); |
| 126 | $file_abs_url = get_option( 'astra_sites_recent_import_log_file', self::$log_file ); |
| 127 | $file_url = $upload_path . basename( $file_abs_url ); |
| 128 | |
| 129 | return array( |
| 130 | 'abs_url' => $file_abs_url, |
| 131 | 'url' => $file_url, |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Current Time for log. |
| 137 | * |
| 138 | * @since 1.1.0 |
| 139 | * @return string Current time with time zone. |
| 140 | */ |
| 141 | public static function current_time() { |
| 142 | return gmdate( 'H:i:s' ) . ' ' . date_default_timezone_get(); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Import Start |
| 147 | * |
| 148 | * @since 1.1.0 |
| 149 | * @param array $data Import Data. |
| 150 | * @param string $demo_api_uri Import site API URL. |
| 151 | * @return void |
| 152 | */ |
| 153 | public function start( $data = array(), $demo_api_uri = '' ) { |
| 154 | |
| 155 | self::add( 'Started Import Process' ); |
| 156 | |
| 157 | self::add( '# System Details: ' ); |
| 158 | self::add( "Debug Mode \t\t: " . self::get_debug_mode() ); |
| 159 | self::add( "Operating System \t: " . self::get_os() ); |
| 160 | self::add( "Software \t\t: " . self::get_software() ); |
| 161 | self::add( "MySQL version \t\t: " . self::get_mysql_version() ); |
| 162 | self::add( "XML Reader \t\t: " . self::get_xmlreader_status() ); |
| 163 | self::add( "PHP Version \t\t: " . self::get_php_version() ); |
| 164 | self::add( "PHP Max Input Vars \t: " . self::get_php_max_input_vars() ); |
| 165 | self::add( "PHP Max Post Size \t: " . self::get_php_max_post_size() ); |
| 166 | self::add( "PHP Extension GD \t: " . self::get_php_extension_gd() ); |
| 167 | self::add( "PHP Max Execution Time \t: " . self::get_max_execution_time() ); |
| 168 | self::add( "Max Upload Size \t: " . size_format( wp_max_upload_size() ) ); |
| 169 | self::add( "Memory Limit \t\t: " . self::get_memory_limit() ); |
| 170 | self::add( "Timezone \t\t: " . self::get_timezone() ); |
| 171 | self::add( PHP_EOL . '-----' . PHP_EOL ); |
| 172 | self::add( 'Importing Started! - ' . self::current_time() ); |
| 173 | |
| 174 | self::add( '---' . PHP_EOL ); |
| 175 | self::add( 'WHY IMPORT PROCESS CAN FAIL? READ THIS - ' ); |
| 176 | self::add( 'https://wpastra.com/docs/?p=1314&utm_source=demo-import-panel&utm_campaign=import-error&utm_medium=wp-dashboard' . PHP_EOL ); |
| 177 | self::add( '---' . PHP_EOL ); |
| 178 | |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Get Log File |
| 183 | * |
| 184 | * @since 1.1.0 |
| 185 | * @return string log file URL. |
| 186 | */ |
| 187 | public static function get_log_file() { |
| 188 | return self::$log_file; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Log file directory |
| 193 | * |
| 194 | * @since 1.1.0 |
| 195 | * @param string $dir_name Directory Name. |
| 196 | * @return array Uploads directory array. |
| 197 | */ |
| 198 | public static function log_dir( $dir_name = 'astra-sites' ) { |
| 199 | |
| 200 | $upload_dir = wp_upload_dir(); |
| 201 | |
| 202 | // Build the paths. |
| 203 | $dir_info = array( |
| 204 | 'path' => $upload_dir['basedir'] . '/' . $dir_name . '/', |
| 205 | 'url' => $upload_dir['baseurl'] . '/' . $dir_name . '/', |
| 206 | ); |
| 207 | |
| 208 | // Create the upload dir if it doesn't exist. |
| 209 | if ( ! file_exists( $dir_info['path'] ) ) { |
| 210 | |
| 211 | // Create the directory. |
| 212 | wp_mkdir_p( $dir_info['path'] ); |
| 213 | |
| 214 | // Add an index file for security. |
| 215 | Astra_Sites::get_instance()->get_filesystem()->put_contents( $dir_info['path'] . 'index.html', '' ); |
| 216 | |
| 217 | // Add an .htaccess for security. |
| 218 | Astra_Sites::get_instance()->get_filesystem()->put_contents( $dir_info['path'] . '.htaccess', 'deny from all' ); |
| 219 | } |
| 220 | |
| 221 | return $dir_info; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Set log file |
| 226 | * |
| 227 | * @since 1.1.0 |
| 228 | */ |
| 229 | public static function set_log_file() { |
| 230 | |
| 231 | $upload_dir = self::log_dir(); |
| 232 | |
| 233 | $upload_path = trailingslashit( $upload_dir['path'] ); |
| 234 | |
| 235 | // File format e.g. 'import-31-Oct-2017-06-39-12-hashcode.log'. |
| 236 | self::$log_file = $upload_path . 'import-' . gmdate( 'd-M-Y-h-i-s' ) . '-' . wp_hash( 'starter-templates-log' ) . '.log'; |
| 237 | |
| 238 | if ( ! get_option( 'astra_sites_recent_import_log_file', false ) ) { |
| 239 | update_option( 'astra_sites_recent_import_log_file', self::$log_file, 'no' ); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Write content to a file. |
| 245 | * |
| 246 | * @since 1.1.0 |
| 247 | * @param string $content content to be saved to the file. |
| 248 | */ |
| 249 | public static function add( $content ) { |
| 250 | |
| 251 | if ( get_option( 'astra_sites_recent_import_log_file', false ) ) { |
| 252 | $log_file = get_option( 'astra_sites_recent_import_log_file', self::$log_file ); |
| 253 | } else { |
| 254 | $log_file = self::$log_file; |
| 255 | } |
| 256 | |
| 257 | if ( apply_filters( 'astra_sites_debug_logs', false ) ) { |
| 258 | error_log( $content ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- This is for the debug logs while importing. This is conditional and will not be logged in the debug.log file for normal users. |
| 259 | } |
| 260 | |
| 261 | |
| 262 | $existing_data = ''; |
| 263 | if ( file_exists( $log_file ) ) { |
| 264 | $existing_data = Astra_Sites::get_instance()->get_filesystem()->get_contents( $log_file ); |
| 265 | } |
| 266 | |
| 267 | // Style separator. |
| 268 | $separator = PHP_EOL; |
| 269 | |
| 270 | Astra_Sites::get_instance()->get_filesystem()->put_contents( $log_file, $existing_data . $separator . $content, FS_CHMOD_FILE ); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Debug Mode |
| 275 | * |
| 276 | * @since 1.1.0 |
| 277 | * @return string Enabled for Debug mode ON and Disabled for Debug mode Off. |
| 278 | */ |
| 279 | public static function get_debug_mode() { |
| 280 | if ( WP_DEBUG ) { |
| 281 | return __( 'Enabled', 'astra-sites' ); |
| 282 | } |
| 283 | |
| 284 | return __( 'Disabled', 'astra-sites' ); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Memory Limit |
| 289 | * |
| 290 | * @since 1.1.0 |
| 291 | * @return string Memory limit. |
| 292 | */ |
| 293 | public static function get_memory_limit() { |
| 294 | |
| 295 | $required_memory = '64M'; |
| 296 | $memory_limit_in_bytes_current = wp_convert_hr_to_bytes( WP_MEMORY_LIMIT ); |
| 297 | $memory_limit_in_bytes_required = wp_convert_hr_to_bytes( $required_memory ); |
| 298 | |
| 299 | if ( $memory_limit_in_bytes_current < $memory_limit_in_bytes_required ) { |
| 300 | return sprintf( |
| 301 | /* translators: %1$s Memory Limit, %2$s Recommended memory limit. */ |
| 302 | _x( 'Current memory limit %1$s. We recommend setting memory to at least %2$s.', 'Recommended Memory Limit', 'astra-sites' ), |
| 303 | WP_MEMORY_LIMIT, |
| 304 | $required_memory |
| 305 | ); |
| 306 | } |
| 307 | |
| 308 | return WP_MEMORY_LIMIT; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Timezone |
| 313 | * |
| 314 | * @since 1.1.0 |
| 315 | * @see https://codex.wordpress.org/Option_Reference/ |
| 316 | * |
| 317 | * @return string Current timezone. |
| 318 | */ |
| 319 | public static function get_timezone() { |
| 320 | $timezone = get_option( 'timezone_string' ); |
| 321 | |
| 322 | if ( ! $timezone ) { |
| 323 | return get_option( 'gmt_offset' ); |
| 324 | } |
| 325 | |
| 326 | return $timezone; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Operating System |
| 331 | * |
| 332 | * @since 1.1.0 |
| 333 | * @return string Current Operating System. |
| 334 | */ |
| 335 | public static function get_os() { |
| 336 | return PHP_OS; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Server Software |
| 341 | * |
| 342 | * @since 1.1.0 |
| 343 | * @return string Current Server Software. |
| 344 | */ |
| 345 | public static function get_software() { |
| 346 | return isset( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( $_SERVER['SERVER_SOFTWARE'] ) : ''; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * MySql Version |
| 351 | * |
| 352 | * @since 1.1.0 |
| 353 | * @return string Current MySql Version. |
| 354 | */ |
| 355 | public static function get_mysql_version() { |
| 356 | global $wpdb; |
| 357 | return $wpdb->db_version(); |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * XML Reader |
| 362 | * |
| 363 | * @since 1.2.8 |
| 364 | * @return string Current XML Reader status. |
| 365 | */ |
| 366 | public static function get_xmlreader_status() { |
| 367 | |
| 368 | if ( class_exists( 'XMLReader' ) ) { |
| 369 | return __( 'Yes', 'astra-sites' ); |
| 370 | } |
| 371 | |
| 372 | return __( 'No', 'astra-sites' ); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * PHP Version |
| 377 | * |
| 378 | * @since 1.1.0 |
| 379 | * @return string Current PHP Version. |
| 380 | */ |
| 381 | public static function get_php_version() { |
| 382 | if ( version_compare( PHP_VERSION, '5.4', '<' ) ) { |
| 383 | return _x( 'We recommend to use php 5.4 or higher', 'PHP Version', 'astra-sites' ); |
| 384 | } |
| 385 | return PHP_VERSION; |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * PHP Max Input Vars |
| 390 | * |
| 391 | * @since 1.1.0 |
| 392 | * @return string Current PHP Max Input Vars |
| 393 | */ |
| 394 | public static function get_php_max_input_vars() { |
| 395 | return ini_get( 'max_input_vars' ); // phpcs:disable PHPCompatibility.IniDirectives.NewIniDirectives.max_input_varsFound |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * PHP Max Post Size |
| 400 | * |
| 401 | * @since 1.1.0 |
| 402 | * @return string Current PHP Max Post Size |
| 403 | */ |
| 404 | public static function get_php_max_post_size() { |
| 405 | return ini_get( 'post_max_size' ); |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * PHP Max Execution Time |
| 410 | * |
| 411 | * @since 1.1.0 |
| 412 | * @return string Current Max Execution Time |
| 413 | */ |
| 414 | public static function get_max_execution_time() { |
| 415 | return ini_get( 'max_execution_time' ); |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * PHP GD Extension |
| 420 | * |
| 421 | * @since 1.1.0 |
| 422 | * @return string Current PHP GD Extension |
| 423 | */ |
| 424 | public static function get_php_extension_gd() { |
| 425 | if ( extension_loaded( 'gd' ) ) { |
| 426 | return __( 'Yes', 'astra-sites' ); |
| 427 | } |
| 428 | |
| 429 | return __( 'No', 'astra-sites' ); |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Display Data |
| 434 | * |
| 435 | * @since 2.0.0 |
| 436 | * @return void |
| 437 | */ |
| 438 | public function display_data() { |
| 439 | |
| 440 | $crons = _get_cron_array(); |
| 441 | $events = array(); |
| 442 | |
| 443 | if ( empty( $crons ) ) { |
| 444 | esc_html_e( 'You currently have no scheduled cron events.', 'astra-sites' ); |
| 445 | } |
| 446 | |
| 447 | foreach ( $crons as $time => $cron ) { |
| 448 | $keys = array_keys( $cron ); |
| 449 | $key = $keys[0]; |
| 450 | $events[ $key ] = $time; |
| 451 | } |
| 452 | |
| 453 | $expired = get_site_transient( 'astra-sites-import-check' ); |
| 454 | if ( $expired ) { |
| 455 | global $wpdb; |
| 456 | $transient = 'astra-sites-import-check'; |
| 457 | |
| 458 | $transient_timeout = $wpdb->get_col( |
| 459 | $wpdb->prepare( |
| 460 | "SELECT option_value |
| 461 | FROM $wpdb->options |
| 462 | WHERE option_name |
| 463 | LIKE %s", |
| 464 | '%_transient_timeout_' . $transient . '%' |
| 465 | ) |
| 466 | ); // WPCS: cache ok. // WPCS: db call ok. |
| 467 | |
| 468 | $older_date = $transient_timeout[0]; |
| 469 | $transient_status = 'Transient: Not Expired! Recheck in ' . human_time_diff( time(), $older_date ); |
| 470 | } else { |
| 471 | $transient_status = 'Transient: Starting.. Process for each 5 minutes.'; |
| 472 | } |
| 473 | $temp = get_site_option( 'astra-sites-batch-status-string', '' ); |
| 474 | $temp .= isset( $events['wp_astra_site_importer_cron'] ) ? '<br/>Batch: Recheck batch in ' . human_time_diff( time(), $events['wp_astra_site_importer_cron'] ) : '<br/>Batch: Not Started! Until the Transient expire.'; |
| 475 | |
| 476 | $upload_dir = self::get_instance()->log_dir(); |
| 477 | $list_files = list_files( $upload_dir['path'] ); |
| 478 | $backup_files = array(); |
| 479 | $log_files = array(); |
| 480 | foreach ( $list_files as $key => $file ) { |
| 481 | if ( strpos( $file, '.json' ) ) { |
| 482 | $backup_files[] = $file; |
| 483 | } |
| 484 | if ( strpos( $file, '.txt' ) ) { |
| 485 | $log_files[] = $file; |
| 486 | } |
| 487 | } |
| 488 | ?> |
| 489 | <table> |
| 490 | <tr> |
| 491 | <td> |
| 492 | <h2>Log Files</h2> |
| 493 | <ul> |
| 494 | <?php |
| 495 | foreach ( $log_files as $key => $file ) { |
| 496 | $file_name = basename( $file ); |
| 497 | $file = str_replace( $upload_dir['path'], $upload_dir['url'], $file ); |
| 498 | ?> |
| 499 | <li> |
| 500 | <a target="_blank" href="<?php echo esc_url( $file ); ?>"><?php echo esc_html( $file_name ); ?></a> |
| 501 | </li> |
| 502 | <?php } ?> |
| 503 | </ul> |
| 504 | </td> |
| 505 | <td> |
| 506 | <h2>Backup Files</h2> |
| 507 | <ul> |
| 508 | <?php |
| 509 | foreach ( $backup_files as $key => $file ) { |
| 510 | $file_name = basename( $file ); |
| 511 | $file = str_replace( $upload_dir['path'], $upload_dir['url'], $file ); |
| 512 | ?> |
| 513 | <li> |
| 514 | <a target="_blank" href="<?php echo esc_url( $file ); ?>"><?php echo esc_html( $file_name ); ?></a> |
| 515 | </li> |
| 516 | <?php } ?> |
| 517 | </ul> |
| 518 | </td> |
| 519 | <td> |
| 520 | <div class="batch-log"> |
| 521 | <p><?php echo wp_kses_post( $temp ); ?></p> |
| 522 | <p><?php echo wp_kses_post( $transient_status ); ?></p> |
| 523 | </div> |
| 524 | </td> |
| 525 | </tr> |
| 526 | </table> |
| 527 | <?php |
| 528 | } |
| 529 | |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * Kicking this off by calling 'get_instance()' method |
| 534 | */ |
| 535 | Astra_Sites_Importer_Log::get_instance(); |
| 536 | |
| 537 | endif; |
| 538 |