PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.6.28
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.6.28
1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 1.8.0 All 210 releases
charitable / includes / libraries / wp-session / wp-cli.php

wp-cli.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.6.28, at includes/libraries/wp-session/wp-cli.php

158 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * CLI commands for WP Session Manager
5 *
6 * @package WP_Session
7 * @subpackage Commands
8 */
9 class WP_Session_Command extends \WP_CLI_Command {
10
11 /**
12 * Count the total number of sessions stored in the database.
13 *
14 *
15 * ## EXAMPLES
16 *
17 * wp session count
18 *
19 * @global wpdb $wpdb
20 *
21 * @param array $args
22 * @param array $assoc_args
23 */
24 public function count( $args, $assoc_args ) {
25 $sessions = WP_Session_Utils::count_sessions();
26
27 \WP_CLI::line( sprintf( '%d sessions currently exist.', absint( $sessions ) ) );
28 }
29
30 /**
31 * Delete sessions from the database.
32 *
33 * ## OPTIONS
34 *
35 * [--all]
36 * : Flag whether or not to purge all sessions from the database.
37 *
38 * [--batch=<batch>]
39 * : Set the batch size for deleting old sessions
40 *
41 * [--limit=<limit>]
42 * : Delete just this number of old sessions
43 *
44 * ## EXAMPLES
45 *
46 * wp session delete
47 * wp session delete [--batch=<batch>]
48 * wp session delete [--limit=<limit>]
49 * wp session delete [--all]
50 *
51 * @synopsis [--all] [--batch=<batch>] [--limit=<limit>]
52 *
53 * @param array $args
54 * @param array $assoc_args
55 */
56 public function delete( $args, $assoc_args ) {
57 if ( isset( $assoc_args['limit'] ) ) {
58 $limit = absint( $assoc_args['limit'] );
59
60 $count = WP_Session_Utils::delete_old_sessions( $limit );
61
62 if ( $count > 0 ) {
63 \WP_CLI::line( sprintf( 'Deleted %d sessions.', $count ) );
64 }
65
66 // Clear memory
67 self::free_up_memory();
68 return;
69 }
70
71 // Determine if we're deleting all sessions or just a subset.
72 $all = isset( $assoc_args['all'] );
73
74 /**
75 * Determine the size of each batch for deletion.
76 *
77 * @param int
78 */
79 $batch = isset( $assoc_args['batch'] ) ? absint( $assoc_args['batch'] ) : apply_filters( 'wp_session_delete_batch_size', 1000 );
80
81 switch ( $all ) {
82 case true:
83 $count = WP_Session_Utils::delete_all_sessions();
84
85 \WP_CLI::line( sprintf( 'Deleted all %d sessions.', $count ) );
86 break;
87 case false:
88 do {
89 $count = WP_Session_Utils::delete_old_sessions( $batch );
90
91 if ( $count > 0 ) {
92 \WP_CLI::line( sprintf( 'Deleted %d sessions.', $count ) );
93 }
94
95 // Clear memory
96 self::free_up_memory();
97 } while ( $count > 0 );
98 break;
99 }
100 }
101
102 /**
103 * Generate a number of dummy sessions for testing purposes.
104 *
105 * ## OPTIONS
106 *
107 * <count>
108 * : Number of sessions to create.
109 *
110 * [--expires=<date>]
111 * : Optional expiration time tagged for each session. Will use WordPress' local time.
112 *
113 * ## EXAMPLES
114 *
115 * wp session generate 5000
116 * wp session generate 5000 --expires="2014-11-09T08:00"
117 *
118 * @synopsis <count> [--expires=<date>]
119 *
120 * @param array $args
121 * @param array $assoc_args
122 */
123 public function generate( $args, $assoc_args ) {
124 $count = absint( $args[0] );
125 $date = isset( $assoc_args['expires'] ) ? $assoc_args['expires'] : null;
126
127 $notify = \WP_CLI\Utils\make_progress_bar( 'Generating sessions', $count );
128
129 for ( $i = 0; $i < $count; $i ++ ) {
130 WP_Session_Utils::create_dummy_session( $date );
131 $notify->tick();
132 }
133
134 $notify->finish();
135 }
136
137 /**
138 * Free up memory
139 *
140 * @global WP_Object_Cache $wp_object_cache
141 * @global wpdb $wpdb
142 */
143 private function free_up_memory() {
144 global $wp_object_cache, $wpdb;
145 $wpdb->queries = array();
146
147 if ( ! is_object( $wp_object_cache ) ) {
148 return;
149 }
150
151 $wp_object_cache->group_ops = array();
152 $wp_object_cache->stats = array();
153 $wp_object_cache->memcache_debug = array();
154 $wp_object_cache->cache = array();
155 }
156 }
157
158 \WP_CLI::add_command( 'session', 'WP_Session_Command' );