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
sql
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
200 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED; |
| 4 | |
| 5 | use IAWP_SCOPED\eftec\bladeone\BladeOne ; |
| 6 | use IAWP_SCOPED\IAWP\Env ; |
| 7 | use IAWP_SCOPED\IAWP\Geo_Database_Background_Job ; |
| 8 | use IAWP_SCOPED\IAWP\Independent_Analytics ; |
| 9 | use IAWP_SCOPED\IAWP\Interrupt ; |
| 10 | use IAWP_SCOPED\IAWP\Migrations ; |
| 11 | use IAWP_SCOPED\IAWP\WP_Option_Cache_Bust ; |
| 12 | \define( 'IAWP_DIRECTORY', \rtrim( \plugin_dir_path( __FILE__ ), \DIRECTORY_SEPARATOR ) ); |
| 13 | \define( 'IAWP_URL', \rtrim( \plugin_dir_url( __FILE__ ), '/' ) ); |
| 14 | \define( 'IAWP_VERSION', '1.30.0' ); |
| 15 | \define( 'IAWP_LANGUAGES_DIRECTORY', \dirname( \plugin_basename( __FILE__ ) ) . '/languages' ); |
| 16 | |
| 17 | if ( \file_exists( \IAWP_SCOPED\iawp_path_to( 'vendor/scoper-autoload.php' ) ) ) { |
| 18 | require_once \IAWP_SCOPED\iawp_path_to( 'vendor/scoper-autoload.php' ); |
| 19 | } else { |
| 20 | require_once \IAWP_SCOPED\iawp_path_to( 'vendor/autoload.php' ); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * @param $log |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | function iawp_log( $log ) : void |
| 29 | { |
| 30 | if ( \WP_DEBUG === \true && \WP_DEBUG_LOG === \true ) { |
| 31 | |
| 32 | if ( \is_array( $log ) || \is_object( $log ) ) { |
| 33 | \error_log( \print_r( $log, \true ) ); |
| 34 | } else { |
| 35 | \error_log( $log ); |
| 36 | } |
| 37 | |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | function iawp_path_to( string $path ) : string |
| 42 | { |
| 43 | $path = \trim( $path, \DIRECTORY_SEPARATOR ); |
| 44 | return \implode( \DIRECTORY_SEPARATOR, [ \IAWP_DIRECTORY, $path ] ); |
| 45 | } |
| 46 | |
| 47 | function iawp_url_to( string $path ) : string |
| 48 | { |
| 49 | $path = \trim( $path, '/' ); |
| 50 | return \implode( '/', [ \IAWP_URL, $path ] ); |
| 51 | } |
| 52 | |
| 53 | function iawp_upload_path_to( string $path ) : string |
| 54 | { |
| 55 | $path = \trim( $path, \DIRECTORY_SEPARATOR ); |
| 56 | return \implode( \DIRECTORY_SEPARATOR, [ \wp_upload_dir()['basedir'], 'iawp', $path ] ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Provide defaults for arguments provided as an associated array |
| 61 | * |
| 62 | * @param array $args |
| 63 | * @param array $defaults |
| 64 | * |
| 65 | * @return array |
| 66 | */ |
| 67 | function iawp_default_args( array $args = array(), array $defaults = array() ) : array |
| 68 | { |
| 69 | $args = \array_filter( $args, function ( $value ) { |
| 70 | // Remove key/value pairs where value is null |
| 71 | return $value !== null; |
| 72 | } ); |
| 73 | $args = \array_intersect_key( $args, $defaults ); |
| 74 | return \array_replace( $defaults, $args ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Determines if the user is running a licensed pro version |
| 79 | * |
| 80 | * @return bool |
| 81 | */ |
| 82 | function iawp_is_pro() : bool |
| 83 | { |
| 84 | return \false; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Determines if the user is running a free version or an unlicensed pro version |
| 89 | * @return bool |
| 90 | */ |
| 91 | function iawp_is_free() : bool |
| 92 | { |
| 93 | return !\IAWP_SCOPED\iawp_is_pro(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Determines if a pro user has WooCommerce activated |
| 98 | * @return bool |
| 99 | */ |
| 100 | function iawp_using_woocommerce() : bool |
| 101 | { |
| 102 | global $wpdb ; |
| 103 | if ( \IAWP_SCOPED\iawp_is_free() ) { |
| 104 | return \false; |
| 105 | } |
| 106 | $class_missing = \class_exists( '\\WooCommerce' ) === \false; |
| 107 | if ( $class_missing ) { |
| 108 | return \false; |
| 109 | } |
| 110 | $table_name = $wpdb->prefix . 'wc_order_stats'; |
| 111 | $order_stats_table = $wpdb->get_row( $wpdb->prepare( ' |
| 112 | SELECT * FROM INFORMATION_SCHEMA.TABLES |
| 113 | WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s |
| 114 | ', $wpdb->dbname, $table_name ) ); |
| 115 | if ( \is_null( $order_stats_table ) ) { |
| 116 | return \false; |
| 117 | } |
| 118 | return \true; |
| 119 | } |
| 120 | |
| 121 | function iawp_dashboard_url() |
| 122 | { |
| 123 | return \add_query_arg( [ |
| 124 | 'page' => 'independent-analytics', |
| 125 | ], \admin_url( 'admin.php' ) ); |
| 126 | } |
| 127 | |
| 128 | function iawp_blade() : BladeOne |
| 129 | { |
| 130 | if ( !\file_exists( \IAWP_SCOPED\iawp_upload_path_to( 'template-cache' ) ) ) { |
| 131 | \wp_mkdir_p( \IAWP_SCOPED\iawp_upload_path_to( 'template-cache' ) ); |
| 132 | } |
| 133 | $blade = new BladeOne( \IAWP_SCOPED\iawp_path_to( 'views' ), \IAWP_SCOPED\iawp_upload_path_to( 'template-cache' ) ); |
| 134 | $blade->share( 'env', new Env() ); |
| 135 | return $blade; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | if ( !\extension_loaded( 'pdo' ) ) { |
| 140 | $interrupt = new Interrupt( 'interrupt.pdo', \false ); |
| 141 | $interrupt->render(); |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | if ( Migrations\Migration::is_database_ahead_of_plugin() ) { |
| 147 | $interrupt = new Interrupt( 'interrupt.database-ahead-of-plugin', \false ); |
| 148 | $interrupt->render(); |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | WP_Option_Cache_Bust::register( 'iawp_is_migrating' ); |
| 153 | WP_Option_Cache_Bust::register( 'iawp_is_database_downloading' ); |
| 154 | WP_Option_Cache_Bust::register( 'iawp_db_version' ); |
| 155 | WP_Option_Cache_Bust::register( 'iawp_geo_database_version' ); |
| 156 | function iawp() |
| 157 | { |
| 158 | return Independent_Analytics::getInstance(); |
| 159 | } |
| 160 | |
| 161 | \IAWP_SCOPED\iawp(); |
| 162 | \register_activation_hook( __DIR__ . '/iawp.php', function () { |
| 163 | \wp_mkdir_p( \IAWP_SCOPED\iawp_upload_path_to( 'template-cache' ) ); |
| 164 | \wp_mkdir_p( \IAWP_SCOPED\iawp_upload_path_to( 'device-data-cache' ) ); |
| 165 | |
| 166 | if ( \get_option( 'iawp_db_version', '0' ) === '0' ) { |
| 167 | // If there is no database installed, run migration on current process |
| 168 | Migrations\Migration::create_or_migrate(); |
| 169 | } else { |
| 170 | // If there is a database, run migration in a background process |
| 171 | Migrations\Migration_Job::maybe_dispatch(); |
| 172 | } |
| 173 | |
| 174 | Geo_Database_Background_Job::maybe_dispatch(); |
| 175 | \update_option( 'iawp_need_clear_cache', \true ); |
| 176 | if ( \IAWP_SCOPED\iawp_is_pro() ) { |
| 177 | \IAWP_SCOPED\iawp()->email_reports->schedule_email_report(); |
| 178 | } |
| 179 | } ); |
| 180 | \register_deactivation_hook( __DIR__ . '/iawp.php', function () { |
| 181 | if ( \IAWP_SCOPED\iawp_is_pro() ) { |
| 182 | \IAWP_SCOPED\iawp()->email_reports->unschedule_email_report(); |
| 183 | } |
| 184 | } ); |
| 185 | /* |
| 186 | * The admin_init hook will fire when the dashboard is loaded or an admin ajax request is made |
| 187 | */ |
| 188 | \add_action( 'admin_init', function () { |
| 189 | new Migrations\Migration_Job(); |
| 190 | |
| 191 | if ( \get_option( 'iawp_db_version', '0' ) === '0' ) { |
| 192 | // If there is no database installed, run migration on current process |
| 193 | Migrations\Migration::create_or_migrate(); |
| 194 | } else { |
| 195 | // If there is a database, run migration in a background process |
| 196 | Migrations\Migration_Job::maybe_dispatch(); |
| 197 | } |
| 198 | |
| 199 | Geo_Database_Background_Job::maybe_dispatch(); |
| 200 | } ); |