wp_cron scheduled but not firing - wordpress

I am trying to run a background script fired on a user action using wp_schedule_single_event, and although I have confirmed that the event is being scheduled and that wp_cron recognizes that the scheduled time has passed, it will not fire the event handler. To further complicate things, the code runs fine on my local WP install, but does nothing on my server.
To schedule the event, I am using:
if ( ! wp_next_scheduled( 'my_action_name' ) ) {
wp_schedule_single_event( time(), 'my_action_name' );
}
My handler action is defined as:
add_action('my_action_name', 'my_action_handler');
function my_action_handler () {
// do stuff
}
I have testing the wp core files and found that the following block (from wp-includes/cron.php:322) is where the script terminates:
var_dump('test1');
$cron_request = apply_filters( 'cron_request', array(
'url' => add_query_arg( 'doing_wp_cron', $doing_wp_cron, site_url( 'wp-cron.php' ) ),
'key' => $doing_wp_cron,
'args' => array(
'timeout' => 0.01,
'blocking' => false,
/** This filter is documented in wp-includes/class-wp-http-streams.php */
'sslverify' => apply_filters( 'https_local_ssl_verify', false )
)
), $doing_wp_cron );
var_dump('test2'); //exit here to test
wp_remote_post( $cron_request['url'], $cron_request['args'] );
On my local machine, the filter is applied, 'test2' is printed, and the script can continue to call wp_remote_post. On the server, however, the script terminates during the execution of apply_filter('cron_request'), printing 'test1' but not 'test2' and failing to reach wp_remost_post.
I have been unable to find the source for this filter to further debug. Wordpress.org says that the filter is defined within wp-includes/cron.php and that the one instance I have already cited is the only place it is ever applied, however, using Sublime multi-file search reveals that the string 'cron_request' appears only in the filter application I have cited.
I have read many post on wp_cron failing and have found no help other than non-descript 'Server configuration blocking wp_cron' answers. Any additional information on what type of server config or what else could be occuring here?

This action will trigger when someone visits your WordPress site, if the scheduled time has passed.
Maybe you can use
wp_schedule_event()
Instead of
wp_schedule_single_event()

Related

WordPress - Verifying nonce with navigator.sendBeacon

I am working on a very simple tracking plugin. I am using the visibilitychange event to send tracking data to a PHP script included in the plugin using navigator.sendBeacon.
However, unlike a simple WordPress AJAX request, the PHP file doesn't have access to any of the WordPress core files. Therefore the check_ajax_referer function throws an error.
I can't figure out a way of ensuring the PHP file has access to the WordPress core files, or even any of the files I have included in the plugin.
Is it possible to set this up like is normally done with WP and AJAX, or do I need to include all of the WP core files in the PHP script? And, if the second option is the only option, which files do I need to include?
Example code:
Enqueuing the JS script and localizing the PHP script path and nonce:
wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'dist/scripts/main.js', array( 'jquery' ), $this->version, true );
wp_localize_script( $this->plugin_name, 'OBJECT_NAME', array('PHP_SCRIPT_PATH' => PATH, 'ajax_nonce' => wp_create_nonce('TRACKING_NONCE')));
Send the data
document.addEventListener("visibilitychange", function () {
if (document.visibilityState === 'hidden') {
var data = new FormData();
data.append('tracking_data', JSON.stringify(form_data));
navigator.sendBeacon(OBJECT_NAME.PHP_SCRIPT_PATH, data);
}
});
Receive and process the data:
if (!empty($_POST)) {
$security_check = check_ajax_referer( 'TRACKING_NONCE', 'security' ); //Error thrown: Uncaught Error: Call to undefined function check_ajax_referer()
//Process data....
}

How to create execute function a one time using wp-cron

I create a plugin for send woocommerce data external system plugin i want create a cron run a one time like only run after plugin active success
this is i'm used cron code
add_action('wpse71941_cron', 'wpse71941_long_running');
function wpse71941_long_running($args)
{
// might need to call `set_time_limit` here
set_time_limit(0);
include 'customers.php';
include 'orders.php';
include 'products.php';
// return normal time limit
if($l = ini_get('max_execution_time'))
set_time_limit($l);
}
// schedule the event for right now
wp_schedule_single_event(
time(),
'wpse71941_cron',
array('args' => 'for', 'callback' => 'function')
);
any suggestions to send data in background process using corn or any?

How to WordPess CRON auto run every 1 minutes in localhost

When i load page cron running good but we want auto run every 60 seconds in localhost.
Any one help me.
add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );
function isa_add_every_three_minutes( $schedules ) {
$schedules['every_three_minutes'] = array(
'interval' => 60,
'display' => __( 'Every 1 Minutes', 'textdomain' )
);
return $schedules;
}
// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'isa_add_every_three_minutes' ) ) {
wp_schedule_event( time(), 'every_three_minutes', 'isa_add_every_three_minutes' );
}
// Hook into that action that'll fire every three minutes
add_action( 'isa_add_every_three_minutes', 'every_three_minutes_event_func' );
function every_three_minutes_event_func() {
$content = "some text here";
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/wordpressrootfolder/".time()."-myText.txt","wb");
fwrite($fp,$content);
fclose($fp);
}
Wordpress CronJobs work a little different then traditional CronJobs. They will only run when WordPress is loaded, if the interval time has passed. As such, on localhost, you would have to find a way for the page to be loaded regularly.
This is easy if you have access to the server's crontab via SSH or cPanel.
You are able to set wp-cron.php to run in a normal cron task on your server and set this to run at an interval of your choosing. I personally don't let wp-cron.php run on many of my sites as once your cron tasks get bulky it can affect page load speed.
The cron task you want to run is wget -q -O - https://example.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1 where you want to replace example.com with your own domain.
For further details, I find this article quite useful https://kinsta.com/knowledgebase/disable-wp-cron/

