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