-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrafty-main.php
303 lines (249 loc) · 7.66 KB
/
drafty-main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/**
* Prevent this script being called outside WordPress
*/
if ( ! defined('ABSPATH') ) {
exit;
}
/**
* Define some constants to keep or code DRY
*/
if ( ! defined( 'DRAFTY_BASENAME' ) ) {
define( 'DRAFTY_BASENAME', plugin_basename( DRAFTY_FILE ) );
}
if ( ! defined( 'DRAFTY_PATH' ) ) {
define( 'DRAFTY_PATH', plugin_dir_path( DRAFTY_FILE ) );
}
/**
* Load our plugin dependencies
*/
require_once DRAFTY_PATH . '/vendor/autoload.php';
use Drafty\Repositories\Admin\Admin;
use Drafty\Repositories\Email\Email;
use Drafty\Repositories\Options\Options;
use Drafty\Repositories\Scheduler\Scheduler;
/**
* Instantiated version of our plugin class
*/
$GLOBALS['Drafty_In_Here'] = new Drafty_In_Here();
/**
* Our plugin hooks
*/
register_activation_hook( DRAFTY_FILE, array( 'Drafty_In_Here', 'activate' ) );
register_deactivation_hook( DRAFTY_FILE, array( 'Drafty_In_Here', 'deactivate' ) );
class Drafty_In_Here
{
/**
* Make our plugin options available to the whole class
*/
public static $options = null;
/**
* The name of the wp_cron event our plugin will use
*/
public static $cron_name = 'drafty_in_here_event';
/**
* Variable to hold an instance of our plugin
*/
private static $_this;
public function __construct() {
self::$_this = $this;
// Load translation files
add_action( 'init', array( $this, 'load_text_domain' ) );
// Add a new wp_cron schedule option
add_filter( 'cron_schedules', array( $this, 'add_weekly_cron_schedule' ) );
// Bind callback to wp_cron event
add_action( self::$cron_name, array( $this, 'cron_callback' ) );
// Add a settings link to our plugin listing in the plugin admin section
$prefix = is_network_admin() ? 'network_admin_' : '';
add_filter( $prefix . 'plugin_action_links_' . DRAFTY_BASENAME, array( $this, 'add_action_link' ), 10, 2 );
// Load plugin options
self::$options = Options::get( 'drafty_options' );
$this->load_admin();
}
/**
* Method to allow users to override any filters or hooks we set.
*
* You can use this method like the following example:
* ````
* $instance = Drafty_In_Here::this();
* remove_filter( 'SOME_FILTER', array( $instance, 'SOME_METHOD_NAME' ) );
* ````
* Where SOME_FILTER would be the WordPress filter and SOME_METHOD_NAME would be a method inside our plugin.
*
* @return Drafty_In_Here
*/
static function this()
{
return self::$_this;
}
/**
* Method for when plugin is activated
*/
static function activate()
{
self::options_init();
}
/**
* Method for when plugin deactivated
*/
static function deactivate()
{
Scheduler::remove( self::$cron_name );
}
/**
* Load or create our plugin options
* @return void
*/
private static function options_init()
{
$slug = 'drafty_options';
$prototype = array(
'email_address' => Options::get( 'admin_email '),
'drafty_frequency' => '',
);
self::$options = Options::get( $slug );
if (! empty( self::$options ) ) {
return;
}
Options::save( $slug, $prototype );
}
/**
* Initializes the Admin class
*/
private function load_admin()
{
$admin = new Admin;
}
/**
* Loads user translation files
*/
public function load_text_domain()
{
$domain = 'drafty-in-here';
// The "plugin_locale" filter is also used in load_plugin_textdomain()
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
load_textdomain( $domain, WP_LANG_DIR . '/drafty-in-here/' . $domain . '-' . $locale . '.mo' );
load_plugin_textdomain( $domain, false, dirname( DRAFTY_BASENAME ) . '/languages/' );
}
/**
* Add Settings link in plugins admin section
*
* @param $links
* @param $file
* @return mixed
*/
public function add_action_link( $links, $file ) {
if ( $file != DRAFTY_BASENAME ) {
return $links;
}
$review_link = '<a href="https://wordpress.org/support/view/plugin-reviews/drafty-in-here">' . __( 'Review', 'drafty-in-here' ) . '</a>';
array_unshift( $links, $review_link );
$support_link = '<a href="https://wordpress.org/plugins/drafty-in-here/faq/">' . __( 'FAQ', 'drafty-in-here' ) . '</a>';
array_unshift( $links, $support_link );
$settings_link = '<a href="' . esc_url( admin_url( 'options-general.php?page=' . dirname( DRAFTY_BASENAME ) ) ) . '">' . __( 'Settings', 'drafty-in-here' ) . '</a>';
array_unshift( $links, $settings_link );
return $links;
}
/**
* Gets posts of given type with given status.
*
* @param string $type
* @param string $status
* @return WP_Query
* @throws Exception
* @internal param or $string array $type the post type
* @internal param or $string array $status the post status
*
*/
public function get_posts( $type = 'post', $status = 'draft' )
{
$args = array(
'post_type' => $type,
'post_status' => $status,
);
$posts = new WP_Query( $args );
if ( ! $posts->have_posts() ) {
throw new \Exception( sprintf( __( '%s can not send you an email, because you do not have any draft posts.', 'drafty-in-here' ), 'Drafty In Here' ), 1 );
}
return $posts;
}
/**
* Adds weekly frequency option to the existing wp_cron schedules.
*
* @param array $schedules An array of wp_cron schedules.
* @return array The array of wp_cron schedules.
*/
public function add_weekly_cron_schedule( $schedules ) {
$schedules['weekly'] = array(
'interval' => 604800,
'display' => __( 'Once Weekly', 'drafty-in-here' )
);
return $schedules;
}
/**
* Method called by wp_con event
*
* Here we build up the email contents and pass it to our Email class.
* @todo: return a call to an event rather than boolean
*
* @param bool $test Weather we are testing the callback or not
* @return bool|void If no draft posts are found returns false
*/
public function cron_callback( $test = false )
{
try {
$posts = $this->get_posts();
} catch (\Exception $e) {
// we have no posts
if ( false === $test ) return false;
$posts = null;
}
if ( $test || $posts) {
$to = self::$options['email_address'];
$subject = __( 'You have drafts waiting to be published', 'drafty-in-here' );
$text = $this->build_message( $posts, $test );
$html = '<html><body>' . nl2br( $text ) . '</body></html>';
Email::to( $to )->subject( $subject )->text( $text )->HTML( $html )->send();
return true;
}
return false;
}
public function build_message( $posts, $test )
{
$message = __( 'Hello,', 'drafty-in-here' );
$message .= "\r\n\r\n";
if ( $posts ) {
$message .= sprintf( _n( 'Right now on %s you have one draft post:',
'Right now on %s you have %s draft posts:',
$posts->post_count,
'drafty-in-here'
),
Options::get( 'blogname' ),
$posts->post_count
);
}
if ( ! $posts && $test ) {
$message .= sprintf( __( 'Right now on %s you have zero draft posts:', 'drafty-in-here' ), Options::get( 'blogname' ) );
}
$message .= "\r\n\r\n";
if ( $posts ) {
while ( $posts->have_posts() ) {
$posts->the_post();
$message .= "* " . get_the_title() . " - " . __( 'last updated', 'drafty-in-here' ) . ' ' . get_the_date() . "\r\n";
}
}
$message .= "\r\n";
$message .= __( 'So what are you waiting for?', 'drafty-in-here' );
$message .= "\r\n";
if ( $posts ) {
$message .= _n( 'Login and publish it today - like a boss! :-)', 'Login and publish them today - like a boss! :-)', $posts->post_count, 'drafty-in-here' );
}
if ( ! $posts && $test ) {
$message .= __( 'Login and write a new post today - like a boss! :-)', 'drafty-in-here' );
}
$message .= "\r\n\r\n" . admin_url();
$message .= "\r\n\r\n-------------------------------------------------------------\r\n\r\n";
$message .= sprintf( __( 'E-mail generated by %s', 'drafty-in-here' ), 'Drafty In Here' );
return $message;
}
}