WooCommerce Subscriptions 2.0 - wcs_renewal_order_created action/filter throws error

I am trying to execute a function when WooCommerce subscriptions creates a new subscription renewal order.
The reason being I wish to trap this event and pass details to a different database so we can trigger business workflows with our payment gateway, within our backend systems.
I've tried add_action and add_filter using wcs_renewal_order_created and then tested this by manually creating a renewal payment from the parent subscription order.
The hook calls the function, executes the code, puts some data into some test tables and then Wordpress shows an error as shown below.
Is there a simpler way to trigger a function when a renewal subscription order is created and if not, can anyone help as to why it seems to be failing calling a class that seems to set the payment method.
function log_renewal_order_interface($order, $subscription) {
write_log('woocommerce_subscriptions_renewal_order_created function called');
global $conn;
global $wpdb, $table_prefix;
write_log('woocommerce_subscriptions_renewal_order_created setting up test variables');
write_log($order);
$test1 = 'logged renewal order';
$test2 = 'order_id'.$order_id;
$test3 = 'not set';
$test4 = 'not set';
$test5 = 'not set';
// test table entry
$conn->insert('test_tabel',
array('test1' => $test1, 'test2' => $test2, 'test3' => $test3, 'test4' => $test4, 'test5' => $test5),
array('%s', '%s', '%s', '%s', '%s'));
}
add_filter ('wcs_renewal_order_created', 'log_renewal_order_interface', 10,2);
I then get the following error:
Fatal error: Uncaught Error: Call to a member function
set_payment_method() on null in
/home/ourislan/lifestyle5d.ourislandsvoice.com/wp-content/plugins/woocommerce-subscriptions/includes/admin/class-wcs-admin-meta-boxes.php:176
Stack trace: #0
/home/ourislan/lifestyle5d.ourislandsvoice.com/wp-includes/class-wp-hook.php(286):
WCS_Admin_Meta_Boxes::create_pending_renewal_action_request(Object(WC_Subscription)) #1 /home/ourislan/lifestyle5d.ourislandsvoice.com/wp-includes/class-wp-hook.php(310):
WP_Hook->apply_filters('', Array) #2
/home/ourislan/lifestyle5d.ourislandsvoice.com/wp-includes/plugin.php(453):
WP_Hook->do_action(Array) #3
/home/ourislan/lifestyle5d.ourislandsvoice.com/wp-content/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-actions.php(131):
do_action('woocommerce_ord...', Object(WC_Subscription)) #4
/home/ourislan/lifestyle5d.ourislandsvoice.com/wp-includes/class-wp-hook.php(286):
WC_Meta_Box_Order_Actions::save(812, Object(WP_Post)) #5
/home/ourislan/lifestyle5d.ourislandsvoice.com/wp- in
/home/ourislan/lifestyle5d.ourislandsvoice.com/wp-content/plugins/woocommerce-subscriptions/includes/admin/class-wcs-admin-meta-boxes.php
on line 176
Figured it out. I needed to explicitly return $object at the end of the function.

Need to pull fields from an issue in Sitecore

I'm working with SiteCore and I need to pull some data out of the software via either that API or the SQL database using a PHP script. The reason I say both are possible is because even if the database changes later on, that doesn't matter to me.
Anyway...
I'm trying to pull any data fields that I can get from a particular issue. This is my SOAP code so far, and it connects to the service and such, but the return isn't what I need...
try
{
$client = new SoapClient('http://localhost:8083/sitecore/shell/webservice/service.asmx?WSDL');
$credentials = array('Password' => 'mypassword','Username' => 'sitecore\myusername');
$Current_Issue = array(
'id' => '{043B69BA-3175-4184-812F-C925CE80324E}',
//'language' => 'en',
//'version' => '1',
//'allFields' => 'true',
'databaseName' => 'web',
'credentials' => $credentials
);
$response = $client->GetItemMasters($Current_Issue);
print_r($response);
}
catch(SoapFault $e)
{
echo $e->getMessage();
}
catch(Exception $e)
{
echo $e->getMessage();
}
This is my output:
stdClass Object
(
[GetItemMastersResult] => stdClass Object
(
[any] => <sitecore xmlns=""/>
)
)
ANY help is appreciated. If anybody knows an example SQL query that I can use, that would be just as useful as an alternative method.
Thanks
If you are running Sitecore 6.5 / 6.6 you may want to take a look at the Sitecore Item Web API which was released yesterday (5/11/12).
http://sdn.sitecore.net/Products/Sitecore%20Item%20Web%20API.aspx
This allows you to perform RESTful operations against Sitecore items without the need for the old web service / SOAP interface. Using this module you can receive a JSON representation of a Sitecore item or collection of items and even post back changes. You may find it easier to work with :)
If you have to use the SOAP interface, are you sure that your items are published ? Try changing the databaseName -> 'master' and see if you get any results. Other things to check are the permissions of the user credentials you are using.

Resources