ajax = $ajax; $this->events = $events; $this->library = $library; $this->schedules = $schedules; } /** * Insert event * * @return void */ public function insert() { $this->ajax->verify_nonce( 'acm/event/insert' ); if ( ! current_user_can( 'manage_options' ) ) { $this->ajax->response( false, array( __( "You're not allowed to do that.", 'advanced-cron-manager' ), ) ); } // phpcs:ignore $data = wp_parse_args( $_REQUEST['data'], array() ); $execution = strtotime( $data['execution'] ) ? strtotime( $data['execution'] ) + ( HOUR_IN_SECONDS * $data['execution_offset'] ) : time() + ( HOUR_IN_SECONDS * $data['execution_offset'] ); $args = array(); if ( ! empty( $data['arguments'] ) ) { foreach ( $data['arguments'] as $arg_raw ) { if ( ! empty( $arg_raw ) ) { $args[] = wp_filter_nohtml_kses( sanitize_text_field( html_entity_decode( $arg_raw, ENT_QUOTES, 'UTF-8' ) ) ); } } } $hook = trim( wp_strip_all_tags( $data['hook'] ) ); $result = $this->library->insert( $hook, $execution, $data['schedule'], $args ); if ( is_array( $result ) ) { $errors = $result; } else { $errors = array(); } $schedule = $this->schedules->get_schedule( $data['schedule'] ); $arg_num = count( $args ); $success = sprintf( // Translators: event hook name, # of args, schedule name. esc_html( _n( 'Event "%1$s" with %2$d argument has been scheduled (%3$s)', 'Event "%1$s" with %2$d arguments has been scheduled (%3$s)', $arg_num, 'advanced-cron-manager' ) ), $hook, $arg_num, $schedule->label ); $this->ajax->response( $success, $errors ); } /** * Run event * * @return void */ public function run() { global $acm_current_event; // phpcs:ignore $event = $this->events->get_event_by_hash( $_REQUEST['event'] ); if ( ! $event ) { $this->ajax->response( false, array( __( 'This event doesn\'t seem to exist anymore', 'advanced-cron-manager' ), ) ); } $this->ajax->verify_nonce( 'acm/event/run/' . $event->hash ); if ( ! current_user_can( 'manage_options' ) ) { $this->ajax->response( false, array( __( "You're not allowed to do that.", 'advanced-cron-manager' ), ) ); } $acm_current_event = $event; if ( ! defined( 'DOING_CRON' ) ) { define( 'DOING_CRON', true ); } do_action_ref_array( $event->hook, $event->args ); // Translators: event hook. $success = sprintf( __( 'Event "%s" has been executed', 'advanced-cron-manager' ), $event->hook ); $this->ajax->response( $success, array() ); } /** * Remove event * * @return void */ public function remove() { // phpcs:ignore $event = $this->events->get_event_by_hash( $_REQUEST['event'] ); if ( false === $event ) { $this->ajax->response( false, array( __( "This event doesn't exist anymore.", 'advanced-cron-manager' ), ) ); } $errors = array(); $this->ajax->verify_nonce( 'acm/event/remove/' . $event->hash ); if ( ! current_user_can( 'manage_options' ) ) { $this->ajax->response( false, array( __( "You're not allowed to do that.", 'advanced-cron-manager' ), ) ); } if ( $event->protected ) { // Translators: event hook. $errors = array( sprintf( __( 'Event "%s" is protected and you cannot remove it', 'advanced-cron-manager' ), $event->hook ) ); } $this->library->unschedule( $event ); $this->library->remove_from_paused( $event ); // Translators: event hook. $success = sprintf( __( 'Event "%s" has been removed', 'advanced-cron-manager' ), $event->hook ); $this->ajax->response( $success, $errors ); } /** * Pause event * * @return void */ public function pause() { // phpcs:ignore $event = $this->events->get_event_by_hash( $_REQUEST['event'] ); $this->ajax->verify_nonce( 'acm/event/pause/' . $event->hash ); if ( ! current_user_can( 'manage_options' ) ) { $this->ajax->response( false, array( __( "You're not allowed to do that.", 'advanced-cron-manager' ), ) ); } $result = $this->library->pause( $event ); if ( is_array( $result ) ) { $errors = $result; } else { $errors = array(); } // Translators: event hook. $success = sprintf( __( 'Event "%s" has been paused', 'advanced-cron-manager' ), $event->hook ); $this->ajax->response( $success, $errors ); } /** * Unpause event * * @return void */ public function unpause() { // phpcs:ignore $event = $this->events->get_event_by_hash( $_REQUEST['event'] ); $this->ajax->verify_nonce( 'acm/event/unpause/' . $event->hash ); if ( ! current_user_can( 'manage_options' ) ) { $this->ajax->response( false, array( __( "You're not allowed to do that.", 'advanced-cron-manager' ), ) ); } $result = $this->library->unpause( $event ); if ( is_array( $result ) ) { $errors = $result; } else { $errors = array(); } // Translators: event hook. $success = sprintf( __( 'Event "%s" has been unpaused', 'advanced-cron-manager' ), $event->hook ); $this->ajax->response( $success, $errors ); } }