Plugin Directory

source: advanced-cron-manager/trunk/inc/FormProvider.php

Last change on this file was 3356826, checked in by bracketspace, 3 months ago

Update to version 2.6.4 from GitHub

File size: 3.5 KB
Line 
1<?php
2/**
3 * FormProvider class
4 * Provides forms for all plugin actions
5 *
6 * @package advanced-cron-manager
7 */
8
9namespace underDEV\AdvancedCronManager;
10
11use underDEV\Utils;
12use underDEV\AdvancedCronManager\Cron;
13
14/**
15 * FormProvider class
16 */
17class FormProvider {
18
19        /**
20         * View class
21         *
22         * @var instance of underDEV\AdvancedCronManager\Utils\View
23         */
24        public $view;
25
26        /**
27         * Ajax class
28         *
29         * @var instance of underDEV\AdvancedCronManager\Utils\Ajax
30         */
31        public $ajax;
32
33        /**
34         * SchedulesLibrary class
35         *
36         * @var instance of underDEV\AdvancedCronManager\Cron\SchedulesLibrary
37         */
38        public $schedules_library;
39
40        /**
41         * Schedules class
42         *
43         * @var instance of underDEV\AdvancedCronManager\Cron\Schedules
44         */
45        public $schedules;
46
47        /**
48         * Constructor
49         *
50         * @param Utils\View            $view              View class.
51         * @param Utils\Ajax            $ajax              Ajax class.
52         * @param Cron\SchedulesLibrary $schedules_library SchedulesLibrary class.
53         * @param Cron\Schedules        $schedules         Schedules class.
54         */
55        public function __construct( Utils\View $view, Utils\Ajax $ajax, Cron\SchedulesLibrary $schedules_library, Cron\Schedules $schedules ) {
56                $this->view              = $view;
57                $this->ajax              = $ajax;
58                $this->schedules_library = $schedules_library;
59                $this->schedules         = $schedules;
60        }
61
62        /**
63         * Gets specified form
64         *
65         * @param  string $form_name  form slug which will be passed to View.
66         * @param  string $form_title heading for form.
67         * @param  string $cta        send button label.
68         * @return void               prints and dies form html
69         */
70        public function get_form( $form_name = null, $form_title = '', $cta = '' ) {
71
72                if ( null === $form_name ) {
73                        trigger_error( 'Form name cannot be empty' );
74                }
75
76                ob_start();
77
78                $this->view->set_var( 'heading', $form_title );
79                $this->view->set_var( 'cta', $cta );
80                $this->view->set_var( 'form_class', str_replace( '/', '-', $form_name ) );
81
82                $this->view->get_view( 'forms/' . $form_name );
83
84                $form_html = ob_get_clean();
85
86                $this->ajax->response( $form_html );
87        }
88
89        /**
90         * Add schedule form
91         */
92        public function add_schedule() {
93
94                $this->ajax->verify_nonce( 'acm/schedule/add' );
95
96                $this->get_form( 'schedule/add', __( 'New schedule', 'advanced-cron-manager' ), __( 'Add schedule', 'advanced-cron-manager' ) );
97        }
98
99        /**
100         * Edit schedule form
101         */
102        public function edit_schedule() {
103
104                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Need schedule slug for nonce string.
105                $schedule_slug = sanitize_key( isset( $_REQUEST['schedule'] ) ? $_REQUEST['schedule'] : '' );
106
107                if ( empty( $schedule_slug ) ) {
108                        $this->ajax->response( false, array(
109                                __( 'Invalid schedule slug.', 'advanced-cron-manager' ),
110                        ) );
111                }
112
113                $this->ajax->verify_nonce( 'acm/schedule/edit/' . $schedule_slug );
114
115                $schedule = $this->schedules_library->get_schedule( $schedule_slug );
116
117                $this->view->set_var( 'schedule', $schedule );
118
119                // Translators: schedule slug.
120                $this->get_form( 'schedule/edit', sprintf( __( 'Edit "%s" schedule', 'advanced-cron-manager' ), $schedule->slug ), __( 'Edit schedule', 'advanced-cron-manager' ) );
121        }
122
123        /**
124         * Add event form
125         */
126        public function add_event() {
127
128                $this->ajax->verify_nonce( 'acm/event/add' );
129
130                $this->view->set_var( 'schedules', $this->schedules->get_schedules() );
131                $this->view->set_var( 'single_schedule', $this->schedules->get_single_event_schedule() );
132
133                $this->get_form( 'event/add', __( 'New event', 'advanced-cron-manager' ), __( 'Schedule event', 'advanced-cron-manager' ) );
134        }
135}
Note: See TracBrowser for help on using the repository browser.