Completed
Branch BUG-9951-10331-8793-pue-fixes (40e696)
by
unknown
27:52 queued 13:57
created

EE_Messages_Scheduler::cleanup()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 0
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
1
<?php
2
defined('EVENT_ESPRESSO_VERSION') || exit;
3
4
/**
5
 * This class is used for setting scheduled tasks related to the EE_messages system.
6
 *
7
 * @package    Event Espresso
8
 * @subpackage messages
9
 * @author     Darren Ethier
10
 * @since      4.9.0
11
 */
12
class EE_Messages_Scheduler extends EE_Base
13
{
14
15
    /**
16
     * Number of seconds between batch sends/generates on the cron job.
17
     * Defaults to 5 minutes in seconds.  If you want to change this interval, you can use the native WordPress
18
     * `cron_schedules` filter and modify the existing custom `ee_message_cron` schedule interval added.
19
     *
20
     * @type int
21
     */
22
    const message_cron_schedule = 300;
23
24
    /**
25
     * Constructor
26
     */
27
    public function __construct()
28
    {
29
        //register tasks (and make sure only registered once).
30
        if (! has_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'))) {
31
            add_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'), 10);
32
        }
33
34
        //register callbacks for scheduled events (but make sure they are set only once).
35
        if (! has_action(
36
            'AHEE__EE_Messages_Scheduler__generation',
37
            array('EE_Messages_Scheduler', 'batch_generation')
38
        )) {
39
            add_action('AHEE__EE_Messages_Scheduler__generation', array('EE_Messages_Scheduler', 'batch_generation'));
40
            add_action('AHEE__EE_Messages_Scheduler__sending', array('EE_Messages_Scheduler', 'batch_sending'));
41
            add_action('AHEE__EE_Messages_Scheduler__cleanup', array('EE_Messages_Scheduler', 'cleanup'));
42
        }
43
44
        //add custom schedules
45
        add_filter('cron_schedules', array($this, 'custom_schedules'));
46
    }
47
48
49
    /**
50
     * Add custom schedules for wp_cron
51
     *
52
     * @param $schedules
53
     */
54
    public function custom_schedules($schedules)
55
    {
56
        $schedules['ee_message_cron'] = array(
57
            'interval' => self::message_cron_schedule,
58
            'display'  => __(
59
                'This is the cron time interval for EE Message schedules (defaults to once every 5 minutes)',
60
                'event_espresso'
61
            ),
62
        );
63
        return $schedules;
64
    }
65
66
67
    /**
68
     * Callback for FHEE__EEH_Activation__get_cron_tasks that is used to retrieve scheduled Cron events to add and
69
     * remove.
70
     *
71
     * @param array $tasks already existing scheduled tasks
72
     * @return array
73
     */
74
    public function register_scheduled_tasks($tasks)
75
    {
76
        EE_Registry::instance()->load_helper('DTT_Helper');
77
        $tasks['AHEE__EE_Messages_Scheduler__generation'] = 'ee_message_cron';
78
        $tasks['AHEE__EE_Messages_Scheduler__sending']    = 'ee_message_cron';
79
        $tasks['AHEE__EE_Messages_Scheduler__cleanup'] = array( EEH_DTT_Helper::tomorrow(), 'daily');
80
        return $tasks;
81
    }
82
83
84
    /**
85
     * This initiates a non-blocking separate request to execute on a scheduled task.
86
     * Note: The EED_Messages module has the handlers for these requests.
87
     *
88
     * @param string $task The task the request is being generated for.
89
     */
90
    public static function initiate_scheduled_non_blocking_request($task)
91
    {
92
        if (apply_filters(
93
            'EE_Messages_Scheduler__initiate_scheduled_non_blocking_request__do_separate_request',
94
            true
95
        )) {
96
            $request_url  = add_query_arg(
97
                array_merge(
98
                    array('ee' => 'msg_cron_trigger'),
99
                    EE_Messages_Scheduler::get_request_params($task)
100
                ),
101
                site_url()
102
            );
103
            $request_args = array(
104
                'timeout'     => 300,
105
                'blocking'    => (defined('DOING_CRON') && DOING_CRON) || (defined('DOING_AJAX') && DOING_AJAX) ? true : false,
106
                'sslverify'   => false,
107
                'redirection' => 10,
108
            );
109
            $response     = wp_remote_get($request_url, $request_args);
110
            if (is_wp_error($response)) {
111
                trigger_error($response->get_error_message());
112
            }
113
        } else {
114
            EE_Messages_Scheduler::initiate_immediate_request_on_cron($task);
115
        }
116
    }
117
118
119
    /**
120
     * This returns
121
     * the request params used for a scheduled message task request.
122
     *
123
     * @param string $task The task the request is for.
124
     * @return array
125
     */
126
    public static function get_request_params($task)
127
    {
128
        //transient is used for flood control on msg_cron_trigger requests
129
        $transient_key = 'ee_trans_' . uniqid($task);
130
        set_transient($transient_key, 1, 5 * MINUTE_IN_SECONDS);
131
        return array(
132
            'type' => $task,
133
            'key'  => $transient_key,
134
        );
135
    }
136
137
138
    /**
139
     * This is used to execute an immediate call to the run_cron task performed by EED_Messages
140
     *
141
     * @param string $task The task the request is being generated for.
142
     */
143
    public static function initiate_immediate_request_on_cron($task)
144
    {
145
        $request_args = EE_Messages_Scheduler::get_request_params($task);
146
        //set those request args in the request so it gets picked up
147
        foreach ($request_args as $request_key => $request_value) {
148
            EE_Registry::instance()->REQ->set($request_key, $request_value);
149
        }
150
        EED_Messages::instance()->run_cron();
151
    }
152
153
154
    /**
155
     * Callback for scheduled AHEE__EE_Messages_Scheduler__generation wp cron event
156
     */
157 View Code Duplication
    public static function batch_generation()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
    {
159
        /**
160
         * @see filter usage in EE_Messages_Queue::initiate_request_by_priority()
161
         */
162
        if (! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
163
            || ! EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request
164
        ) {
165
            EE_Messages_Scheduler::initiate_immediate_request_on_cron('generate');
166
        }
167
    }
168
169
170
    /**
171
     * Callback for scheduled AHEE__EE_Messages_Scheduler__sending
172
     */
173 View Code Duplication
    public static function batch_sending()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
    {
175
        /**
176
         * @see filter usage in EE_Messages_Queue::initiate_request_by_priority()
177
         */
178
        if (! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
179
            || ! EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request
180
        ) {
181
            EE_Messages_Scheduler::initiate_immediate_request_on_cron('send');
182
        }
183
    }
184
185
186
    /**
187
     * This is the callback for the `AHEE__EE_Messages_Scheduler__cleanup` scheduled event action.
188
     * This runs once a day and if cleanup is active (set via messages settings), it will (by default) delete permanently
189
     * from the database messages that have a MSG_modified date older than 30 days.
190
     */
191
    public static function cleanup()
192
    {
193
        //first check if user has cleanup turned on or if we're in maintenance mode.  If in maintenance mode we'll wait
194
        //until the next scheduled event.
195
        if (! EE_Registry::instance()->CFG->messages->delete_threshold
196
            || ! EE_Maintenance_Mode::instance()->models_can_query()
197
        ) {
198
            return;
199
        }
200
201
        /**
202
         * This filter switch allows other code (such as the EE_Worker_Queue add-on) to replace this with its own handling
203
         * of deleting messages.
204
         */
205
        if (apply_filters('FHEE__EE_Messages_Scheduler__cleanup__handle_cleanup_on_cron', true)) {
206
            EEM_Message::instance()->delete_old_messages(EE_Registry::instance()->CFG->messages->delete_threshold);
207
        }
208
    }
209
210
} //end EE_Messages_Scheduler