Skip to content

Commit 90d495a

Browse files
committed
Cron & others
1 parent 2607e6e commit 90d495a

File tree

5 files changed

+64
-25
lines changed

5 files changed

+64
-25
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Go through the files in `/lib/` and `/src/`. First one contains classes for extr
3737

3838
`/lib/script.php` :: `PLUGIN_SCRIPT` to add required CSS and JS.
3939

40+
`/lib/cron.php` :: `PLUGIN_CRON` to add cron actions and callbacks.
41+
4042
### `/src` Files
4143

4244
`/src/install.php` :: `PLUGIN_INSTALL` to handle activation process.
@@ -49,4 +51,4 @@ Go through the files in `/lib/` and `/src/`. First one contains classes for extr
4951

5052
`/src/metabox.php` :: `PLUGIN_METABOX` to add custom metabox in editor screen.
5153

52-
`/src/shortcode.php`:: `PLUGIN_SHORTCODE` to add and display shortcodes.
54+
`/src/shortcode.php`:: `PLUGIN_SHORTCODE` to add and display shortcodes.

autoload.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,30 @@ public function installation() {
2727
);
2828
$install->execute();
2929
}
30-
31-
$this->corn();
3230
*
3331
*/
3432
}
3533

3634

3735

3836
//Custom corn class, register it while activation
39-
public function corn() {
37+
public function cron_activation() {
4038

4139
if ( class_exists( 'PLUGIN_CRON' ) ) {
4240
$cron = new PLUGIN_CRON();
4341
$schedule = $cron->schedule_task(
4442
array(
4543
'timestamp' => current_time('timestamp'),
46-
'recurrence' => 'amaz_24_hrs',
44+
//'schedule' can be 'hourly', 'daily', 'weekly' or anything custom as defined in PLUGIN_CRON
45+
'recurrence' => 'schedule',
4746
// Use custom_corn_hook to hook into any cron process, anywhere in the plugin.
48-
'hook' => 'custom_corn_hook'
47+
'hook' => 'custom_cron_hook'
4948
) );
5049
}
5150

5251
}
5352

5453

55-
5654
public function db_install() {
5755

5856
/**
@@ -125,6 +123,23 @@ public function db_uninstall() {
125123
}
126124

127125

126+
public function do_cron_job_function() {
127+
128+
//Do cron function
129+
}
130+
131+
132+
public function custom_cron_hook_cb() {
133+
134+
add_action('custom_cron_hook', array( $this, 'do_cron_job_function'));
135+
}
136+
137+
138+
public function cron_uninstall() {
139+
140+
wp_clear_scheduled_hook('custom_cron_hook');
141+
}
142+
128143

129144
//Include scripts
130145
public function scripts() {
@@ -204,11 +219,14 @@ public function __construct() {
204219
$this->functionality();
205220

206221
register_activation_hook( PLUGIN_FILE, array( $this, 'db_install' ) );
222+
register_activation_hook( PLUGIN_FILE, array($this, 'cron_activation' ));
207223

208224
//remove the DB upon uninstallation
209225
register_uninstall_hook( PLUGIN_FILE, array( 'PLUGIN_BUILD', 'db_uninstall' ) ); //$this won't work here.
226+
register_uninstall_hook( PLUGIN_FILE, array( 'PLUGIN_BUILD', 'cron_uninstall' ) );
210227

211228
add_action('init', array($this, 'installation'));
229+
add_action('init', array($this, 'custom_cron_hook_cb'));
212230

213231
$this->scripts();
214232

lib/cron.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
/**
55
*
6-
* Add settings page with regarding options
6+
* Add Cron schedules and cron task callback
77
*
88
*/
99
if ( ! class_exists( 'PLUGIN_CRON' ) ) {
@@ -12,12 +12,17 @@ final class PLUGIN_CRON {
1212

1313
public function __construct() {
1414

15+
//Add cron schedules
1516
add_filter('cron_schedules', array( $this, 'cron_schedules' ) );
17+
18+
//Add cron callbacks
19+
1620
}
1721

1822
public function cron_schedules( $schedules ) {
1923

2024
$prefix = 'prefix_'; // Avoid conflict with other crons. Example Reference: cron_30_mins
25+
2126
// Example schedule options
2227
$schedule_options = array(
2328
'24_hrs' => array(
@@ -37,15 +42,19 @@ public function cron_schedules( $schedules ) {
3742
/* Add each custom schedule into the cron job system. */
3843
foreach($schedule_options as $schedule_key => $schedule){
3944

40-
$schedules[$prefix.$schedule_key] = array(
45+
if(!isset($schedules[$prefix.$schedule_key])) {
46+
47+
$schedules[$prefix.$schedule_key] = array(
4148
'interval' => $schedule['interval'],
4249
'display' => __('Every '.$schedule['display'])
4350
);
51+
}
4452
}
4553

4654
return $schedules;
4755
}
4856

57+
//Called in autoload.php
4958
public function schedule_task($task) {
5059

5160
if( ! $task ) {
@@ -76,4 +85,4 @@ public function schedule_task($task) {
7685
return true;
7786
}
7887
}
79-
} ?>
88+
} ?>

src/query.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ public function __construct() {
2323
: (get_query_var('page') ?
2424
get_query_var('page') : 1);
2525

26-
$args = $this->user_args($paged); // OR $this->post_args($paged)
26+
$post_args = $this->post_args($paged);
27+
$the_query = new WP_Query( $post_args );
2728

28-
$the_query = new WP_Query( $args );
29+
/**
30+
$user_args = $this->user_args($paged);
31+
$the_query = new WP_User_Query( $user_args );
32+
*/
2933

3034
// The Loop
3135
if ( $the_query->have_posts() ) {

src/settings.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public function __construct() {
4848

4949
/**
5050
* Add menues and hooks
51-
*
52-
add_action( 'admin_menu', array( $this, 'add_settings' ) );
51+
*
52+
add_action( 'admin_init', array( $this, 'add_settings' ) );
5353
add_action( 'admin_menu', array( $this, 'menu_page' ) );
5454
add_action( 'admin_menu', array( $this, 'sub_menu_page' ) );
5555
add_filter( 'set-screen-option', array( $this, 'set_screen' ), 10, 3 );
@@ -102,9 +102,9 @@ public function sub_menu_page() {
102102

103103
//Set screen option
104104
public function set_screen($status, $option, $value) {
105-
105+
106106
if ( 'option_name_per_page' == $option ) return $value; // Related to PLUGIN_TABLE()
107-
//return $status;
107+
//return $status;
108108
}
109109

110110

@@ -136,8 +136,8 @@ public function menu_page_callback() { ?>
136136
* Following is the settings form
137137
*/ ?>
138138
<form method="post" action="">
139-
<?php settings_fields("addPdfsId");
140-
do_settings_sections("addPdfs");
139+
<?php settings_fields("settings_name");
140+
do_settings_sections("settings_name");
141141
submit_button( __( 'Save', 'textdomain' ), 'primary', 'id' ); ?>
142142
</form>
143143

@@ -177,25 +177,31 @@ public function help_tabs() {
177177
//Add different types of settings and corrosponding sections
178178
public function add_settings() {
179179

180-
add_settings_section( 'SettingsId', __( 'Section Name', 'textdomain' ), array( $this,'SectionCb' ), 'SettingsName' );
181-
register_setting( 'SettingsId', 'SettingsField' );
182-
add_settings_field( 'SettingsFieldName', __( 'Field Name', 'textdomain' ), array( $this, 'SettingsFieldCb' ), 'SettingsName', 'SettingsId' );
180+
add_settings_section( 'settings_id', __( 'Section Name', 'textdomain' ), array( $this,'section_cb' ), 'settings_name' );
181+
register_setting( 'settings_name', 'settings_field' );
182+
add_settings_field( 'settings_field_name', __( 'Field Name', 'textdomain' ), array( $this, 'settings_field_cb' ), 'settings_name', 'settings_id' );
183183
}
184184

185185

186186

187187
//Section description
188-
public function SectionCb() {
188+
public function section_cb() {
189189

190190
echo '<p class="description">' . __( 'Set up settings', 'textdomain' ) . '</p>';
191191
}
192192

193193

194194

195195
//Field explanation
196-
public function SettingsFieldCb() {
196+
public function settings_field_cb() {
197197

198-
echo '<input type="text" class="medium-text" name="SettingsFieldName" id="SettingsFieldName" value="' . get_option('SettingsFieldName') . '" placeholder="' . __( 'Enter Value', 'textdomain' ) . '" required />';
198+
//Choose any one from input, textarea, select or checkbox
199+
/**
200+
echo '<input type="text" class="medium-text" name="settings_field_name" id="settings_field_name" value="' . get_option('settings_field_name') . '" placeholder="' . __( 'Enter Value', 'textdomain' ) . '" required />';
201+
echo '<textarea name="settings_field_name" id="settings_field_name" value="' . get_option('settings_field_name') . '>'. __( 'Enter Value', 'textdomain' ) . '</textarea>';
202+
echo '<select name="settings_field_name" id="settings_field_name"><option value="value" ' . selected( 'value', get_option('settings_field_name'), false) . '>Value</option></select>';
203+
echo '<input type="checkbox" id="settings_field_name" name="settings_field_name" value="1"' . checked( 1, get_option('settings_field_name'), false ) . '/>';
204+
*/
199205
}
200206
}
201-
} ?>
207+
} ?>

0 commit comments

Comments
 (0)