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 / Services / AIWizardService.php

AIWizardService.php in Depicter — Popup & Slider Builder trunk, at app/src/Services/AIWizardService.php

158 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Services;
3
4 use Averta\Core\Utility\Media;
5 use Averta\WordPress\Utility\JSON;
6 use Depicter\Document\CSS\Breakpoints;
7 use Depicter\Document\Helper\Helper;
8 use Depicter\Utility\Http;
9
10 /**
11 * AI Wizard Service
12 *
13 * @package Depicter\Services
14 */
15 class AIWizardService
16 {
17
18 /**
19 * Make editor data array
20 * @param array $editorData
21 * @param array $AiGeneratedData
22 *
23 * @return array
24 */
25 public function updateEditorDataByAiContent( array $editorData, array $AiGeneratedData ) {
26 $numberOfSections = count( $AiGeneratedData['sections'] ?? [] );
27
28 if ( $numberOfSections > 0 ) {
29 $firstSectionID = key( $editorData['sections'] );
30 $editorData = \Depicter::editorData()->duplicateSectionWithElements( $firstSectionID, $editorData, $numberOfSections );
31
32 $i = 0;
33 foreach ( $editorData['sections'] as $section ) {
34 // inside the popup we have a teaser section so we need to skip it
35 if ( !empty( $section['type'] ) && $section['type'] != 'section' ) {
36 continue;
37 }
38
39 // replace featured photo for AI Popup documents inside each step
40 if ( !empty( $AiGeneratedData['featuredPhotoData'] ) ) {
41 $editorData['sections'][ $editorData['sectionsList'][ $i ] ] = $this->searchAndReplaceInObject( $editorData['sections'][ $editorData['sectionsList'][ $i ] ], [
42 'imageData' => $AiGeneratedData['featuredPhotoData']
43 ] );
44 }
45
46 $editorData['sections'][ $editorData['sectionsList'][ $i ] ] = $this->searchAndReplaceInObject( $editorData['sections'][ $editorData['sectionsList'][ $i ] ], $AiGeneratedData['sections'][ $i ] );
47 foreach( $section['elements'] as $elementID ) {
48 if ( $editorData['elements'][ $elementID ]['type'] == 'image' ) {
49 if ( empty( $editorData['elements'][ $elementID ]['options']['className'] ) || false === strpos( $editorData['elements'][ $elementID ]['options']['className'], 'targetImage' ) ) {
50 continue;
51 }
52
53 }
54
55 // replace featured photo for AI Popup documents inside each element
56 if ( !empty( $AiGeneratedData['featuredPhotoData'] ) ) {
57 $this->recursiveSearchAndReplaceInElements( $elementID, $editorData['elements'], [
58 'imageData' => $AiGeneratedData['featuredPhotoData']
59 ] );
60 }
61
62 $this->recursiveSearchAndReplaceInElements( $elementID, $editorData['elements'], $AiGeneratedData['sections'][ $i ] );
63 }
64 ++$i;
65 }
66 }
67
68 $last_element = array_key_last( $editorData['elements'] );
69 $last_section = array_key_last( $editorData['sections'] );
70 $last_element_id = explode( '-', $last_element )[1];
71 $last_section_id = explode( '-', $last_section )[1];
72 $editorData['lastId'] = max( $last_element_id, $last_section_id ) + 1;
73
74 // replace special colors with color palette colors
75 $editorDataInString = is_array( $editorData ) ? JSON::encode( $editorData ) : $editorData;
76 $definedColors = ['#012d13', '#112d13', '#212d13', '#312d13', '#412d13'];
77 $editorDataInString = str_ireplace( $definedColors, $AiGeneratedData['colorPalette'], $editorDataInString );
78
79 return JSON::decode( $editorDataInString, true );
80 }
81
82 private function recursiveSearchAndReplaceInElements( $elementID, &$elements, $aiSectionData ) {
83 // Apply search and replace on the current element
84 $elements[ $elementID ] = $this->searchAndReplaceInObject( $elements[ $elementID ], $aiSectionData );
85
86 // If it has children, recursively apply the same function to each child
87 if ( ! empty( $elements[ $elementID ]['children'] ) && is_array( $elements[ $elementID ]['children'] ) ) {
88 foreach ( $elements[ $elementID ]['children'] as $childID ) {
89 $this->recursiveSearchAndReplaceInElements( $childID, $elements, $aiSectionData );
90 }
91 }
92 }
93
94
95
96 /**
97 * search and replace data in duplicated elements
98 *
99 * @param $item
100 * @param $searchReplaceData
101 *
102 * @return mixed
103 */
104 public function searchAndReplaceInObject( $item, $searchReplaceData ) {
105 $item = is_array( $item ) ? JSON::encode( $item ) : $item;
106
107 foreach ( $searchReplaceData as $search => $replace ) {
108 if ( $search == 'imageData' ) {
109 $assetIDs = Helper::extractAssetIds( $item, true );
110 foreach( $assetIDs as $assetID ) {
111 $item = str_replace( $assetID, $replace['id'], $item );
112 }
113 } else {
114 $item = str_ireplace( '{{{'. $search .'}}}' , $replace, $item );
115 }
116 }
117
118 return JSON::decode( $item, true );
119 }
120
121 /**
122 * @param $editorData
123 *
124 * @return false|string
125 */
126 public function fixMediaSizes( $editorData ) {
127 $devices = Breakpoints::names();
128 if ( JSON::isJson( $editorData ) ) {
129 $editorData = JSON::decode( $editorData, true );
130 }
131
132 foreach ( $editorData['sections'] as $section ) {
133 foreach( $section['elements'] as $elementID ) {
134 if ( $editorData['elements'][ $elementID ]['type'] == 'image' ) {
135 $imageID = \Depicter::media()->getAttachmentId( $editorData['elements'][ $elementID ]['options']['source'] );
136 $attachment = wp_get_attachment_image_src( $imageID, 'full' );
137 if ( !$attachment ) {
138 continue;
139 }
140 $originalMediaWidth = $attachment[1] ?: null;
141 $originalMediaHeight = $attachment[2] ?: null;
142
143 foreach( $devices as $device ) {
144 $resizeW = $editorData['elements'][ $elementID ]['cropData'][ $device ]['mediaSize']['width'] ?? null;
145 $resizeH = $editorData['elements'][ $elementID ]['cropData'][ $device ]['mediaSize']['height'] ?? null;
146
147 [ $mediaWidth, $mediaHeight ] = Media::fitInBox( 'contain', $resizeW, $resizeH, $originalMediaWidth, $originalMediaHeight );
148 $editorData['elements'][ $elementID ]['cropData'][ $device ]['mediaSize']['width'] = $mediaWidth;
149 $editorData['elements'][ $elementID ]['cropData'][ $device ]['mediaSize']['height'] = $mediaHeight;
150 }
151 }
152 }
153 }
154
155 return JSON::encode( $editorData );
156 }
157 }
158