PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / WordPress / SchedulingService.php

SchedulingService.php in Depicter — Popup & Slider Builder trunk, at app/src/WordPress/SchedulingService.php

175 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Depicter\WordPress;
4
5 use Depicter\GuzzleHttp\Exception\GuzzleException;
6
7 class SchedulingService
8 {
9
10 public function hooks() {
11 add_action( 'depicter/document/schedule/publish', [ $this, 'publishSlider' ], 10, 1 );
12 add_action( 'depicter/document/schedule/draft', [ $this, 'draftSlider' ], 10, 1 );
13 add_action( 'depicter/document/schedule/clear/cache', [ $this, 'clearCache' ], 10, 1 );
14 }
15
16 /**
17 * Schedule Publish Event for slider
18 *
19 * @param $documentID
20 * @param $date
21 *
22 * @return void
23 */
24 public function schedulePublishEvent( $documentID, $date ) {
25 $this->clearScheduledPublishEvent( $documentID );
26 if ( ! wp_next_scheduled( 'depicter/document/schedule/publish', [ $documentID ] ) ) {
27 wp_schedule_single_event( strtotime( $date ), 'depicter/document/schedule/publish', [ $documentID ] );
28 }
29 }
30
31 /**
32 * Clear all scheduled publish event for specific slider
33 *
34 * @param $documentID
35 *
36 * @return void
37 */
38 public function clearScheduledPublishEvent( $documentID ) {
39 wp_clear_scheduled_hook( 'depicter/document/schedule/publish', [ $documentID ] );
40 }
41
42 /**
43 * Schedule Draft Event for slider
44 *
45 * @param $documentID
46 * @param $date
47 *
48 * @return void
49 */
50 public function scheduleDraftEvent( $documentID, $date ) {
51 $this->clearScheduledDraftEvent( $documentID );
52 if ( ! wp_next_scheduled( 'depicter/document/schedule/draft', [ $documentID ] ) ) {
53 wp_schedule_single_event( strtotime( $date ), 'depicter/document/schedule/draft', [ $documentID ] );
54 }
55 }
56
57 /**
58 * Clear all scheduled draft event for specific slider
59 *
60 * @param $documentID
61 *
62 * @return void
63 */
64 public function clearScheduledDraftEvent( $documentID ) {
65 wp_clear_scheduled_hook( 'depicter/document/schedule/draft', [ $documentID ] );
66 }
67
68 /**
69 * Schedule Publish Event for slider
70 *
71 * @param $documentID
72 * @param $date
73 * @param string $timestampType
74 *
75 * @return void
76 */
77 public function scheduleClearCacheEvent( $documentID, $date, $timestampType = 'UTC' ) {
78 if ( ! wp_next_scheduled( 'depicter/document/schedule/clear/cache', [ $documentID, $date ] ) ) {
79 wp_schedule_single_event( strtotime( $date . ( $timestampType ? ' ' . $timestampType : '' ) ), 'depicter/document/schedule/clear/cache', [ $documentID ] );
80 }
81 }
82
83 /**
84 * Clear all scheduled clear cache event for specific slider
85 *
86 * @param $documentID
87 *
88 * @return void
89 */
90 public function clearScheduledClearCacheEvent( $documentID ) {
91 wp_clear_scheduled_hook( 'depicter/document/schedule/clear/cache', [ $documentID ] );
92 }
93
94
95 /**
96 * Set publish status for slider
97 *
98 * @param $documentID
99 *
100 * @return void
101 */
102 public function publishSlider( $documentID ) {
103 try {
104 if ( \Depicter::documentRepository()->isPublished( $documentID ) ) {
105 return;
106 }
107
108 if ( \Depicter::auth()->isPaid() && ! \Depicter::auth()->verifyActivation() ) {
109 error_log( esc_html__( 'License is not valid.', 'depicter' ), 0 );
110 return;
111 }
112
113 if( function_exists( 'get_filesystem_method' ) && get_filesystem_method() != 'direct' ){
114 error_log( esc_html__( 'Media files cannot be published due to lack of proper file permissions for uploads directory.', 'depicter' ), 0 );
115 return;
116 }
117
118 $editorRawData = \Depicter::document()->getEditorRawData( $documentID );
119
120 // Download media if document published
121 \Depicter::media()->importDocumentAssets( $editorRawData );
122
123 \Depicter::documentRepository()->saveEditorData( $documentID, ['status' => 'publish'] );
124 $this->clearCache( $documentID );
125 $documentModel = \Depicter::document()->getModel( $documentID )->prepare();
126 $documentModel->render();
127 $documentModel->saveCss();
128 } catch( \Exception $exception ) {
129 error_log( $exception->getMessage(), 0);
130 } catch( GuzzleException $exception ) {
131 error_log( $exception->getMessage(), 0);
132 }
133 }
134
135 /**
136 * Set draft status for slider
137 *
138 * @param $documentID
139 *
140 * @return void
141 */
142 public function draftSlider( $documentID ) {
143 try {
144 if ( !\Depicter::documentRepository()->isPublished( $documentID ) ) {
145 return;
146 }
147
148 $this->clearCache( $documentID );
149 \Depicter::documentRepository()->saveEditorData( $documentID, ['status' => 'unpublished'] );
150 } catch( \Exception $exception ) {
151 error_log( $exception->getMessage(), 0);
152 }
153 }
154
155 public function clearCache( $documentID ) {
156 \Depicter::front()->render()->flushDocumentCache( $documentID );
157 \Depicter::document()->cacheCustomStyles( $documentID );
158 }
159
160 /**
161 * Compares the current time with a specified time in UTC timezone
162 *
163 * @param string $specifiedTime Specified timestamp
164 * @param string $type Timestamp type
165 *
166 * @return bool
167 */
168 public function isDatePassed( $specifiedTime, $type = 'UTC' ): bool{
169 // Get timestamp based on specified type
170 $specifiedTime = strtotime( $specifiedTime . ( $type ? ' ' . $type : '' ) );
171
172 return time() > $specifiedTime;
173 }
174 }
175