AJAX
2 years ago
Admin_Page
2 years ago
Date_Range
2 years ago
Interval
2 years ago
Menu_Bar_Stats
2 years ago
Migrations
2 years ago
Models
2 years ago
Public_API
2 years ago
Rows
2 years ago
Statistics
2 years ago
Tables
2 years ago
Utils
2 years ago
Campaign_Builder.php
2 years ago
Capability_Manager.php
2 years ago
Chart.php
2 years ago
Chart_Geo.php
2 years ago
Cron_Manager.php
2 years ago
Current_Traffic_Finder.php
2 years ago
Dashboard_Options.php
2 years ago
Dashboard_Widget.php
2 years ago
Database_Manager.php
2 years ago
Email_Chart.php
2 years ago
Email_Reports.php
2 years ago
Empty_Report_Option.php
2 years ago
Env.php
2 years ago
Filters.php
2 years ago
Geo_Database_Background_Job.php
2 years ago
Geo_Database_Manager.php
2 years ago
Geoposition.php
2 years ago
Icon_Directory.php
2 years ago
Icon_Directory_Factory.php
2 years ago
Illuminate_Builder.php
2 years ago
Independent_Analytics.php
2 years ago
Interrupt.php
2 years ago
Known_Referrers.php
2 years ago
Plugin_Conflict_Detector.php
2 years ago
Query.php
2 years ago
Quick_Stats.php
2 years ago
REST_API.php
2 years ago
Real_Time.php
2 years ago
Report.php
2 years ago
Report_Finder.php
2 years ago
Report_Options_Parser.php
2 years ago
Resource_Identifier.php
2 years ago
Settings.php
2 years ago
Sort_Configuration.php
2 years ago
Track_Resource_Changes.php
2 years ago
View.php
2 years ago
View_Counter.php
2 years ago
Visitors_Over_Time_Finder.php
2 years ago
WP_Option_Cache_Bust.php
2 years ago
WooCommerce_Order.php
2 years ago
WooCommerce_Referrer_Meta_Box.php
2 years ago
Track_Resource_Changes.php
77 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\IAWP; |
| 4 | |
| 5 | use IAWP_SCOPED\IAWP\Models\Page_Author_Archive; |
| 6 | use IAWP_SCOPED\IAWP\Models\Page_Post_Type_Archive; |
| 7 | use IAWP_SCOPED\IAWP\Models\Page_Singular; |
| 8 | use IAWP_SCOPED\IAWP\Models\Page_Term_Archive; |
| 9 | use IAWP_SCOPED\Illuminate\Database\Query\JoinClause; |
| 10 | use IAWP_SCOPED\Proper\Periodic; |
| 11 | /** @internal */ |
| 12 | class Track_Resource_Changes |
| 13 | { |
| 14 | public function __construct() |
| 15 | { |
| 16 | \add_action('wp_after_insert_post', [$this, 'handle_updated_post'], 10, 1); |
| 17 | \add_action('profile_update', [$this, 'handle_updated_author']); |
| 18 | \add_action('edit_term', [$this, 'handle_updated_term']); |
| 19 | if (Periodic::check('iawp_last_updated_post_type_cache', 'PT1M')) { |
| 20 | \add_action('registered_post_type', [$this, 'handle_registered_post_type']); |
| 21 | } |
| 22 | } |
| 23 | public function handle_updated_post($post_id) |
| 24 | { |
| 25 | $post = \get_post($post_id); |
| 26 | if (\is_null($post) || $post->post_status === 'trash') { |
| 27 | return; |
| 28 | } |
| 29 | $row = (object) ['resource' => 'singular', 'singular_id' => $post_id]; |
| 30 | $page = new Page_Singular($row); |
| 31 | $page->update_cache(); |
| 32 | $campaigns_table = Query::get_table_name(Query::CAMPAIGNS); |
| 33 | $sessions_table = Query::get_table_name(Query::SESSIONS); |
| 34 | $views_table = Query::get_table_name(Query::VIEWS); |
| 35 | $resources_table = Query::get_table_name(Query::RESOURCES); |
| 36 | Illuminate_Builder::get_builder()->from($campaigns_table, 'campaigns')->join("{$sessions_table} AS sessions", function (JoinClause $join) { |
| 37 | $join->on('sessions.campaign_id', '=', 'campaigns.campaign_id'); |
| 38 | })->join("{$views_table} AS views", function (JoinClause $join) { |
| 39 | $join->on('views.id', '=', 'sessions.initial_view_id'); |
| 40 | })->join("{$resources_table} AS resources", function (JoinClause $join) { |
| 41 | $join->on('resources.id', '=', 'views.resource_id'); |
| 42 | })->where('resources.singular_id', '=', $post_id)->update(['campaigns.landing_page_title' => $page->title()]); |
| 43 | } |
| 44 | public function handle_updated_author($user_id) |
| 45 | { |
| 46 | $row = (object) ['resource' => 'author', 'author_id' => $user_id]; |
| 47 | $page = new Page_Author_Archive($row); |
| 48 | $page->update_cache(); |
| 49 | } |
| 50 | public function handle_registered_post_type($post_type) |
| 51 | { |
| 52 | $post_type_object = \get_post_type_object($post_type); |
| 53 | if ($post_type_object->_builtin == \false) { |
| 54 | $row = (object) ['resource' => 'post_type_archive', 'post_type' => $post_type]; |
| 55 | $page = new Page_Post_Type_Archive($row); |
| 56 | $page->update_cache(); |
| 57 | } |
| 58 | } |
| 59 | // Works for tag, categories, and custom taxonomies. Keep in mind that terms for custom taxonomies might just |
| 60 | // disappear if the custom taxonomy is not registered the next time around. |
| 61 | public function handle_updated_term($term_id) |
| 62 | { |
| 63 | // Term must be cleared from the cache in order to use the new term data |
| 64 | \clean_term_cache($term_id); |
| 65 | $row = (object) ['term_id' => $term_id]; |
| 66 | $page = new Page_Term_Archive($row); |
| 67 | $page->update_cache(); |
| 68 | $posts = \get_posts(['post_type' => \get_post_types(), 'category' => $term_id, 'numberposts' => -1]); |
| 69 | // Update cache for all singulars associated with a given term |
| 70 | foreach ($posts as $post) { |
| 71 | $row = (object) ['resource' => 'singular', 'singular_id' => $post->ID]; |
| 72 | $page = new Page_Singular($row); |
| 73 | $page->update_cache(); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 |