-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauto-delete.php
More file actions
320 lines (277 loc) · 11 KB
/
Copy pathauto-delete.php
File metadata and controls
320 lines (277 loc) · 11 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/**
* Add-on for Hirezoot for automatically deleting applications based on the specified time.
*
* @package wp-job-openings
*/
/**
* Plugin Name: Auto Delete Applications - Add-on for Hirezoot
* Plugin URI: https://hirezoot.com/
* Description: This is an add-on for Hirezoot Plugin, which will let you delete the received applications periodically.
* Author: AWSM Innovations
* Author URI: https://awsm.in/
* Version: 1.0.4
* License: GPLv2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text domain: auto-delete-wp-job-openings
* Domain Path: /languages
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Plugin Constants
if ( ! defined( 'AWSM_JOBS_MAIN_PLUGIN' ) ) {
define( 'AWSM_JOBS_MAIN_PLUGIN', 'wp-job-openings/wp-job-openings.php' );
}
if ( ! defined( 'AWSM_JOBS_ADL_MAIN_REQ_VERSION' ) ) {
define( 'AWSM_JOBS_ADL_MAIN_REQ_VERSION', '1.4' );
}
if ( ! defined( 'AWSM_JOBS_ADL_PLUGIN_BASENAME' ) ) {
define( 'AWSM_JOBS_ADL_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
}
if ( ! class_exists( 'AWSM_Job_Openings_Auto_Delete_Addon' ) ) {
class AWSM_Job_Openings_Auto_Delete_Addon {
private static $instance = null;
protected $cpath = null;
public function __construct() {
$this->cpath = untrailingslashit( plugin_dir_path( __FILE__ ) );
add_action( 'init', array( $this, 'cron_jobs' ) );
add_action( 'admin_init', array( $this, 'handle_plugin_activation' ) );
add_action( 'awsm_jobs_adl_applications', array( $this, 'handle_old_applications' ) );
add_action( 'before_delete_post', array( $this, 'remove_attachments' ) );
add_action( 'admin_init', array( $this, 'register_settings' ) );
add_action( 'update_option_awsm_jobs_adl_general_settings', array( $this, 'update_awsm_jobs_adl_general_settings' ) );
add_filter( 'awsm_jobs_general_settings_fields', array( $this, 'awsm_jobs_general_settings_fields' ) );
}
public static function init() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
public function activate() {
$this->register_default_settings();
}
public function deactivate() {
$this->clear_cron_jobs();
}
public function cron_jobs() {
$settings = self::get_general_settings();
if ( $settings['enable_auto_delete'] === 'enable' && ! wp_next_scheduled( 'awsm_jobs_adl_applications' ) ) {
wp_schedule_event( time() + DAY_IN_SECONDS, 'daily', 'awsm_jobs_adl_applications' );
}
}
public function clear_cron_jobs() {
wp_clear_scheduled_hook( 'awsm_jobs_adl_applications' );
}
public function handle_plugin_activation() {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
if ( is_plugin_inactive( AWSM_JOBS_MAIN_PLUGIN ) || ! class_exists( 'AWSM_Job_Openings' ) ) {
add_action(
'admin_notices',
function() {
$this->admin_notices();
}
);
deactivate_plugins( AWSM_JOBS_ADL_PLUGIN_BASENAME );
}
if ( defined( 'AWSM_JOBS_PLUGIN_VERSION' ) ) {
if ( version_compare( AWSM_JOBS_PLUGIN_VERSION, AWSM_JOBS_ADL_MAIN_REQ_VERSION, '<' ) ) {
add_action(
'admin_notices',
function() {
$this->admin_notices( false );
}
);
deactivate_plugins( AWSM_JOBS_ADL_PLUGIN_BASENAME );
}
}
}
public function get_main_plugin_activation_link( $is_update = false ) {
$content = $link_action = $action_url = $link_class = ''; // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found
if ( ! $is_update ) {
// when plugin is not active.
$link_action = esc_html__( 'Activate', 'auto-delete-wp-job-openings' );
$action_url = wp_nonce_url( self_admin_url( 'plugins.php?action=activate&plugin=' . AWSM_JOBS_MAIN_PLUGIN ), 'activate-plugin_' . AWSM_JOBS_MAIN_PLUGIN );
$link_class = ' activate-now';
// when plugin is not installed.
$plugin_arr = explode( '/', esc_html( AWSM_JOBS_MAIN_PLUGIN ) );
$plugin_slug = $plugin_arr[0];
$installed_plugin = get_plugins( '/' . $plugin_slug );
if ( empty( $installed_plugin ) ) {
if ( get_filesystem_method( array(), WP_PLUGIN_DIR ) === 'direct' ) {
$link_action = esc_html__( 'Install', 'auto-delete-wp-job-openings' );
$action_url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=' . $plugin_slug ), 'install-plugin_' . $plugin_slug );
$link_class = ' install-now';
}
}
} else {
// when plugin needs an update.
$link_action = esc_html__( 'Update', 'auto-delete-wp-job-openings' );
$action_url = wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' . AWSM_JOBS_MAIN_PLUGIN ), 'upgrade-plugin_' . AWSM_JOBS_MAIN_PLUGIN );
$link_class = ' update-now';
}
if ( ! empty( $link_action ) && ! empty( $action_url ) && ! empty( $link_class ) ) {
$content = sprintf( '<a href="%2$s" class="button button-small%3$s">%1$s</a>', esc_html( $link_action ), esc_url( $action_url ), esc_attr( $link_class ) );
}
return $content;
}
public function admin_notices( $is_default = true, $req_plugin_version = AWSM_JOBS_ADL_MAIN_REQ_VERSION ) { ?>
<div class="updated error">
<p>
<?php
$req_plugin = sprintf( '<strong>"%s"</strong>', esc_html__( 'HireZoot', 'wp-job-openings' ) );
$plugin = sprintf( '<strong>"%s"</strong>', esc_html__( 'Auto Delete Applications - Add-on for HireZoot', 'auto-delete-wp-job-openings' ) );
if ( $is_default ) {
/* translators: %1$s: main plugin, %2$s: current plugin, %3$s: plugin activation link, %4$s: line break */
printf( esc_html__( 'The plugin %2$s needs the plugin %1$s active. %4$s Please %3$s %1$s', 'auto-delete-wp-job-openings' ), $req_plugin, $plugin, $this->get_main_plugin_activation_link(), '<br />' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} else {
/* translators: %1$s: main plugin, %2$s: current plugin, %3$s: minimum required version of the main plugin, %4$s: plugin updation link */
printf( esc_html__( '%2$s plugin requires %1$s version %3$s. Please %4$s %1$s plugin to the latest version.', 'auto-delete-wp-job-openings' ), $req_plugin, $plugin, esc_html( $req_plugin_version ), $this->get_main_plugin_activation_link( true ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
?>
</p>
</div>
<?php
}
public function awsm_jobs_general_settings_fields( $settings_fields ) {
ob_start();
include $this->cpath . '/inc/remove-applications.php';
$field_content = ob_get_clean();
$settings_fields['default'][] =
array(
'name' => 'awsm_jobs_adl_general_settings',
'label' => __( 'Auto delete applications ', 'auto-delete-wp-job-openings' ),
'type' => 'raw',
'value' => $field_content,
'description' => __( 'CAUTION: Checking this option will delete applications after the selected period from the date of application. (For example, if you configure the option for 6 months, all the applications you have received before 6 months will be deleted immediately and every application that completes 6 months will be deleted from next day onwards automatically).', 'auto-delete-wp-job-openings' ),
);
return $settings_fields;
}
private function settings() {
$settings = array(
'general' => array(
array(
'option_name' => 'awsm_jobs_adl_general_settings',
'callback' => array( $this, 'auto_delete_handler' ),
),
),
);
return $settings;
}
public static function get_default_general_settings() {
return array(
'enable_auto_delete' => '',
'count' => '6',
'period' => 'months',
'force_delete' => '',
);
}
public static function get_general_settings() {
$settings = get_option( 'awsm_jobs_adl_general_settings' );
if ( ! is_array( $settings ) ) {
$settings = array();
}
$defaults = self::get_default_general_settings();
$settings = wp_parse_args( $settings, $defaults );
return $settings;
}
private static function default_settings() {
$options = array(
'awsm_jobs_adl_general_settings' => self::get_default_general_settings(),
);
if ( ! empty( $options ) ) {
foreach ( $options as $option => $value ) {
if ( ! get_option( $option ) ) {
update_option( $option, $value );
}
}
}
}
private function register_default_settings() {
if ( intval( get_option( 'awsm_jobs_adl_register_default_settings' ) ) === 1 ) {
return;
}
self::default_settings();
update_option( 'awsm_jobs_adl_register_default_settings', 1 );
}
public function register_settings() {
$settings = $this->settings();
foreach ( $settings as $group => $settings_args ) {
foreach ( $settings_args as $setting_args ) {
register_setting( 'awsm-jobs-' . $group . '-settings', $setting_args['option_name'], isset( $setting_args['callback'] ) ? $setting_args['callback'] : 'sanitize_text_field' );
}
}
}
public function update_awsm_jobs_adl_general_settings() {
$this->clear_cron_jobs();
}
public function auto_delete_handler( $adl_options ) {
if ( ! is_array( $adl_options ) ) {
$adl_options = array();
}
$adl_options = wp_parse_args( $adl_options, self::get_default_general_settings() );
$options['enable_auto_delete'] = sanitize_text_field( $adl_options['enable_auto_delete'] );
$options['count'] = absint( $adl_options['count'] );
$options['period'] = sanitize_text_field( $adl_options['period'] );
$options['force_delete'] = sanitize_text_field( $adl_options['force_delete'] );
if ( ! $options['count'] ) {
$options['enable_auto_delete'] = '';
}
if ( $options['enable_auto_delete'] === 'enable' && current_user_can( 'delete_others_applications' ) ) {
$this->delete_applications( $options );
}
return $options;
}
public function delete_applications( $options ) {
$args = apply_filters(
'awsm_jobs_adl_query_args',
array(
'fields' => 'ids',
'post_status' => 'any',
'posts_per_page' => -1,
'date_query' => array(
array(
'column' => 'post_date_gmt',
'before' => sanitize_text_field( $options['count'] . ' ' . $options['period'] . ' ago' ),
),
),
)
);
$args['post_type'] = 'awsm_job_application';
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
if ( $options['force_delete'] === 'enable' ) {
wp_delete_post( get_the_ID(), true );
} else {
wp_trash_post( get_the_ID() );
}
}
}
}
public function handle_old_applications() {
$options = self::get_general_settings();
if ( $options['enable_auto_delete'] === 'enable' ) {
$this->delete_applications( $options );
}
}
public function remove_attachments( $post_id ) {
if ( get_post_type( $post_id ) === 'awsm_job_application' ) {
$attachments = get_attached_media( '', $post_id );
if ( ! empty( $attachments ) ) {
foreach ( $attachments as $attachment ) {
wp_delete_attachment( $attachment->ID, true );
}
}
}
}
}
}
$auto_delete_addon = AWSM_Job_Openings_Auto_Delete_Addon::init();
// activation
register_activation_hook( __FILE__, array( $auto_delete_addon, 'activate' ) );
// deactivation
register_deactivation_hook( __FILE__, array( $auto_delete_addon, 'deactivate' ) );