PluginProbe
Depicter — Popup & Slider Builder / 1.9.2
Depicter — Popup & Slider Builder v1.9.2
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
← All changes | app/src/Database/Repository/DocumentRepository.php +46 -271 trunk1.9.2 View file →
@@ -4,9 +4,8 @@
4 4 use Averta\Core\Utility\Arr;
5 5 use Averta\Core\Utility\JSON;
6 6 use Depicter;
7 7 use Depicter\Database\Entity\Document;
8 -use Depicter\Document\Helper\Helper;
9 8 use Depicter\Exception\DocumentNotFoundException;
10 9 use Exception;
11 10 use TypeRocket\Database\Results;
12 11 use TypeRocket\Models\Model;
@@ -12,18 +11,9 @@
12 11 use TypeRocket\Models\Model;
13 12
14 13 class DocumentRepository
15 14 {
16 -
17 15 /**
18 - * Columns that the document list may be sorted by.
19 - */
20 - private const ALLOWED_ORDER_BY = [
21 - 'id', 'name', 'slug', 'type', 'author',
22 - 'sections_count', 'created_at', 'modified_at', 'status', 'parent'
23 - ];
24 -
25 - /**
26 16 * @var Document
27 17 */
28 18 private $document;
29 19
@@ -28,9 +18,9 @@
28 18 private $document;
29 19
30 20
31 21 public function __construct(){
32 - $this->document = Document::new();
22 + $this->document = New Document();
33 23 }
34 24
35 25 /**
36 26 * @return Document
@@ -38,9 +28,9 @@
38 28 * @throws Exception
39 29 */
40 30 public function document()
41 31 {
42 - return Document::new();
32 + return new Document();
43 33 }
44 34
45 35
46 36 /**
@@ -53,13 +43,8 @@
53 43 * @throws Exception
54 44 */
55 45 public function saveEditorData( int $id = 0, array $properties = [] )
56 46 {
57 -
58 - if ( $id && $document = $this->document()->findById( $id ) ) {
59 - $this->document = $document;
60 - }
61 -
62 47 if( isset( $properties['editor'] ) ){
63 48 $editor = $properties['editor'];
64 49 unset( $properties['editor'] );
65 50
@@ -89,13 +74,8 @@
89 74 throw new Exception('Name cannot be empty.');
90 75 }
91 76 }
92 77
93 - if ( $properties['status'] == 'unpublished' ) {
94 - $properties['status'] = 'draft';
95 - $this->changeRevisionsStatus( $id, 'draft');
96 - }
97 -
98 78 $result = $this->update( $id, $properties, true );
99 79
100 80 if( $properties['status'] == 'publish' ){
101 81 $this->addRevision( $id, $properties );
@@ -102,10 +82,10 @@
102 82 }
103 83
104 84 return [
105 85 'result' => $result,
106 - 'modifiedAt' => $result ? $this->document->getApiProperties()['modified_at'] : "",
107 - 'publishedAt' => $result ? $this->document->getLastPublishedAt() : ""
86 + 'modifiedAt' => $this->document->getApiProperties()['modified_at'],
87 + 'publishedAt' => $this->document->getLastPublishedAt()
108 88 ];
109 89 }
110 90
111 91 /**
@@ -132,38 +112,16 @@
132 112 {
133 113 $fields['created_at'] = $this->document->getDateTime();
134 114 $fields['parent'] = $id;
135 115
136 - // set type if was not available in $fields
137 - if( empty( $fields['type'] ) ){
138 - $fields['type'] = $this->getFieldValue( $id, 'type' );
139 - }
140 -
141 116 // check if revisions limit exceed than 15 number then remove the oldest one
142 117 if( DEPICTER_REVISIONS ){
143 118 $this->checkRevisionsLimit( $id );
144 119 }
145 120
146 - return $this->findOrCreate( 0, $fields);
147 - }
121 + $documentId = $this->findOrCreate(0, $fields);
148 122
149 - /**
150 - * change status of revisions
151 - *
152 - * @param int $id
153 - * @param string $status
154 - *
155 - * @return void
156 - * @throws Exception
157 - */
158 - public function changeRevisionsStatus( int $id, string $status ) {
159 - $revisions = $this->document()->where( 'parent', $id)->findAll();
160 - if ( !empty( $revisions ) ) {
161 - $revisions = $revisions->get();
162 - foreach( $revisions as $revision ) {
163 - $revision->update(['status' => $status ]);
164 - }
165 - }
123 + return $documentId;
166 124 }
167 125
168 126 /**
169 127 * Reverts to a previously published checkpoint
@@ -200,17 +158,15 @@
200 158 if( $parentDocument->getID() != $lastRevision->parent() ){
201 159 throw new Exception('Revision ID does not belong to this document.');
202 160 }
203 161 // Set revision editor data for document
204 - $lastRevisionContent = \Depicter::document()->migrations()->apply( $lastRevision->content() );
205 - $parentDocument->save( ['content' => $lastRevisionContent ] );
162 + $parentDocument->save( ['content' => $lastRevision->content() ] );
206 163
207 164 } else {
208 165 throw new Exception("Couldn't revert to the revision. No revision found for this checkpoint.");
209 166 }
210 167
211 - $hits = Arr::camelizeKeys( $lastRevision->getApiProperties(), '_' );
212 - return [ "hits" => $hits ];
168 + return $lastRevision;
213 169 }
214 170
215 171 /**
216 172 * Check for revisions limit number
@@ -248,70 +204,12 @@
248 204 * @throws Exception
249 205 */
250 206 public function getList( array $fields = [], $args = [] )
251 207 {
252 - $columnsName = !empty( $fields ) ? $fields : ['id', 'name', 'slug', 'type', 'author', 'sections_count', 'created_at', 'modified_at', 'thumbnail', 'status'];
208 + $columnsName = !empty( $fields ) ? $fields : ['id', 'name', 'slug', 'author', 'sections_count', 'created_at', 'modified_at', 'thumbnail', 'status'];
253 209 $numberOfPages = '';
254 -
255 - $documents = $this->select( $columnsName );
256 -
257 - // if 'has' filter is set. valid values for 'has': form, shortcode
258 - if ( is_array( $args['has'] ) ) {
259 - if( in_array( 'form', $args['has'] ) ){
260 - $documents = $documents->withMeta()->whereMeta('hasForm', "1");
261 - } elseif( in_array( 'shortcode', $args['has'] ) ){
262 - $documents = $documents->withMeta()->whereMeta('hasShortcode', "1");
263 - }
264 - }
265 -
266 - if ( !empty( $args['orderBy'] ) && !empty( $args['order'] ) ) {
267 - // Resolve ordering against a known column list, never raw request input.
268 - $order = 'ASC' === strtoupper( (string) $args['order'] ) ? 'ASC' : 'DESC';
269 - $orderBy = in_array( $args['orderBy'], self::ALLOWED_ORDER_BY, true ) ? $args['orderBy'] : 'modified_at';
270 -
271 - $documents = $documents->orderBy( $orderBy, $order );
272 - }
273 -
274 - if ( !empty( $args['s'] ) ) {
275 - $documents = $documents->where( 'name', 'like', '%' . $args['s'] . '%' );
276 - }
277 -
278 - if ( !empty( $args['status'] ) ) {
279 - $documents = $documents->where( 'status', $args['status'] );
280 - }
281 -
282 - if ( !empty( $args['types'] ) ) {
283 - $types = explode( ',', $args['types'] );
284 - $where = [];
285 - foreach( $types as $type ) {
286 - $where[] = [
287 - 'column' => 'type',
288 - 'operator' => '=',
289 - 'value' => $type
290 - ];
291 -
292 - $where[] = 'OR';
293 -
294 - if ( $type == 'slider' ) {
295 - $where[] = [
296 - 'column' => 'type',
297 - 'operator' => '=',
298 - 'value' => 'custom'
299 - ];
300 -
301 - $where[] = 'OR';
302 - }
303 - }
304 -
305 - array_pop( $where );
306 -
307 - $documents = $documents->where( $where );
308 - }
309 -
310 - $total = $documents->count();
311 -
312 210 if ( !empty( $args['page'] ) && !empty( $args['perPage'] ) ) {
313 - $pager = $documents->paginate( $args['perPage'], $args['page'] );
211 + $pager = $this->select( $fields )->paginate( $args['perPage'], $args['page'] );
314 212 if ( $pager ) {
315 213 $numberOfPages = $pager->getNumberOfPages();
316 214 $documents = $pager->getResults();
317 215 } else {
@@ -318,9 +216,9 @@
318 216 $documents = [];
319 217 }
320 218
321 219 } else {
322 - $documents = $documents->get();
220 + $documents = $this->select( $fields )->findAll()->get();
323 221 }
324 222
325 223
326 224 $documents = $documents ? $documents->toArray() : [];
@@ -327,16 +225,8 @@
327 225
328 226 if ( $documents && empty( $fields ) ) {
329 227 $uploadDir = wp_upload_dir();
330 228 foreach ( $documents as $key => $document ) {
331 - $documents[ $key ]['has'] = [];
332 - if ( Depicter::metaRepository()->get( $document['id'], 'hasForm', false ) ) {
333 - $documents[ $key ]['has'][] = 'form';
334 - }
335 - if ( Depicter::metaRepository()->get( $document['id'], 'hasShortcode', false ) ) {
336 - $documents[ $key ]['has'][] = 'shortcode';
337 - }
338 -
339 229 $documents[ $key ]['publishedAt'] = $this->getLastPublishedAt( $document );
340 230
341 231 if ( is_file( $uploadDir['basedir'] . '/depicter/preview-images/' . $document['id'] . '.png' ) ) {
342 232 $documents[ $key ]['previewImage'] = $uploadDir['baseurl'] . '/depicter/preview-images/' . $document['id'] . '.png';
@@ -349,9 +239,8 @@
349 239 if ( !empty( $numberOfPages ) ) {
350 240 return [
351 241 'page' => $args['page'],
352 242 'perPage' => $args['perPage'],
353 - 'total' => $total,
354 243 'numberOfPages' => $numberOfPages,
355 244 'documents' => $documents
356 245 ];
357 246 }
@@ -369,9 +258,10 @@
369 258 */
370 259 public function select( array $fields = [] )
371 260 {
372 261 $columnsName = !empty( $fields ) ? $fields : ['id', 'name', 'slug', 'author', 'sections_count', 'created_at', 'modified_at', 'thumbnail', 'status'];
373 - return $this->document()->reselect( $columnsName )->parents();
262 + return $this->document()->reselect( $columnsName )
263 + ->where('parent', '0');
374 264 }
375 265
376 266 /**
377 267 * Save changes directly to current document
@@ -397,9 +287,9 @@
397 287 * @throws Exception
398 288 */
399 289 public function update( int $id = 0, array $fields = [], bool $parentOnly = false )
400 290 {
401 - if( $document = $this->document()->findById( $id ) ){
291 + if( $document = $this->document->findById( $id ) ){
402 292 if( $parentOnly && $document->getFieldValue('parent') != 0 ){
403 293 throw new Exception('Updating revision is not allowed.');
404 294 }
405 295
@@ -416,18 +306,15 @@
416 306 *
417 307 * @return Document
418 308 * @throws Exception
419 309 */
420 - public function create( $type = 'custom' )
310 + public function create()
421 311 {
422 - $documentId = $this->findOrCreate(0,
423 - $this->getMergedFields([
424 - 'slug' => $this->makeSlug(),
425 - 'type' => $type,
426 - 'created_at' => $this->document->getDateTime(),
427 - 'modified_at' => $this->document->getDateTime()
428 - ])
429 - );
312 + $documentId = $this->findOrCreate(0, [
313 + 'created_at' => $this->document->getDateTime(),
314 + 'modified_at' => $this->document->getDateTime(),
315 + 'slug' => $this->makeSlug()
316 + ]);
430 317
431 318 $document = $this->document()->findById( $documentId );
432 319 $document->rename( $document->getFieldValue( 'name' ) . ' ' . $documentId );
433 320
@@ -439,13 +326,13 @@
439 326 *
440 327 * @param $id
441 328 * @param $name
442 329 *
443 - * @return bool
330 + * @return int
444 331 */
445 332 public function rename( $id, $name )
446 333 {
447 - if( $document = $this->document()->findById( $id ) ){
334 + if( $document = $this->document->findById( $id ) ){
448 335 return $document->rename( $name );
449 336 }
450 337 return false;
451 338 }
@@ -463,9 +350,9 @@
463 350 {
464 351 if( $this->checkSlug( $slug ) ){
465 352 throw new Exception("The slug is already in use.");
466 353 }
467 - if( $document = $this->document()->findById( $id ) ){
354 + if( $document = $this->document->findById( $id ) ){
468 355 return $document->changeSlug( $slug );
469 356 }
470 357 return false;
471 358 }
@@ -481,9 +368,9 @@
481 368 * @throws Exception
482 369 */
483 370 public function duplicate( int $id, bool $returnNew = false )
484 371 {
485 - if( $document = $this->document()->findById( $id ) ){
372 + if( $document = $this->document->findById( $id ) ){
486 373 $fields = $document->getProperties();
487 374
488 375 unset( $fields['id'] );
489 376
@@ -509,17 +396,9 @@
509 396 if( is_null( $fields['thumbnail'] ) ){
510 397 unset( $fields['thumbnail'] );
511 398 }
512 399
513 - if ( !empty( $fields['content'] ) ) {
514 - $fields['content'] = \Depicter::document()->migrations()->apply( $fields['content'] );
515 - }
516 -
517 - $metaRelationID = ! empty( $fields['parent'] ) ? $fields['parent'] : $id;
518 - $fields['parent'] = 0;
519 -
520 400 $newId = $this->findOrCreate( 0, $fields );
521 - Depicter::metaRepository()->duplicateAllMetaByRelationID( $metaRelationID, $newId );
522 401
523 402 return $this->document()->findById( $newId );
524 403 }
525 404
@@ -536,13 +415,8 @@
536 415 */
537 416 public function delete( $id )
538 417 {
539 418 if( $document = $this->document()->findById( $id ) ){
540 - $revisions = $this->document()->select('id')->where('parent', $id )->get();
541 - if ( $revisions ) {
542 - $revisionIDs = wp_list_pluck( $revisions->toArray() , 'id' );
543 - $this->document()->delete( $revisionIDs );
544 - }
545 419 return $document->delete();
546 420 }
547 421 return false;
548 422 }
@@ -552,9 +426,8 @@
552 426 *
553 427 * @param integer $id
554 428 *
555 429 * @return mixed
556 - * @throws Exception
557 430 */
558 431 public function findById( int $id )
559 432 {
560 433 return $this->document()->findById( $id );
@@ -592,9 +465,9 @@
592 465 *
593 466 * @param string $slug
594 467 * @param int $id
595 468 *
596 - * @return string
469 + * @return int
597 470 * @throws Exception
598 471 */
599 472 public function makeSlug( string $slug = '', int $id = 0 )
600 473 {
@@ -623,9 +496,9 @@
623 496 * @param string $slug
624 497 *
625 498 * @param int $ignoreID The document ID to ignore on check
626 499 *
627 - * @return bool
500 + * @return int
628 501 * @throws Exception
629 502 */
630 503 public function checkSlug( string $slug, int $ignoreID = 0 )
631 504 {
@@ -630,10 +503,10 @@
630 503 public function checkSlug( string $slug, int $ignoreID = 0 )
631 504 {
632 505 $document = $this->document();
633 506 $foundCount = $document->where('slug', $slug )
634 - ->where('id', 'NOT LIKE', $ignoreID )
635 - ->findAll()->count();
507 + ->where('id', 'NOT LIKE', $ignoreID )
508 + ->findAll()->count();
636 509
637 510 if( $foundCount > 0 ){
638 511 unset( $document );
639 512 return true;
@@ -646,16 +519,15 @@
646 519 * Retrieves default fields
647 520 *
648 521 * @return array
649 522 */
650 - public function draftFields( $type = '')
523 + public function draftFields()
651 524 {
652 525 return [
653 - 'name' => !empty($type) ? Helper::getDocumentTypeLabel( $type ) : __('Slider', 'depicter' ),
526 + 'name' => __('Untitled Slider'),
654 527 'status' => 'draft',
655 528 'author' => $this->getCurrentUserId()
656 529 ];
657 -
658 530 }
659 531
660 532 /**
661 533 * Retrieves current logged in user ID
@@ -673,21 +545,19 @@
673 545 */
674 546 public function defaultFields()
675 547 {
676 548 return [
677 - 'name' => __('Untitled Slider', 'depicter'),
678 - 'slug' => '',
679 - 'type' => 'slider',
680 - 'author' => 0,
681 - 'parent' => 0,
682 - 'created_at' => $this->document->getDateTime(),
683 - 'sections_count'=> 0,
684 - 'thumbnail' => '',
685 - 'content' => '',
686 - 'password' => '',
687 - 'status' => 'draft'
549 + 'name' => __('Untitled Slider'),
550 + 'slug' => '',
551 + 'author' => 0,
552 + 'parent' => 0,
553 + 'created_at' => $this->document->getDateTime(),
554 + 'sections_count' => 0,
555 + 'thumbnail' => '',
556 + 'content' => '',
557 + 'password' => '',
558 + 'status' => 'draft'
688 559 ];
689 -
690 560 }
691 561
692 562 /**
693 563 * Merge fields with default fields
@@ -697,11 +567,9 @@
697 567 * @return array
698 568 */
699 569 public function getMergedFields( $fields )
700 570 {
701 - $type = $fields['type'] ?? '';
702 -
703 - return Arr::merge( $fields, $this->draftFields( $type ) );
571 + return Arr::merge( $fields, $this->draftFields() );
704 572 }
705 573
706 574 /**
707 575 * Get a revision of a document
@@ -717,21 +585,8 @@
717 585 return $this->document()->orderBy('id', 'DESC')->where( 'parent', $id )->take( 1, $offset )->get();
718 586 }
719 587
720 588 /**
721 - * Get list of revisions ID
722 - *
723 - * @param int $id Document ID
724 - *
725 - * @return array
726 - * @throws Exception
727 - */
728 - public function getRevisionsID( int $id ) {
729 - $revisions = $this->document()->select( ['id'])->orderBy( 'id', 'DESC')->where( 'parent', $id )->get();
730 - return $revisions ? wp_list_pluck( $revisions->toArray(), 'id' ) : [];
731 - }
732 -
733 - /**
734 589 * @param array $document Document object in array
735 590 *
736 591 * @return mixed|string|null
737 592 * @throws Exception
@@ -741,10 +596,10 @@
741 596 return $document['modified_at'];
742 597 }
743 598
744 599 $lastRevision = $this->getRecentRevision( $document['id'] );
600 + return $lastRevision ? $lastRevision->modified_at : null;
745 601
746 - return $lastRevision && $lastRevision->status === 'publish' ? $lastRevision->modified_at : null;
747 602 }
748 603
749 604 /**
750 605 * Get a document entity
@@ -784,9 +639,9 @@
784 639 *
785 640 * @return mixed
786 641 * @throws DocumentNotFoundException
787 642 */
788 - public function getContent( int $documentId, array $where = [] ){
643 + public function geContent( int $documentId, array $where = [] ){
789 644 if( ! $documentEntity = $this->findOne( $documentId, $where ) ){
790 645 if ( isset( $where['status'] ) && is_array( $where['status'] ) ) {
791 646 unset( $where['status'][0] );
792 647 if( ! $documentEntity = $this->findOne( $documentId, $where ) ){
@@ -827,13 +682,13 @@
827 682 try{
828 683 if( $this->getRecentRevision( $documentId ) ){
829 684 return true;
830 685 }
831 -
832 - return $this->isPublished( $documentId );
833 686 } catch ( \Exception $exception ) {
834 687 return false;
835 688 }
689 +
690 + return false;
836 691 }
837 692
838 693 /**
839 694 * Whether the document has published status or not
@@ -839,9 +694,9 @@
839 694 * Whether the document has published status or not
840 695 *
841 696 * @param int $documentId
842 697 *
843 - * @return bool
698 + * @return bool|Document
844 699 * @throws Exception
845 700 */
846 701 public function isPublished( $documentId ){
847 702 try{
@@ -851,28 +706,13 @@
851 706 }
852 707 }
853 708
854 709 /**
855 - * @param int $documentID Document ID
856 - * @param string $fieldName Table field to retrieve value from
857 - *
858 - * @return array|mixed|object|string|null
859 - */
860 - public function getFieldValue( $documentID, $fieldName = 'name' ){
861 -
862 - if( $document = $this->document()->findById( $documentID ) ){
863 - return $document->getFieldValue( $fieldName );
864 - }
865 - return null;
866 - }
867 -
868 - /**
869 710 * Get status of document
870 711 *
871 712 * @param int $documentId
872 713 *
873 714 * @return string
874 - * @throws Exception
875 715 */
876 716 public function getStatus( $documentId ) {
877 717 if( $document = $this->findById( $documentId ) ){
878 718 $document = $document->toArray();
@@ -931,70 +771,5 @@
931 771 */
932 772 protected function getPreviewRelativeImagePath( $documentID ){
933 773 return '/depicter/preview-images/' . $documentID . '.png';
934 774 }
935 -
936 - /**
937 - * Retrieves IDs of all conditional documents
938 - *
939 - * @return array|object
940 - */
941 - public function getConditionalDocumentIDs(){
942 -
943 - try{
944 - // get all main documents with conditional friendly types
945 - $parents = $this->document()
946 - ->reselect(['id'])
947 - ->appendRawWhere( 'and', " `type` in ('popup', 'banner-bar')" )
948 - ->where('status', 'publish')
949 - ->where('parent', '0')
950 - ->get();
951 - $parents = $parents ? $parents->toArray() : [];
952 -
953 - $parents = array_map( function( $record ){
954 - return $record['id'];
955 - }, $parents );
956 -
957 - // get published revisions with conditional friendly types
958 - $revisions = $this->document()
959 - ->reselect(['parent'])
960 - ->appendRawWhere( 'and', " `type` in ('popup', 'banner-bar')" )
961 - ->where('status', 'publish')
962 - ->where('parent', 'NOT LIKE', '0' )
963 - ->get();
964 -
965 - $revisions = $revisions ? $revisions->toArray() : [];
966 - $revisionParents = array_map( function( $record ){
967 - return $record['parent'];
968 - }, $revisions );
969 -
970 - return array_unique( Arr::merge( $revisionParents, $parents ) );
971 -
972 - } catch ( \Exception $exception ) {
973 - return [];
974 - }
975 - }
976 -
977 - /**
978 - * Get number of published documents
979 - *
980 - * @return int
981 - */
982 - public function getNumberOfPublishedDocuments(){
983 - try{
984 - $publishedDocuments = $this->document()->where( 'parent', '0' )->published()->count();
985 - $draftDocuments = $this->document()->select('id')->where( 'parent', '0' )->draft()->findAll()->get();
986 - if ( $draftDocuments ) {
987 - $draftDocuments = $draftDocuments->toArray();
988 - foreach ( $draftDocuments as $document ) {
989 - if ( $this->isPublishedBefore( $document['id'] ) ) {
990 - ++$publishedDocuments;
991 - }
992 - }
993 - }
994 -
995 - return $publishedDocuments;
996 - } catch ( Exception $e ){
997 - return 0;
998 - }
999 - }
1000 775 }