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