| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP Database Controller |
| 4 |
* |
| 5 |
* This file handles all interactions with the DB. |
| 6 |
* |
| 7 |
* @package MainWP/Dashboard |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace MainWP\Dashboard; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class MainWP_DB |
| 14 |
* |
| 15 |
* @package MainWP\Dashboard |
| 16 |
* |
| 17 |
* @uses \MainWP\Dashboard\MainWP_DB |
| 18 |
*/ |
| 19 |
class MainWP_Auto_Updates_DB extends MainWP_DB { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR. |
| 20 |
|
| 21 |
// phpcs:disable WordPress.DB.RestrictedFunctions, WordPress.DB.PreparedSQL.NotPrepared, Generic.Metrics.CyclomaticComplexity -- This is the only way to achieve desired results, pull request solutions appreciated. |
| 22 |
|
| 23 |
/** |
| 24 |
* Private static variable to hold the single instance of the class. |
| 25 |
* |
| 26 |
* @static |
| 27 |
* |
| 28 |
* @var mixed Default null |
| 29 |
*/ |
| 30 |
private static $instance = null; |
| 31 |
|
| 32 |
/** |
| 33 |
* Create public static instance. |
| 34 |
* |
| 35 |
* @static |
| 36 |
* |
| 37 |
* @return MainWP_DB |
| 38 |
*/ |
| 39 |
public static function instance() { |
| 40 |
if ( null === static::$instance ) { |
| 41 |
static::$instance = new self(); |
| 42 |
} |
| 43 |
|
| 44 |
static::$instance->test_connection(); |
| 45 |
|
| 46 |
return static::$instance; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
/** |
| 51 |
* Get child sites check updates. |
| 52 |
* |
| 53 |
* @param int $limit Query limit. |
| 54 |
* @param int $lasttime_start Lasttime start automatic update. |
| 55 |
* @param bool $connected Requires sites connected. |
| 56 |
* @param bool $not_suspended Requires sites unsuspended. |
| 57 |
* |
| 58 |
* @return object|null Database query result or null on failure. |
| 59 |
*/ |
| 60 |
public function get_websites_check_updates( $limit, $lasttime_start, $connected = false, $not_suspended = false ) { |
| 61 |
$where = ''; |
| 62 |
if ( true === $connected ) { |
| 63 |
$where .= ' wp_sync.sync_errors = "" AND'; |
| 64 |
} |
| 65 |
if ( true === $not_suspended ) { |
| 66 |
$where .= ' wp.suspended = 0 AND'; |
| 67 |
} |
| 68 |
$sql = 'SELECT wp.*,wp_sync.*,wp_optionview.* FROM ' . $this->table_name( 'wp' ) . ' wp JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid JOIN ' . $this->get_option_view( array( 'favi_icon' ) ) . ' wp_optionview ON wp.id = wp_optionview.wpid WHERE ' . $where . ' ( wp_sync.dtsAutomaticSyncStart = 0 OR wp_sync.dtsAutomaticSyncStart < ' . intval( $lasttime_start ) . ' ) ORDER BY wp_sync.dtsAutomaticSyncStart ASC LIMIT ' . $limit; |
| 69 |
return $this->wpdb->get_results( $sql, OBJECT ); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
/** |
| 74 |
* Get child sites to start updates. |
| 75 |
* |
| 76 |
* @param bool $connected Requires sites connected. |
| 77 |
* @param bool $not_suspended Requires sites unsuspended. |
| 78 |
* |
| 79 |
* @return object|null Database query result or null on failure. |
| 80 |
*/ |
| 81 |
public function get_websites_to_start_updates( $connected = false, $not_suspended = false ) { |
| 82 |
|
| 83 |
$where = ''; |
| 84 |
if ( true === $connected ) { |
| 85 |
$where .= ' wp_sync.sync_errors = "" AND'; |
| 86 |
} |
| 87 |
if ( true === $not_suspended ) { |
| 88 |
$where .= ' wp.suspended = 0 AND'; |
| 89 |
} |
| 90 |
|
| 91 |
$where = rtrim( $where, 'AND' ); |
| 92 |
$params = array( |
| 93 |
'view' => 'updates_view', |
| 94 |
'where' => $where, |
| 95 |
); |
| 96 |
|
| 97 |
return MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user_by_params( $params ) ); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
/** |
| 102 |
* Get child sites to continue updates. |
| 103 |
* |
| 104 |
* @param int $limit limit sites to continue updates. |
| 105 |
* @param int $lasttime_start Lasttime start automatic update. |
| 106 |
* @param bool $connected Requires sites connected. |
| 107 |
* @param bool $not_suspended Requires sites unsuspended. |
| 108 |
* |
| 109 |
* @return object|null Database query result or null on failure. |
| 110 |
*/ |
| 111 |
public function get_websites_to_continue_updates( $limit, $lasttime_start, $connected = false, $not_suspended = false ) { |
| 112 |
|
| 113 |
if ( empty( $lasttime_start ) ) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
$where = ' ( wp_sync.dtsAutomaticSyncStart > 0 AND wp_sync.dtsAutomaticSync < wp_sync.dtsAutomaticSyncStart AND wp_sync.dtsAutomaticSyncStart < ' . intval( $lasttime_start ) . ' ) AND'; // less than to sure that exactly trigger start updates timestamp. |
| 118 |
if ( true === $connected ) { |
| 119 |
$where .= ' wp_sync.sync_errors = "" AND'; |
| 120 |
} |
| 121 |
if ( true === $not_suspended ) { |
| 122 |
$where .= ' wp.suspended = 0 AND'; |
| 123 |
} |
| 124 |
|
| 125 |
$where = rtrim( $where, 'AND' ); |
| 126 |
|
| 127 |
$params = array( |
| 128 |
'view' => 'updates_view', |
| 129 |
'where' => $where, |
| 130 |
'limit' => $limit, |
| 131 |
); |
| 132 |
|
| 133 |
$results = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user_by_params( $params ) ); |
| 134 |
|
| 135 |
$websites = array(); |
| 136 |
while ( $results && $website = static::fetch_object( $results ) ) { |
| 137 |
$websites[] = $website; |
| 138 |
} |
| 139 |
|
| 140 |
if ( $results ) { |
| 141 |
static::free_result( $results ); |
| 142 |
} |
| 143 |
|
| 144 |
return $websites; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
/** |
| 149 |
* Get child sites to start updates. |
| 150 |
* |
| 151 |
* @param int $limit limit sites to continue updates. |
| 152 |
* @param int $lasttime_start Lasttime start automatic update. |
| 153 |
* |
| 154 |
* @return object|null Database query result or null on failure. |
| 155 |
*/ |
| 156 |
public function get_websites_to_continue_individual_updates( $limit = 4, $lasttime_start = false ) { |
| 157 |
|
| 158 |
if ( empty( $lasttime_start ) ) { |
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
$where = ' wp_sync.sync_errors = "" AND '; |
| 163 |
$where .= ' wp.suspended = 0 AND '; |
| 164 |
$where .= ' wp_optionview.batch_individual_queue_time >= ' . intval( $lasttime_start ) . ' '; |
| 165 |
|
| 166 |
$params = array( |
| 167 |
'view' => 'updates_view', |
| 168 |
'where' => $where, |
| 169 |
'limit' => $limit, |
| 170 |
'others_fields' => array( 'batch_individual_queue_time' ), |
| 171 |
); |
| 172 |
|
| 173 |
$results = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user_by_params( $params ) ); |
| 174 |
|
| 175 |
$websites = array(); |
| 176 |
while ( $results && $website = static::fetch_object( $results ) ) { |
| 177 |
$websites[] = $website; |
| 178 |
} |
| 179 |
|
| 180 |
if ( $results ) { |
| 181 |
static::free_result( $results ); |
| 182 |
} |
| 183 |
|
| 184 |
return $websites; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
/** |
| 189 |
* Get websites check updates count. |
| 190 |
* |
| 191 |
* @param int $lasttime_start Lasttime start automatic update. |
| 192 |
* |
| 193 |
* @return int Child sites update count. |
| 194 |
*/ |
| 195 |
public function get_websites_check_updates_count( $lasttime_start ) { |
| 196 |
$where = $this->get_sql_where_allow_access_sites( 'wp' ); |
| 197 |
|
| 198 |
return $this->wpdb->get_var( 'SELECT count(wp.id) FROM ' . $this->table_name( 'wp' ) . ' wp JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid WHERE ( wp_sync.dtsAutomaticSyncStart = 0 OR wp_sync.dtsAutomaticSyncStart < ' . intval( $lasttime_start ) . ')' . $where ); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Get child site count where date & time Session sync is smaller then start. |
| 203 |
* |
| 204 |
* @param int $lasttime_start Last time start automatic. |
| 205 |
* |
| 206 |
* @return int Returned child site count. |
| 207 |
*/ |
| 208 |
public function get_websites_count_where_dts_automatic_sync_smaller_then_start( $lasttime_start ) { |
| 209 |
return $this->wpdb->get_var( 'SELECT count(wp.id) FROM ' . $this->table_name( 'wp' ) . ' wp JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid WHERE wp.suspended = 0 AND ( ( wp_sync.dtsAutomaticSync < wp_sync.dtsAutomaticSyncStart AND wp_sync.dtsAutomaticSyncStart > ' . intval( $lasttime_start ) . ') OR (wp_sync.dtsAutomaticSyncStart = 0) ) ' ); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Get child site last automatic sync date & time. |
| 214 |
* |
| 215 |
* @return string Date and time of last automatic sync. |
| 216 |
*/ |
| 217 |
public function get_websites_last_automatic_sync() { |
| 218 |
return $this->wpdb->get_var( 'SELECT MAX(wp_sync.dtsAutomaticSync) FROM ' . $this->table_name( 'wp' ) . ' wp JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid' ); |
| 219 |
} |
| 220 |
} |
| 221 |
|