DemoSites.php
1 year ago
DemoSitesHelpers.php
1 year ago
DemoSitesImportBlockMap.php
1 year ago
DemoSitesImporter.php
1 year ago
DemoSitesLogger.php
4 years ago
DemoSitesRepository.php
1 year ago
WXRExporter.php
1 year ago
WXRImporter.php
1 year 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 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 60 | $reload = $empty && Arr::get( $_REQUEST, 'tab' ) === 'demo-sites' && Arr::get( $_REQUEST, 'page' ) === 'kubio-get-started'; |
| 61 | |
| 62 | add_action( |
| 63 | 'admin_footer', |
| 64 | function () use ( $reload ) { |
| 65 | $fetch_url = add_query_arg( |
| 66 | array( |
| 67 | 'action' => 'kubio-demo-sites-retrieve', |
| 68 | '_wpnonce' => wp_create_nonce( 'kubio-demo-sites-retrieve-nonce' ), |
| 69 | ), |
| 70 | admin_url( 'admin-ajax.php' ) |
| 71 | ); |
| 72 | |
| 73 | ?> |
| 74 | <script> |
| 75 | window.fetch("<?php echo esc_url_raw( $fetch_url ); ?>").then(()=>{ |
| 76 | if(<?php echo ( $reload ? 'true' : 'false' ); ?>){ |
| 77 | window.location.reload(); |
| 78 | } |
| 79 | }) |
| 80 | </script> |
| 81 | <?php |
| 82 | } |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | public static function getDemos( $with_internals = false ) { |
| 87 | |
| 88 | $demos = static::getInstance()->getDemoSitesList(); |
| 89 | |
| 90 | if ( ! $with_internals ) { |
| 91 | foreach ( $demos as $slug => $demo ) { |
| 92 | if ( Arr::get( $demo, 'internal', false ) ) { |
| 93 | unset( $demos[ $slug ] ); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return $demos; |
| 99 | } |
| 100 | |
| 101 | public static function getPluginsStates() { |
| 102 | $demoRepository = static::getInstance(); |
| 103 | |
| 104 | if ( $demoRepository->pluginsStates ) { |
| 105 | return $demoRepository->pluginsStates; |
| 106 | } |
| 107 | $demos = $demoRepository::getDemos(); |
| 108 | |
| 109 | $pluginsStates = array(); |
| 110 | |
| 111 | foreach ( $demos as $demo ) { |
| 112 | $plugins = Arr::get( $demo, 'plugins', array() ); |
| 113 | foreach ( $plugins as $plugin ) { |
| 114 | $slug = Arr::get( $plugin, 'slug', null ); |
| 115 | if ( $slug && ! isset( $pluginsStates[ $slug ] ) ) { |
| 116 | $pluginsStates[ $slug ] = PluginsManager::getInstance()->getPluginStatus( $slug ); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | $demoRepository->pluginsStates = $pluginsStates; |
| 122 | |
| 123 | return $pluginsStates; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @return DemoSitesRepository |
| 128 | */ |
| 129 | public static function getInstance() { |
| 130 | static::load(); |
| 131 | |
| 132 | return static::$instance; |
| 133 | } |
| 134 | |
| 135 | public static function load() { |
| 136 | if ( ! static::$instance ) { |
| 137 | static::$instance = new DemoSitesRepository(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | public function getDemoSitesList() { |
| 142 | return Arr::get( (array) $this->demos, 'demos', array() ); |
| 143 | } |
| 144 | |
| 145 | public function retrieveDemoSites( $print = true, $verifySSL = true, $scope = null ) { |
| 146 | $demos = array(); |
| 147 | |
| 148 | foreach ( $this->getLocalDemoSites() as $site ) { |
| 149 | $slug = Arr::get( $site, 'slug', null ); |
| 150 | |
| 151 | if ( $slug ) { |
| 152 | $demos[ $slug ] = $site; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | foreach ( $this->getRemoteDemoSites( $verifySSL, $scope ) as $site ) { |
| 157 | $slug = Arr::get( $site, 'slug', null ); |
| 158 | |
| 159 | if ( $slug ) { |
| 160 | $demos[ $slug ] = $site; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // remove keys from assoc array |
| 165 | $this->demos = array( |
| 166 | 'version' => KUBIO_VERSION, |
| 167 | 'demos' => $demos, |
| 168 | ); |
| 169 | |
| 170 | set_transient( self::DEMO_SITES_TRANSIENT, $this->demos, DAY_IN_SECONDS ); |
| 171 | |
| 172 | if ( $print ) { |
| 173 | wp_send_json_success( $this->demos ); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | private function getLocalDemoSites() { |
| 178 | return apply_filters( 'kudio/demo-sites/local-demos', array() ); |
| 179 | } |
| 180 | |
| 181 | public function getRemoteDemoSites( $verifySSL = true, $scope = null ) { |
| 182 | $url = Utils::getStarterSitesURL(); |
| 183 | |
| 184 | if ( ! $url ) { |
| 185 | return array(); |
| 186 | } |
| 187 | |
| 188 | if ( $scope ) { |
| 189 | $url = add_query_arg( 'scope', $scope, $url ); |
| 190 | } |
| 191 | |
| 192 | if ( defined( 'KUBIO_INCLUDE_TEST_TEMPLATES' ) && KUBIO_INCLUDE_TEST_TEMPLATES === true ) { |
| 193 | $url = add_query_arg( 'tests', true, $url ); |
| 194 | } |
| 195 | |
| 196 | $url = add_query_arg( |
| 197 | array( |
| 198 | 'kubio_version' => KUBIO_VERSION, |
| 199 | 'kubio_build' => KUBIO_BUILD_NUMBER, |
| 200 | 'activated_on' => Flags::get( 'kubio_activation_time', '' ), |
| 201 | ), |
| 202 | $url |
| 203 | ); |
| 204 | |
| 205 | $response = wp_remote_get( |
| 206 | $url, |
| 207 | array( |
| 208 | 'sslverify' => $verifySSL, |
| 209 | ) |
| 210 | ); |
| 211 | |
| 212 | if ( $response instanceof \WP_Error ) { |
| 213 | return array(); |
| 214 | } |
| 215 | |
| 216 | if ( wp_remote_retrieve_response_code( $response ) !== 200 ) { |
| 217 | return array(); |
| 218 | } |
| 219 | |
| 220 | $body = wp_remote_retrieve_body( $response ); |
| 221 | |
| 222 | $response = json_decode( $body, true ); |
| 223 | |
| 224 | $demos = $response['body']; |
| 225 | |
| 226 | //quick fix convert from cloud format to kubio format to be easier to implement. |
| 227 | foreach ( $demos as &$demo ) { |
| 228 | $plugins = LodashBasic::get( $demo, 'plugins', array() ); |
| 229 | $newPlugins = array(); |
| 230 | foreach ( $plugins as $pluginName => $pluginLabel ) { |
| 231 | $pluginNameParts = explode( '/', $pluginName ); |
| 232 | if ( count( $pluginNameParts ) > 0 ) { |
| 233 | $newPlugins[] = array( |
| 234 | 'slug' => $pluginNameParts[0], |
| 235 | 'label' => $pluginLabel, |
| 236 | ); |
| 237 | } |
| 238 | } |
| 239 | $demo['plugins'] = $newPlugins; |
| 240 | } |
| 241 | if ( json_last_error() !== JSON_ERROR_NONE ) { |
| 242 | return array(); |
| 243 | } |
| 244 | |
| 245 | return $demos; |
| 246 | } |
| 247 | } |
| 248 |