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