Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.19% |
59 / 64 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
| InstallChecker | |
92.19% |
59 / 64 |
|
66.67% |
4 / 6 |
13.08 | |
0.00% |
0 / 1 |
| isFreshInstallation | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
6.22 | |||
| getInstallationDate | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getOldestUser | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| getUserCount | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| getOldestPost | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
2 | |||
| getNewestPost | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\InstallChecker; |
| 4 | |
| 5 | class InstallChecker { |
| 6 | |
| 7 | |
| 8 | /** |
| 9 | * Check if this is a fresh WordPress installation. |
| 10 | * |
| 11 | * @return bool |
| 12 | */ |
| 13 | public function isFreshInstallation() { |
| 14 | |
| 15 | $oldestPost = $this->getOldestPost(); |
| 16 | $newestPost = $this->getNewestPost(); |
| 17 | |
| 18 | // If the "Hello World!" post doesn't exist, this isn't a fresh installation. |
| 19 | if ( ! isset( $oldestPost->ID ) || $oldestPost->ID !== 1 ) { |
| 20 | return false; |
| 21 | } |
| 22 | |
| 23 | // If the oldest and newest posts don't have the same modification dates, this isn't a fresh installation. |
| 24 | if ( $oldestPost->post_modified_gmt !== $newestPost->post_modified_gmt ) { |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | // If there isn't a user with an ID of 1, this isn't a fresh installation. |
| 29 | if ( $this->getOldestUser()->ID !== 1 ) { |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | // If there is more than 1 user, this isn't a fresh installation. |
| 34 | if ( $this->getUserCount() !== 1 ) { |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Get the site's creation/installation date. |
| 43 | * |
| 44 | * @return int |
| 45 | */ |
| 46 | public function getInstallationDate() { |
| 47 | |
| 48 | $postTimestamp = get_post_timestamp( $this->getOldestPost()->ID ); |
| 49 | $userTimestamp = strtotime( $this->getOldestUser()->user_registered ); |
| 50 | |
| 51 | return min( $postTimestamp, $userTimestamp ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns the oldest user on the site, based on user ID. |
| 56 | * |
| 57 | * @return \WP_User |
| 58 | */ |
| 59 | protected function getOldestUser() { |
| 60 | $query = new \WP_User_Query( |
| 61 | [ |
| 62 | 'number' => 1, |
| 63 | 'order' => 'ASC', |
| 64 | 'orderby' => 'ID', |
| 65 | 'count_total' => true, |
| 66 | 'cache_results' => false, |
| 67 | ] |
| 68 | ); |
| 69 | |
| 70 | return $query->get_results()[0]; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns the number of users on the site. |
| 75 | * |
| 76 | * @return int |
| 77 | */ |
| 78 | protected function getUserCount() { |
| 79 | $query = new \WP_User_Query( |
| 80 | [ |
| 81 | 'order' => 'ASC', |
| 82 | 'orderby' => 'ID', |
| 83 | 'fields' => 'ID', |
| 84 | 'count_total' => true, |
| 85 | 'cache_results' => false, |
| 86 | ] |
| 87 | ); |
| 88 | |
| 89 | return $query->total_users; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns the oldest post on the site, based on post ID. |
| 94 | * |
| 95 | * @return \WP_Post |
| 96 | */ |
| 97 | protected function getOldestPost() { |
| 98 | |
| 99 | $args = [ |
| 100 | 'post_type' => [ 'post', 'page' ], |
| 101 | 'post_status' => 'any', |
| 102 | 'orderby' => 'ID', |
| 103 | 'order' => 'ASC', |
| 104 | 'posts_per_page' => 1, |
| 105 | 'ignore_sticky_posts' => true, |
| 106 | 'no_found_rows' => true, |
| 107 | 'cache_results' => false, |
| 108 | 'suppress_filters' => true, |
| 109 | ]; |
| 110 | |
| 111 | if ( WooCommerce::isWooCommerce() ) { |
| 112 | $args['post__not_in'] = WooCommerce::getAllPageIds(); |
| 113 | } |
| 114 | |
| 115 | $query = new \WP_Query( $args ); |
| 116 | |
| 117 | return $query->post; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Returns the newest post on the site, based on post ID. |
| 122 | * |
| 123 | * @return \WP_Post |
| 124 | */ |
| 125 | protected function getNewestPost() { |
| 126 | |
| 127 | $args = [ |
| 128 | 'post_type' => [ 'post', 'page' ], |
| 129 | 'post_status' => 'any', |
| 130 | 'orderby' => 'ID', |
| 131 | 'order' => 'DESC', |
| 132 | 'posts_per_page' => 1, |
| 133 | 'ignore_sticky_posts' => true, |
| 134 | 'no_found_rows' => true, |
| 135 | 'cache_results' => false, |
| 136 | 'suppress_filters' => true, |
| 137 | ]; |
| 138 | |
| 139 | if ( WooCommerce::isWooCommerce() ) { |
| 140 | $args['post__not_in'] = WooCommerce::getAllPageIds(); |
| 141 | } |
| 142 | |
| 143 | $query = new \WP_Query( $args ); |
| 144 | |
| 145 | return $query->post; |
| 146 | } |
| 147 | |
| 148 | } |