PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.2.0
Kubio AI Page Builder v2.2.0
2.8.3 2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / lib / src / DemoSites / DemoSitesRepository.php
kubio / lib / src / DemoSites Last commit date
DemoSites.php 2 years ago DemoSitesHelpers.php 3 years ago DemoSitesImportBlockMap.php 4 years ago DemoSitesImporter.php 2 years ago DemoSitesLogger.php 4 years ago DemoSitesRepository.php 2 years ago WXRExporter.php 4 years ago WXRImporter.php 2 years ago
DemoSitesRepository.php
246 lines
1 <?php
2
3 namespace Kubio\DemoSites;
4
5 use IlluminateAgnostic\Arr\Support\Arr;
6 use Kubio\Core\LodashBasic;
7 use Kubio\Core\Utils;
8 use Kubio\PluginsManager;
9
10 class DemoSitesRepository {
11
12 const DEMO_SITES_TRANSIENT = 'kubio-demo-sites-repository';
13 private static $instance = null;
14 public $demos = array();
15 public $demoParts = array();
16 public $pluginsStates = null;
17
18 private static $action_added = false;
19
20 public function __construct() {
21
22 $data = get_transient( self::DEMO_SITES_TRANSIENT );
23 if ( $data !== false ) {
24 $version = Arr::get( $data, 'version' );
25 if ( $version !== KUBIO_VERSION ) {
26 $this->prepareRetrieveDemoSites();
27 }
28 $this->demos = $data;
29 } else {
30 $this->prepareRetrieveDemoSites();
31 }
32
33 add_action( 'upgrader_process_complete', array( $this, 'onPluginUpgrade' ), 10, 2 );
34 add_filter( 'kubio/demo-sites/list', array( $this, 'getDemoSitesList' ) );
35 add_action( 'wp_ajax_kubio-demo-sites-retrieve', array( $this, 'actionRetrieveDemoSites' ) );
36 }
37
38 public function onPluginUpgrade( $upgrader, $hook_extra ) {
39 if ( in_array( KUBIO_ENTRY_FILE, Arr::get( $hook_extra, 'plugins', array() ) ) ) {
40 delete_transient( self::DEMO_SITES_TRANSIENT );
41 }
42 }
43
44 public function actionRetrieveDemoSites() {
45 check_ajax_referer('kubio-demo-sites-retrieve-nonce');
46 return $this->retrieveDemoSites();
47 }
48
49 public function prepareRetrieveDemoSites() {
50
51 if ( static::$action_added ) {
52 return;
53 }
54
55 static::$action_added = true;
56
57 $empty = empty( $this->getDemoSitesList() );
58 $reload = $empty && Arr::get( $_REQUEST, 'tab' ) === 'demo-sites' && Arr::get( $_REQUEST, 'page' ) === 'kubio-get-started';
59
60 add_action(
61 'admin_footer',
62 function () use ( $reload ) {
63 $fetch_url = add_query_arg(
64 array(
65 'action' => 'kubio-demo-sites-retrieve',
66 '_wpnonce' => wp_create_nonce( 'kubio-demo-sites-retrieve-nonce' )
67 ),
68 admin_url( 'admin-ajax.php' )
69 );
70
71 ?>
72 <script>
73 window.fetch("<?php echo esc_url_raw( $fetch_url ); ?>").then(()=>{
74 if(<?php echo ( $reload ? 'true' : 'false' ); ?>){
75 window.location.reload();
76 }
77 })
78 </script>
79 <?php
80 }
81 );
82 }
83
84 public static function getDemos( $with_internals = false ) {
85
86 $demos = static::getInstance()->getDemoSitesList();
87
88 if ( ! $with_internals ) {
89 foreach ( $demos as $slug => $demo ) {
90 if ( Arr::get( $demo, 'internal', false ) ) {
91 unset( $demos[ $slug ] );
92 }
93 }
94 }
95
96 return $demos;
97 }
98
99 public static function getPluginsStates() {
100 $demoRepository = static::getInstance();
101
102 if ( $demoRepository->pluginsStates ) {
103 return $demoRepository->pluginsStates;
104 }
105 $demos = $demoRepository::getDemos();
106
107 $pluginsStates = array();
108
109 foreach ( $demos as $demo ) {
110 $plugins = Arr::get( $demo, 'plugins', array() );
111 foreach ( $plugins as $plugin ) {
112 $slug = Arr::get( $plugin, 'slug', null );
113 if ( $slug && ! isset( $pluginsStates[ $slug ] ) ) {
114 $pluginsStates[ $slug ] = PluginsManager::getInstance()->getPluginStatus( $slug );
115 }
116 }
117 }
118
119 $demoRepository->pluginsStates = $pluginsStates;
120
121 return $pluginsStates;
122 }
123
124 /**
125 * @return DemoSitesRepository
126 */
127 public static function getInstance() {
128 static::load();
129
130 return static::$instance;
131 }
132
133 public static function load() {
134 if ( ! static::$instance ) {
135 static::$instance = new DemoSitesRepository();
136 }
137 }
138
139 public function getDemoSitesList() {
140 return Arr::get( (array) $this->demos, 'demos', array() );
141 }
142
143 public function retrieveDemoSites( $print = true, $verifySSL = true, $scope = null ) {
144 $demos = array();
145
146 foreach ( $this->getLocalDemoSites() as $site ) {
147 $slug = Arr::get( $site, 'slug', null );
148
149 if ( $slug ) {
150 $demos[ $slug ] = $site;
151 }
152 }
153
154 foreach ( $this->getRemoteDemoSites( $verifySSL, $scope ) as $site ) {
155 $slug = Arr::get( $site, 'slug', null );
156
157 if ( $slug ) {
158 $demos[ $slug ] = $site;
159 }
160 }
161
162 // remove keys from assoc array
163 $this->demos = array(
164 'version' => KUBIO_VERSION,
165 'demos' => $demos,
166 );
167
168 set_transient( self::DEMO_SITES_TRANSIENT, $this->demos, DAY_IN_SECONDS );
169
170 if ( $print ) {
171 wp_send_json_success( $this->demos );
172 }
173 }
174
175 private function getLocalDemoSites() {
176 return apply_filters( 'kudio/demo-sites/local-demos', array() );
177 }
178
179 public function getRemoteDemoSites( $verifySSL = true, $scope = null ) {
180 $url = Utils::getStarterSitesURL();
181
182 if ( ! $url ) {
183 return array();
184 }
185
186 if ( $scope ) {
187 $url = add_query_arg( 'scope', $scope, $url );
188 }
189
190 if ( defined( 'KUBIO_INCLUDE_TEST_TEMPLATES' ) && KUBIO_INCLUDE_TEST_TEMPLATES === true ) {
191 $url = add_query_arg( 'tests', true, $url );
192 }
193
194 $url = add_query_arg(
195 array(
196 'kubio_version' => KUBIO_VERSION,
197 'kubio_build' => KUBIO_BUILD_NUMBER,
198 ),
199 $url
200 );
201
202 $response = wp_remote_get(
203 $url,
204 array(
205 'sslverify' => $verifySSL,
206 )
207 );
208
209 if ( $response instanceof \WP_Error ) {
210 return array();
211 }
212
213 if ( wp_remote_retrieve_response_code( $response ) !== 200 ) {
214 return array();
215 }
216
217 $body = wp_remote_retrieve_body( $response );
218
219 $response = json_decode( $body, true );
220
221 $demos = $response['body'];
222
223 //quick fix convert from cloud format to kubio format to be easier to implement.
224 foreach ( $demos as &$demo ) {
225 $plugins = LodashBasic::get( $demo, 'plugins', array() );
226 $newPlugins = array();
227 foreach ( $plugins as $pluginName => $pluginLabel ) {
228 $pluginNameParts = explode( '/', $pluginName );
229 if ( count( $pluginNameParts ) > 0 ) {
230 $newPlugins[] = array(
231 'slug' => $pluginNameParts[0],
232 'label' => $pluginLabel,
233 );
234 }
235 }
236 $demo['plugins'] = $newPlugins;
237 }
238 if ( json_last_error() !== JSON_ERROR_NONE ) {
239 return array();
240 }
241
242 return $demos;
243 }
244
245 }
246