|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * MainWP Child Site Api Backups |
| 4 | + * |
| 5 | + * Manages MainWP API Backups child site actions when needed. |
| 6 | + * |
| 7 | + * @package MainWP\Child |
| 8 | + */ |
| 9 | + |
| 10 | +namespace MainWP\Child; |
| 11 | + |
| 12 | +/** |
| 13 | + * Class MainWP_Child_Api_Backups |
| 14 | + * |
| 15 | + * This class handles all the MainWP API Backups child site actions when needed. |
| 16 | + * |
| 17 | + * @package MainWP\Child |
| 18 | + */ |
| 19 | +class MainWP_Child_Api_Backups { |
| 20 | + |
| 21 | + /** |
| 22 | + * Public variable to state if supported plugin is installed on the child site. |
| 23 | + * |
| 24 | + * @var bool If supported plugin is installed, return true, if not, return false. |
| 25 | + */ |
| 26 | + public $is_plugin_installed = false; |
| 27 | + |
| 28 | + /** |
| 29 | + * Public static variable to hold the single instance of the class. |
| 30 | + * |
| 31 | + * @var mixed Default null |
| 32 | + */ |
| 33 | + protected static $instance = null; |
| 34 | + |
| 35 | + /** |
| 36 | + * Method instance() |
| 37 | + * |
| 38 | + * Create a public static instance. |
| 39 | + * |
| 40 | + * @return mixed Class instance. |
| 41 | + */ |
| 42 | + public static function instance() { |
| 43 | + if ( null === self::$instance ) { |
| 44 | + self::$instance = new self(); |
| 45 | + } |
| 46 | + |
| 47 | + return self::$instance; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * MainWP_Child_Api_Backups constructor. |
| 52 | + * |
| 53 | + * Run any time class is called. |
| 54 | + */ |
| 55 | + public function __construct() { |
| 56 | + // Constructor. |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Create a backup of the database for the given child site. |
| 61 | + * |
| 62 | + * @return void |
| 63 | + */ |
| 64 | + public function api_backups_mysqldump() { |
| 65 | + |
| 66 | + // WordPress DB credentials. |
| 67 | + $database_name = DB_NAME; |
| 68 | + $user = DB_USER; |
| 69 | + $pass = DB_PASSWORD; |
| 70 | + |
| 71 | + // Remove ":" & all numbers from "Localhost:3306". |
| 72 | + $host = str_replace( ':', '', preg_replace( '/[0-9]+/', '', DB_HOST ) ); |
| 73 | + |
| 74 | + // Get Site URL. |
| 75 | + $site_url = str_replace( '/', '.', preg_replace( '#^https?://#i', '', get_bloginfo( 'url' ) ) ); |
| 76 | + |
| 77 | + // Create a timestamp. |
| 78 | + $current_date_time = current_datetime(); |
| 79 | + $current_date_time = $current_date_time->format( 'm-d-Y_H.i.s.A' ); |
| 80 | + |
| 81 | + // Build the uploads directory. |
| 82 | + $wp_get_upload_dir = wp_get_upload_dir(); |
| 83 | + $wp_upload_dir = $wp_get_upload_dir['basedir'] . '/mainwp/api_db_backups/'; |
| 84 | + |
| 85 | + // Build the full path to the backup file. |
| 86 | + $gzip_full_path = $wp_upload_dir . $database_name . '_' . $site_url . '_' . $current_date_time . '.sql.gz'; |
| 87 | + |
| 88 | + // Create the directory if it doesn't exist. |
| 89 | + if ( ! file_exists( $wp_upload_dir ) ) { //phpcs:ignore |
| 90 | + mkdir( $wp_upload_dir, 0755, true ); //phpcs:ignore |
| 91 | + } |
| 92 | + |
| 93 | + if ( function_exists( 'exec' ) ) { |
| 94 | + // Create the backup file. hide from logs ( password ). |
| 95 | + exec( "mysqldump --user={$user} --password='{$pass}' --host={$host} {$database_name} | gzip > {$gzip_full_path}", $output, $result ); //phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_exec |
| 96 | + } |
| 97 | + |
| 98 | + // Check if the backup was successful. |
| 99 | + if ( 0 === $result ) { |
| 100 | + // Success. |
| 101 | + MainWP_Helper::write( |
| 102 | + array( |
| 103 | + 'result' => 'GOOD', |
| 104 | + 'output' => $output, |
| 105 | + 'res' => $result, |
| 106 | + ) |
| 107 | + ); |
| 108 | + } else { |
| 109 | + // Error. |
| 110 | + MainWP_Helper::write( |
| 111 | + array( |
| 112 | + 'result' => 'ERROR', |
| 113 | + 'output' => $output, |
| 114 | + 'res' => $result, |
| 115 | + ) |
| 116 | + ); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments