independent-analytics
Last commit date
IAWP
3 years ago
dist
3 years ago
freemius
3 years ago
img
3 years ago
languages
3 years ago
templates
3 years ago
vendor
3 years ago
iawp.php
3 years ago
index.php
3 years ago
readme.txt
3 years ago
index.php
223 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | define( 'IAWP_DIRECTORY', rtrim( plugin_dir_path( __FILE__ ), DIRECTORY_SEPARATOR ) ); |
| 6 | define( 'IAWP_URL', rtrim( plugin_dir_url( __FILE__ ), '/' ) ); |
| 7 | define( 'IAWP_VERSION', '1.18.1' ); |
| 8 | define( 'IAWP_LANGUAGES_DIRECTORY', dirname( plugin_basename( __FILE__ ) ) . '/languages' ); |
| 9 | function iawp_log( $log ) |
| 10 | { |
| 11 | if ( WP_DEBUG === true ) { |
| 12 | |
| 13 | if ( is_array( $log ) || is_object( $log ) ) { |
| 14 | error_log( print_r( $log, true ) ); |
| 15 | } else { |
| 16 | error_log( $log ); |
| 17 | } |
| 18 | |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | function path_to( $path ) |
| 23 | { |
| 24 | $path = trim( $path, DIRECTORY_SEPARATOR ); |
| 25 | return implode( DIRECTORY_SEPARATOR, [ IAWP_DIRECTORY, $path ] ); |
| 26 | } |
| 27 | |
| 28 | function url_to( $path ) |
| 29 | { |
| 30 | $path = trim( $path, '/' ); |
| 31 | return implode( '/', [ IAWP_URL, $path ] ); |
| 32 | } |
| 33 | |
| 34 | // Load scoped composer autoloader |
| 35 | require_once path_to( 'vendor/scoper-autoload.php' ); |
| 36 | // Load scoped composer autoloader |
| 37 | require_once path_to( 'vendor/scoper-autoload.php' ); |
| 38 | // Load class utilities needed for other require calls to work |
| 39 | require_once path_to( 'IAWP/utils/singleton.php' ); |
| 40 | // Load utils |
| 41 | require_once path_to( 'IAWP/utils/array_to_csv.php' ); |
| 42 | require_once path_to( 'IAWP/utils/date_format.php' ); |
| 43 | require_once path_to( 'IAWP/utils/exact_range.php' ); |
| 44 | require_once path_to( 'IAWP/utils/number_formatter.php' ); |
| 45 | require_once path_to( 'IAWP/models/visitor.php' ); |
| 46 | require_once path_to( 'IAWP/utils/request.php' ); |
| 47 | require_once path_to( 'IAWP/utils/salt.php' ); |
| 48 | require_once path_to( 'IAWP/utils/security.php' ); |
| 49 | require_once path_to( 'IAWP/utils/string.php' ); |
| 50 | require_once path_to( 'IAWP/utils/timezone.php' ); |
| 51 | require_once path_to( 'IAWP/utils/relative_range.php' ); |
| 52 | require_once path_to( 'IAWP/utils/url.php' ); |
| 53 | require_once path_to( 'IAWP/utils/wp-async-request.php' ); |
| 54 | // Load ajax |
| 55 | require_once path_to( 'IAWP/ajax/ajax.php' ); |
| 56 | require_once path_to( 'IAWP/ajax/cleared_cache_ajax.php' ); |
| 57 | require_once path_to( 'IAWP/ajax/create_campaign_ajax.php' ); |
| 58 | require_once path_to( 'IAWP/ajax/delete_data_ajax.php' ); |
| 59 | require_once path_to( 'IAWP/ajax/export_campaigns_ajax.php' ); |
| 60 | require_once path_to( 'IAWP/ajax/export_geo_ajax.php' ); |
| 61 | require_once path_to( 'IAWP/ajax/export_referrers_ajax.php' ); |
| 62 | require_once path_to( 'IAWP/ajax/export_views_ajax.php' ); |
| 63 | require_once path_to( 'IAWP/ajax/filters_ajax.php' ); |
| 64 | require_once path_to( 'IAWP/ajax/migration_status_ajax.php' ); |
| 65 | require_once path_to( 'IAWP/ajax/real_time_ajax.php' ); |
| 66 | require_once path_to( 'IAWP/ajax/test_email_ajax.php' ); |
| 67 | require_once path_to( 'IAWP/ajax/update_capabilities_ajax.php' ); |
| 68 | // Load models |
| 69 | require_once path_to( 'IAWP/models/view_stats.php' ); |
| 70 | require_once path_to( 'IAWP/models/woocommerce_stats.php' ); |
| 71 | require_once path_to( 'IAWP/models/page.php' ); |
| 72 | require_once path_to( 'IAWP/models/page_author_archive.php' ); |
| 73 | require_once path_to( 'IAWP/models/page_date_archive.php' ); |
| 74 | require_once path_to( 'IAWP/models/page_home.php' ); |
| 75 | require_once path_to( 'IAWP/models/page_not_found.php' ); |
| 76 | require_once path_to( 'IAWP/models/page_post_type_archive.php' ); |
| 77 | require_once path_to( 'IAWP/models/page_search.php' ); |
| 78 | require_once path_to( 'IAWP/models/page_singular.php' ); |
| 79 | require_once path_to( 'IAWP/models/page_term_archive.php' ); |
| 80 | require_once path_to( 'IAWP/models/campaign.php' ); |
| 81 | require_once path_to( 'IAWP/models/current_traffic.php' ); |
| 82 | require_once path_to( 'IAWP/models/geo.php' ); |
| 83 | require_once path_to( 'IAWP/models/referrer.php' ); |
| 84 | // Load queries |
| 85 | require_once path_to( 'IAWP/queries/range_query.php' ); |
| 86 | require_once path_to( 'IAWP/queries/campaigns.php' ); |
| 87 | require_once path_to( 'IAWP/queries/city_statistics.php' ); |
| 88 | require_once path_to( 'IAWP/queries/country_statistics.php' ); |
| 89 | require_once path_to( 'IAWP/queries/current_traffic_finder.php' ); |
| 90 | require_once path_to( 'IAWP/queries/referrers.php' ); |
| 91 | require_once path_to( 'IAWP/queries/reset_database.php' ); |
| 92 | require_once path_to( 'IAWP/queries/resources.php' ); |
| 93 | require_once path_to( 'IAWP/queries/view.php' ); |
| 94 | require_once path_to( 'IAWP/queries/views.php' ); |
| 95 | require_once path_to( 'IAWP/queries/visitors_over_time_finder.php' ); |
| 96 | // Load root files |
| 97 | require_once path_to( 'IAWP/campaign_builder.php' ); |
| 98 | require_once path_to( 'IAWP/capability_manager.php' ); |
| 99 | require_once path_to( 'IAWP/chart.php' ); |
| 100 | require_once path_to( 'IAWP/chart_geo.php' ); |
| 101 | require_once path_to( 'IAWP/chart_svg.php' ); |
| 102 | require_once path_to( 'IAWP/current_resource.php' ); |
| 103 | require_once path_to( 'IAWP/dashboard_options.php' ); |
| 104 | require_once path_to( 'IAWP/dashboard_widget.php' ); |
| 105 | require_once path_to( 'IAWP/query.php' ); |
| 106 | require_once path_to( 'IAWP/email_reports.php' ); |
| 107 | require_once path_to( 'IAWP/filters.php' ); |
| 108 | require_once path_to( 'IAWP/geo_database.php' ); |
| 109 | require_once path_to( 'IAWP/geo_database_download_job.php' ); |
| 110 | require_once path_to( 'IAWP/health_check.php' ); |
| 111 | require_once path_to( 'IAWP/independent_analytics.php' ); |
| 112 | require_once path_to( 'IAWP/known_referrers.php' ); |
| 113 | require_once path_to( 'IAWP/pdf.php' ); |
| 114 | require_once path_to( 'IAWP/quick_stats.php' ); |
| 115 | require_once path_to( 'IAWP/real_time.php' ); |
| 116 | require_once path_to( 'IAWP/rest_api.php' ); |
| 117 | require_once path_to( 'IAWP/settings.php' ); |
| 118 | require_once path_to( 'IAWP/tables/column.php' ); |
| 119 | require_once path_to( 'IAWP/tables/table.php' ); |
| 120 | require_once path_to( 'IAWP/tables/table_campaigns.php' ); |
| 121 | require_once path_to( 'IAWP/tables/table_geo.php' ); |
| 122 | require_once path_to( 'IAWP/tables/table_referrers.php' ); |
| 123 | require_once path_to( 'IAWP/tables/table_views.php' ); |
| 124 | require_once path_to( 'IAWP/track_resource_changes.php' ); |
| 125 | require_once path_to( 'IAWP/view_counter.php' ); |
| 126 | // Initialize independent analytics |
| 127 | function IAWP() |
| 128 | { |
| 129 | return Independent_Analytics::getInstance(); |
| 130 | } |
| 131 | |
| 132 | IAWP(); |
| 133 | add_filter( 'pre_option_iawp_is_migrating', function ( $default ) { |
| 134 | global $wpdb ; |
| 135 | $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", 'iawp_is_migrating' ) ); |
| 136 | |
| 137 | if ( is_object( $row ) ) { |
| 138 | $value = $row->option_value; |
| 139 | } else { |
| 140 | $value = $default; |
| 141 | } |
| 142 | |
| 143 | return apply_filters( 'option_iawp_is_migrating', maybe_unserialize( $value ), 'iawp_is_migrating' ); |
| 144 | } ); |
| 145 | add_filter( 'pre_option_iawp_is_database_downloading', function ( $default ) { |
| 146 | global $wpdb ; |
| 147 | $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", 'iawp_is_database_downloading' ) ); |
| 148 | |
| 149 | if ( is_object( $row ) ) { |
| 150 | $value = $row->option_value; |
| 151 | } else { |
| 152 | $value = $default; |
| 153 | } |
| 154 | |
| 155 | return apply_filters( 'option_iawp_is_database_downloading', maybe_unserialize( $value ), 'iawp_is_database_downloading' ); |
| 156 | } ); |
| 157 | add_filter( 'pre_option_iawp_db_version', function ( $default ) { |
| 158 | global $wpdb ; |
| 159 | $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", 'iawp_db_version' ) ); |
| 160 | |
| 161 | if ( is_object( $row ) ) { |
| 162 | $value = $row->option_value; |
| 163 | } else { |
| 164 | $value = $default; |
| 165 | } |
| 166 | |
| 167 | return apply_filters( 'option_iawp_db_version', maybe_unserialize( $value ), 'iawp_db_version' ); |
| 168 | } ); |
| 169 | register_activation_hook( __DIR__ . '/iawp.php', function () { |
| 170 | |
| 171 | if ( get_option( 'iawp_db_version', '0' ) === '0' ) { |
| 172 | // If there is no database installed, run migration on current process |
| 173 | Migrations\Migration::create_or_migrate(); |
| 174 | } else { |
| 175 | // If there is a database, run migration in a background process |
| 176 | Migrations\Migration_Job::maybe_dispatch(); |
| 177 | } |
| 178 | |
| 179 | $geo_database = new Geo_Database(); |
| 180 | $geo_database->maybe_download(); |
| 181 | update_option( 'iawp_need_clear_cache', true ); |
| 182 | } ); |
| 183 | register_deactivation_hook( __DIR__ . '/iawp.php', function () { |
| 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 = new Geo_Database(); |
| 200 | $geo_database->maybe_download(); |
| 201 | } ); |
| 202 | // Since WC 4.3.0 |
| 203 | add_action( 'woocommerce_checkout_order_created', function ( $order ) { |
| 204 | global $wpdb ; |
| 205 | $views_table = Query::get_table_name( Query::VIEWS ); |
| 206 | $sessions_table = Query::get_table_name( Query::SESSIONS ); |
| 207 | $wc_orders_table = Query::get_table_name( Query::WC_ORDERS ); |
| 208 | $order_id = $order->id; |
| 209 | $visitor = new Visitor( Request::ip(), Request::user_agent() ); |
| 210 | $most_recent_view = $wpdb->get_row( $wpdb->prepare( "SELECT views.id as id FROM {$views_table} AS views LEFT JOIN {$sessions_table} AS sessions ON sessions.session_id = views.session_id WHERE sessions.visitor_id = %s ORDER BY views.viewed_at DESC LIMIT 1", $visitor->id() ) ); |
| 211 | if ( is_null( $most_recent_view ) ) { |
| 212 | return; |
| 213 | } |
| 214 | $existing_wc_order = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wc_orders_table} WHERE order_id = %d", $order_id ) ); |
| 215 | if ( !is_null( $existing_wc_order ) ) { |
| 216 | return; |
| 217 | } |
| 218 | $wpdb->insert( $wc_orders_table, [ |
| 219 | 'view_id' => $most_recent_view->id, |
| 220 | 'order_id' => $order_id, |
| 221 | 'created_at' => ( new \DateTime() )->format( 'Y-m-d H:i:s' ), |
| 222 | ] ); |
| 223 | } ); |