Making WordPress.org

Ticket #3434: 3434.2.diff

File 3434.2.diff, 4.0 KB (added by obenland, 9 years ago)
  • class-manager.php

     
    2020                add_action( 'plugin_directory_meta_sync', array( __NAMESPACE__ . '\Meta_Sync', 'cron_trigger' ) );
    2121                add_action( 'plugin_directory_svn_sync', array( __NAMESPACE__ . '\SVN_Watcher', 'cron_trigger' ) );
    2222                add_action( 'plugin_directory_update_api_check', array( __NAMESPACE__ . '\API_Update_Updater', 'cron_trigger' ) );
     23                add_action( 'plugin_directory_plugin_support_resolved', array( __NAMESPACE__ . '\Plugin_Support_Resolved', 'cron_trigger' ) );
    2324
    2425                // A cronjob to check cronjobs
    2526                add_action( 'plugin_directory_check_cronjobs', array( $this, 'register_cron_tasks' ) );
  • class-plugin-support-resolved.php

     
     1<?php
     2/**
     3 * Updates stats on resolved and unresolved support requests.
     4 *
     5 * @package WordPressdotorg\Plugin_Directory\Jobs
     6 */
     7
     8namespace WordPressdotorg\Plugin_Directory\Jobs;
     9
     10use WordPressdotorg\Plugin_Directory\Plugin_Directory;
     11
     12/**
     13 * Class Plugin_Support_Resolved
     14 *
     15 * @package WordPressdotorg\Plugin_Directory\Jobs
     16 */
     17class Plugin_Support_Resolved {
     18
     19        /**
     20         * The cron trigger for the import job.
     21         *
     22         * @static
     23         * @global wpdb $wpdb WordPress database class.
     24         */
     25        public static function cron_trigger() {
     26                global $wpdb;
     27
     28                // Support resolutions are on a 2 month rolling period.
     29                $time_limit   = date( 'Y-m-d 00:00:00', strtotime( '-2 months' ) );
     30                $plugin_stats = [];
     31
     32                $wpdb->set_blog_id( WPORG_SUPPORT_FORUMS_BLOGID );
     33                // phpcs:ignore WordPress.VIP.DirectDatabaseQuery
     34                $results = $wpdb->get_results( $wpdb->prepare( "SELECT t.slug as plugin_slug, meta_value as resolved, count(*) as topic_count
     35FROM {$wpdb->posts} p
     36        JOIN {$wpdb->postmeta} pm ON (p.ID = pm.post_id AND pm.meta_key = 'topic_resolved')
     37        JOIN {$wpdb->term_relationships} tr ON (p.ID = tr.object_id)
     38        JOIN {$wpdb->term_taxonomy} tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'topic-plugin')
     39        JOIN {$wpdb->terms} t ON (tt.term_id = t.term_id)
     40WHERE
     41        post_type = 'topic' AND
     42        post_status = 'publish' AND
     43        post_parent = (SELECT ID FROM {$wpdb->posts} WHERE post_type = 'forum' AND post_name = 'plugins-and-hacks') AND
     44        post_date >= %s
     45
     46GROUP BY t.slug, meta_value", $time_limit ) );
     47                $wpdb->set_blog_id( WPORG_PLUGIN_DIRECTORY_BLOGID );
     48
     49                foreach ( $results as $result ) {
     50                        if ( ! isset( $plugin_stats[ $result->plugin_slug ] ) ) {
     51                                $plugin_stats[ $result->plugin_slug ] = [
     52                                        'yes' => 0,
     53                                        'no'  => 0,
     54                                        'mu'  => 0,
     55                                ];
     56                        }
     57
     58                        $plugin_stats[ $result->plugin_slug ][ $result->resolved ] = (int) $result->topic_count;
     59                }
     60
     61                foreach ( array_chunk( $plugin_stats, 1000, true ) as $plugin_stats_chunk ) {
     62                        foreach ( $plugin_stats_chunk as $plugin_slug => $stats ) {
     63                                $plugin = Plugin_Directory::get_plugin_post( $plugin_slug );
     64                                if ( ! $plugin || 'publish' !== $plugin->post_status ) {
     65                                        continue;
     66                                }
     67
     68                                update_post_meta( $plugin->ID, 'support_threads', wp_slash( $stats ? $stats['yes'] + $stats['no'] : 0 ) );
     69                                update_post_meta( $plugin->ID, 'support_threads_resolved', wp_slash( $stats ? $stats['yes'] : 0 ) );
     70                        }
     71
     72                        self::stop_the_insanity();
     73                }
     74        }
     75
     76        /**
     77         * Clear caches for memory management.
     78         *
     79         * @static
     80         * @global \wpdb            $wpdb
     81         * @global \WP_Object_Cache $wp_object_cache
     82         */
     83        protected static function stop_the_insanity() {
     84                global $wpdb, $wp_object_cache;
     85
     86                $wpdb->queries = [];
     87
     88                if ( is_object( $wp_object_cache ) ) {
     89                        $wp_object_cache->group_ops      = [];
     90                        $wp_object_cache->stats          = [];
     91                        $wp_object_cache->memcache_debug = [];
     92                        $wp_object_cache->cache          = [];
     93
     94                        if ( method_exists( $wp_object_cache, '__remoteset' ) ) {
     95                                $wp_object_cache->__remoteset(); // Important.
     96                        }
     97                }
     98        }
     99}