independent-analytics
Last commit date
IAWP
2 years ago
dist
2 years ago
freemius
2 years ago
img
2 years ago
languages
2 years ago
vendor
2 years ago
views
2 years ago
iawp-bootstrap.php
2 years ago
iawp.php
2 years ago
readme.txt
2 years ago
iawp-bootstrap.php
316 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED; |
| 4 | |
| 5 | use IAWP\Dashboard_Options ; |
| 6 | use IAWP\Database ; |
| 7 | use IAWP\Database_Manager ; |
| 8 | use IAWP\Date_Range\Exact_Date_Range ; |
| 9 | use IAWP\Env ; |
| 10 | use IAWP\Geo_Database_Background_Job ; |
| 11 | use IAWP\Independent_Analytics ; |
| 12 | use IAWP\Interrupt ; |
| 13 | use IAWP\Migrations ; |
| 14 | use IAWP\Public_API\Analytics ; |
| 15 | use IAWP\Public_API\Singular_Analytics ; |
| 16 | use IAWP\Utils\BladeOne ; |
| 17 | use IAWP\WP_Option_Cache_Bust ; |
| 18 | \define( 'IAWP_DIRECTORY', \rtrim( \plugin_dir_path( __FILE__ ), \DIRECTORY_SEPARATOR ) ); |
| 19 | \define( 'IAWP_URL', \rtrim( \plugin_dir_url( __FILE__ ), '/' ) ); |
| 20 | \define( 'IAWP_VERSION', '2.3.1' ); |
| 21 | \define( 'IAWP_DATABASE_VERSION', '27' ); |
| 22 | \define( 'IAWP_LANGUAGES_DIRECTORY', \dirname( \plugin_basename( __FILE__ ) ) . '/languages' ); |
| 23 | \define( 'IAWP_PLUGIN_FILE', __DIR__ . '/iawp.php' ); |
| 24 | |
| 25 | if ( \file_exists( \IAWPSCOPED\iawp_path_to( 'vendor/scoper-autoload.php' ) ) ) { |
| 26 | require_once \IAWPSCOPED\iawp_path_to( 'vendor/scoper-autoload.php' ); |
| 27 | } else { |
| 28 | require_once \IAWPSCOPED\iawp_path_to( 'vendor/autoload.php' ); |
| 29 | } |
| 30 | |
| 31 | // This is needed because something with age gate is preventing my own helpers from loading |
| 32 | // The problem is that in autoload_static.php, there's some sort of caching going on where it's trying |
| 33 | // to not load a file twice. I'm guessing that's normally a good thing, but in our case we've made changes (scoped) |
| 34 | // so we do indeed want to load our version even though age gate has alreawdy learned |
| 35 | require_once \IAWPSCOPED\iawp_path_to( 'vendor/illuminate/collections/helpers.php' ); |
| 36 | /** |
| 37 | * @param $log |
| 38 | * |
| 39 | * @return void |
| 40 | * @internal |
| 41 | */ |
| 42 | function iawp_log( $log ) : void |
| 43 | { |
| 44 | if ( \WP_DEBUG === \true && \WP_DEBUG_LOG === \true ) { |
| 45 | |
| 46 | if ( \is_array( $log ) || \is_object( $log ) ) { |
| 47 | \error_log( \print_r( $log, \true ) ); |
| 48 | } else { |
| 49 | \error_log( $log ); |
| 50 | } |
| 51 | |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** @internal */ |
| 56 | function iawp_path_to( string $path ) : string |
| 57 | { |
| 58 | $path = \trim( $path, \DIRECTORY_SEPARATOR ); |
| 59 | return \implode( \DIRECTORY_SEPARATOR, [ \IAWP_DIRECTORY, $path ] ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * add_filter('iawp_temp_directory_path', function ($value) { |
| 64 | * return '/Users/andrew/site/wp-content/uploads/iawp'; |
| 65 | * }); |
| 66 | * |
| 67 | * @param string $path |
| 68 | * |
| 69 | * @return string |
| 70 | * @throws Exception |
| 71 | * @internal |
| 72 | */ |
| 73 | function iawp_temp_path_to( string $path ) : string |
| 74 | { |
| 75 | $temp_directory = ( \defined( 'IAWP_TEMP_DIR' ) ? \IAWP_TEMP_DIR : \apply_filters( 'iawp_temp_directory_path', 'temp' ) ); |
| 76 | $path = \rtrim( $path, \DIRECTORY_SEPARATOR ); |
| 77 | if ( $temp_directory === 'temp' ) { |
| 78 | return \IAWPSCOPED\iawp_path_to( \implode( \DIRECTORY_SEPARATOR, [ $temp_directory, $path ] ) ); |
| 79 | } |
| 80 | $temp_directory = \rtrim( $temp_directory, \DIRECTORY_SEPARATOR ); |
| 81 | if ( !\is_writable( $temp_directory ) ) { |
| 82 | \wp_mkdir_p( $temp_directory ); |
| 83 | } |
| 84 | // Separate condition to see if wp_mkdir_p call fixed the issue |
| 85 | if ( !\is_writable( $temp_directory ) ) { |
| 86 | throw new \Exception( 'You have provided and missing or non-writable directory for the iawp_temp_directory_path filter: ' . $temp_directory ); |
| 87 | } |
| 88 | return \implode( \DIRECTORY_SEPARATOR, [ $temp_directory, $path ] ); |
| 89 | } |
| 90 | |
| 91 | /** @internal */ |
| 92 | function iawp_url_to( string $path ) : string |
| 93 | { |
| 94 | $path = \trim( $path, '/' ); |
| 95 | return \implode( '/', [ \IAWP_URL, $path ] ); |
| 96 | } |
| 97 | |
| 98 | /** @internal */ |
| 99 | function iawp_upload_path_to( string $path ) : string |
| 100 | { |
| 101 | $path = \trim( $path, \DIRECTORY_SEPARATOR ); |
| 102 | return \implode( \DIRECTORY_SEPARATOR, [ \wp_upload_dir()['basedir'], $path ] ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Determines if the user is running a licensed pro version |
| 107 | * |
| 108 | * @return bool |
| 109 | * @internal |
| 110 | */ |
| 111 | function iawp_is_pro() : bool |
| 112 | { |
| 113 | return \false; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Determines if the user is running a free version or an unlicensed pro version |
| 118 | * @return bool |
| 119 | * @internal |
| 120 | */ |
| 121 | function iawp_is_free() : bool |
| 122 | { |
| 123 | return !\IAWPSCOPED\iawp_is_pro(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Determines if a pro user has WooCommerce activated |
| 128 | * @return bool |
| 129 | * @internal |
| 130 | */ |
| 131 | function iawp_using_woocommerce() : bool |
| 132 | { |
| 133 | global $wpdb ; |
| 134 | if ( \IAWPSCOPED\iawp_is_free() ) { |
| 135 | return \false; |
| 136 | } |
| 137 | $class_missing = \class_exists( '\\WooCommerce' ) === \false; |
| 138 | if ( $class_missing ) { |
| 139 | return \false; |
| 140 | } |
| 141 | $table_name = $wpdb->prefix . 'wc_order_stats'; |
| 142 | $order_stats_table = $wpdb->get_row( $wpdb->prepare( ' |
| 143 | SELECT * FROM INFORMATION_SCHEMA.TABLES |
| 144 | WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s |
| 145 | ', $wpdb->dbname, $table_name ) ); |
| 146 | if ( \is_null( $order_stats_table ) ) { |
| 147 | return \false; |
| 148 | } |
| 149 | return \true; |
| 150 | } |
| 151 | |
| 152 | /** @internal */ |
| 153 | function iawp_dashboard_url( array $query_arguments = [] ) : string |
| 154 | { |
| 155 | $default_query_arguments = [ |
| 156 | 'page' => 'independent-analytics', |
| 157 | ]; |
| 158 | return \add_query_arg( \array_merge( $default_query_arguments, $query_arguments ), \admin_url( 'admin.php' ) ); |
| 159 | } |
| 160 | |
| 161 | /** @internal */ |
| 162 | function iawp_blade() |
| 163 | { |
| 164 | if ( !\file_exists( \IAWPSCOPED\iawp_temp_path_to( 'template-cache' ) ) ) { |
| 165 | \wp_mkdir_p( \IAWPSCOPED\iawp_temp_path_to( 'template-cache' ) ); |
| 166 | } |
| 167 | $blade = BladeOne::create(); |
| 168 | $blade->share( 'env', new Env() ); |
| 169 | return $blade; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Get the currently installed database version |
| 174 | * |
| 175 | * @return int |
| 176 | * @internal |
| 177 | */ |
| 178 | function iawp_db_version() : int |
| 179 | { |
| 180 | return \intval( \get_option( 'iawp_db_version', '0' ) ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * iawp_singular_analytics('60', new DateTime('-3 days'), new DateTime()); |
| 185 | * |
| 186 | * @param string|int $singular_id |
| 187 | * @param DateTime $from |
| 188 | * @param DateTime $to |
| 189 | * |
| 190 | * @return Singular_Analytics|null |
| 191 | * @internal |
| 192 | */ |
| 193 | function iawp_singular_analytics( $singular_id, \DateTime $from, \DateTime $to ) : ?Singular_Analytics |
| 194 | { |
| 195 | $date_range = new Exact_Date_Range( $from, $to ); |
| 196 | return Singular_Analytics::for( $singular_id, $date_range ); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * iawp_analytics(new DateTime('-3 days'), new DateTime()); |
| 201 | * |
| 202 | * @param DateTime $from |
| 203 | * @param DateTime $to |
| 204 | * |
| 205 | * @return Analytics |
| 206 | * @internal |
| 207 | */ |
| 208 | function iawp_analytics( \DateTime $from, \DateTime $to ) : Analytics |
| 209 | { |
| 210 | $date_range = new Exact_Date_Range( $from, $to ); |
| 211 | return Analytics::for( $date_range ); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | if ( !\extension_loaded( 'pdo' ) || !\extension_loaded( 'pdo_mysql' ) ) { |
| 216 | $interrupt = new Interrupt( 'interrupt.pdo' ); |
| 217 | $interrupt->render(); |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | global $wpdb ; |
| 222 | |
| 223 | if ( \strlen( $wpdb->prefix ) > 25 ) { |
| 224 | $interrupt = new Interrupt( 'interrupt.database-prefix-too-long' ); |
| 225 | $interrupt->render( [ |
| 226 | 'prefix' => $wpdb->prefix, |
| 227 | 'length' => \strlen( $wpdb->prefix ), |
| 228 | ] ); |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | if ( Migrations\Migrations::is_database_ahead_of_plugin() ) { |
| 234 | $interrupt = new Interrupt( 'interrupt.database-ahead-of-plugin' ); |
| 235 | $interrupt->render(); |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | |
| 240 | if ( \IAWPSCOPED\iawp_db_version() === 0 && !Database::has_correct_database_privileges() ) { |
| 241 | $interrupt = new Interrupt( 'interrupt.missing-database-permissions' ); |
| 242 | $interrupt->render( [ |
| 243 | 'missing_privileges' => Database::missing_database_privileges(), |
| 244 | ] ); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | |
| 249 | if ( \IAWPSCOPED\iawp_db_version() > 0 && Database::is_missing_all_tables() ) { |
| 250 | $interrupt = new Interrupt( 'interrupt.missing-database-tables' ); |
| 251 | $interrupt->render(); |
| 252 | return; |
| 253 | // $database_manager = new Database_Manager(); |
| 254 | // $database_manager->delete_all_data(); |
| 255 | } |
| 256 | |
| 257 | WP_Option_Cache_Bust::register( 'iawp_is_migrating' ); |
| 258 | WP_Option_Cache_Bust::register( 'iawp_is_database_downloading' ); |
| 259 | WP_Option_Cache_Bust::register( 'iawp_db_version' ); |
| 260 | WP_Option_Cache_Bust::register( 'iawp_geo_database_version' ); |
| 261 | /** @internal */ |
| 262 | function iawp() |
| 263 | { |
| 264 | return Independent_Analytics::getInstance(); |
| 265 | } |
| 266 | |
| 267 | \IAWPSCOPED\iawp(); |
| 268 | \register_activation_hook( \IAWP_PLUGIN_FILE, function () { |
| 269 | \wp_mkdir_p( \IAWPSCOPED\iawp_temp_path_to( 'template-cache' ) ); |
| 270 | \wp_mkdir_p( \IAWPSCOPED\iawp_temp_path_to( 'device-data-cache' ) ); |
| 271 | |
| 272 | if ( \get_option( 'iawp_db_version', '0' ) === '0' ) { |
| 273 | // If there is no database installed, run migration on current process |
| 274 | Migrations\Migrations::create_or_migrate(); |
| 275 | } else { |
| 276 | // If there is a database, run migration in a background process |
| 277 | Migrations\Migration_Job::maybe_dispatch(); |
| 278 | } |
| 279 | |
| 280 | Geo_Database_Background_Job::maybe_dispatch(); |
| 281 | \update_option( 'iawp_need_clear_cache', \true ); |
| 282 | \IAWPSCOPED\iawp()->cron_manager->schedule_refresh_salt(); |
| 283 | if ( \IAWPSCOPED\iawp_is_pro() ) { |
| 284 | \IAWPSCOPED\iawp()->email_reports->schedule_email_report(); |
| 285 | } |
| 286 | // Set current version for changelog notifications |
| 287 | \update_option( 'iawp_last_update_viewed', \IAWP_VERSION ); |
| 288 | } ); |
| 289 | \register_deactivation_hook( \IAWP_PLUGIN_FILE, function () { |
| 290 | \IAWPSCOPED\iawp()->cron_manager->unschedule_daily_salt_refresh(); |
| 291 | if ( \IAWPSCOPED\iawp_is_pro() ) { |
| 292 | \IAWPSCOPED\iawp()->email_reports->unschedule_email_report(); |
| 293 | } |
| 294 | \wp_delete_file( \trailingslashit( \WPMU_PLUGIN_DIR ) . 'iawp-performance-boost.php' ); |
| 295 | \delete_option( 'iawp_must_use_directory_not_writable' ); |
| 296 | } ); |
| 297 | /* |
| 298 | * The admin_init hook will fire when the dashboard is loaded or an admin ajax request is made |
| 299 | */ |
| 300 | \add_action( 'admin_init', function () { |
| 301 | Migrations\Migrations::handle_migration_18_error(); |
| 302 | Migrations\Migrations::handle_migration_22_error(); |
| 303 | $options = new Dashboard_Options(); |
| 304 | $options->maybe_redirect(); |
| 305 | new Migrations\Migration_Job(); |
| 306 | |
| 307 | if ( \get_option( 'iawp_db_version', '0' ) === '0' ) { |
| 308 | // If there is no database installed, run migration on current process |
| 309 | Migrations\Migrations::create_or_migrate(); |
| 310 | } else { |
| 311 | // If there is a database, run migration in a background process |
| 312 | Migrations\Migration_Job::maybe_dispatch(); |
| 313 | } |
| 314 | |
| 315 | Geo_Database_Background_Job::maybe_dispatch(); |
| 316 | } ); |