| 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 |
|