DemoSites.php
4 years ago
DemoSitesHelpers.php
4 years ago
DemoSitesImportBlockMap.php
4 years ago
DemoSitesImporter.php
4 years ago
DemoSitesLogger.php
4 years ago
DemoSitesRepository.php
4 years ago
WXRExporter.php
4 years ago
WXRImporter.php
4 years ago
WpCliCommand.php
4 years ago
DemoSitesRepository.php
183 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\DemoSites; |
| 4 | |
| 5 | use IlluminateAgnostic\Arr\Support\Arr; |
| 6 | use Kubio\Core\LodashBasic; |
| 7 | |
| 8 | class DemoSitesRepository { |
| 9 | |
| 10 | const DEMO_SITES_TRANSIENT = 'kubio-demo-sites-repository'; |
| 11 | private static $instance = null; |
| 12 | public $demos = array(); |
| 13 | |
| 14 | public function __construct() { |
| 15 | |
| 16 | $data = get_transient( self::DEMO_SITES_TRANSIENT ); |
| 17 | if ( $data !== false ) { |
| 18 | $version = Arr::get( $data, 'version' ); |
| 19 | if ( $version !== KUBIO_VERSION ) { |
| 20 | $this->prepareRetrieveDemoSites(); |
| 21 | } |
| 22 | $this->demos = $data; |
| 23 | } else { |
| 24 | $this->prepareRetrieveDemoSites(); |
| 25 | } |
| 26 | |
| 27 | add_filter( 'kubio/demo-sites/list', array( $this, 'getDemoSitesList' ) ); |
| 28 | add_action( 'wp_ajax_kubio-demo-sites-retrieve', array( $this, 'actionRetrieveDemoSites' ) ); |
| 29 | } |
| 30 | |
| 31 | public function actionRetrieveDemoSites() { |
| 32 | return $this->retrieveDemoSites(); |
| 33 | } |
| 34 | |
| 35 | public function prepareRetrieveDemoSites() { |
| 36 | $empty = empty( $this->getDemoSitesList() ); |
| 37 | $reload = $empty && Arr::get( $_REQUEST, 'tab' ) === 'demo-sites' && Arr::get( $_REQUEST, 'page' ) === 'kubio-get-started'; |
| 38 | |
| 39 | add_action( |
| 40 | 'admin_footer', |
| 41 | function () use ( $reload ) { |
| 42 | $fetch_url = add_query_arg( |
| 43 | array( 'action' => 'kubio-demo-sites-retrieve' ), |
| 44 | admin_url( 'admin-ajax.php' ) |
| 45 | ); |
| 46 | ?> |
| 47 | <script> |
| 48 | window.fetch("<?php echo esc_js( $fetch_url ); ?>").then(()=>{ |
| 49 | if(<?php echo $reload ? 'true' : 'false'; ?>){ |
| 50 | window.location.reload(); |
| 51 | } |
| 52 | }) |
| 53 | </script> |
| 54 | <?php |
| 55 | } |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | public static function getDemos( $with_internals = false ) { |
| 60 | $demos = static::getInstance()->getDemoSitesList(); |
| 61 | |
| 62 | if ( ! $with_internals ) { |
| 63 | foreach ( $demos as $slug => $demo ) { |
| 64 | if ( Arr::get( $demo, 'internal', false ) ) { |
| 65 | unset( $demos[ $slug ] ); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return $demos; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @return DemoSitesRepository |
| 75 | */ |
| 76 | private static function getInstance() { |
| 77 | static::load(); |
| 78 | |
| 79 | return static::$instance; |
| 80 | } |
| 81 | |
| 82 | public static function load() { |
| 83 | if ( ! static::$instance ) { |
| 84 | static::$instance = new DemoSitesRepository(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | public function getDemoSitesList() { |
| 89 | return Arr::get( (array) $this->demos, 'demos', array() ); |
| 90 | } |
| 91 | |
| 92 | public function retrieveDemoSites( $print = true, $verifySSL = true, $scope = null ) { |
| 93 | $demos = array(); |
| 94 | |
| 95 | foreach ( $this->getLocalDemoSites() as $site ) { |
| 96 | $slug = Arr::get( $site, 'slug', null ); |
| 97 | |
| 98 | if ( $slug ) { |
| 99 | $demos[ $slug ] = $site; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | foreach ( $this->getRemoteDemoSites( $verifySSL, $scope ) as $site ) { |
| 104 | $slug = Arr::get( $site, 'slug', null ); |
| 105 | |
| 106 | if ( $slug ) { |
| 107 | $demos[ $slug ] = $site; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // remove keys from assoc array |
| 112 | $this->demos = array( |
| 113 | 'version' => KUBIO_VERSION, |
| 114 | 'demos' => $demos, |
| 115 | ); |
| 116 | |
| 117 | set_transient( self::DEMO_SITES_TRANSIENT, $this->demos, DAY_IN_SECONDS ); |
| 118 | |
| 119 | if ( $print ) { |
| 120 | wp_send_json_success( $this->demos ); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | private function getLocalDemoSites() { |
| 125 | return apply_filters( 'kudio/demo-sites/local-demos', array() ); |
| 126 | } |
| 127 | |
| 128 | public function getRemoteDemoSites( $verifySSL = true, $scope = null ) { |
| 129 | $url = apply_filters( 'kubio/demo-sites/demos-url', 'https://cloud.kubiobuilder.com/api/project/demo-sites' ); |
| 130 | |
| 131 | if ( $scope ) { |
| 132 | $url = add_query_arg( 'scope', $scope, $url ); |
| 133 | } |
| 134 | |
| 135 | if ( ! $url ) { |
| 136 | return array(); |
| 137 | } |
| 138 | |
| 139 | $response = wp_remote_get( |
| 140 | $url, |
| 141 | array( |
| 142 | 'sslverify' => $verifySSL, |
| 143 | ) |
| 144 | ); |
| 145 | |
| 146 | if ( $response instanceof \WP_Error ) { |
| 147 | return array(); |
| 148 | } |
| 149 | |
| 150 | if ( wp_remote_retrieve_response_code( $response ) !== 200 ) { |
| 151 | return array(); |
| 152 | } |
| 153 | |
| 154 | $body = wp_remote_retrieve_body( $response ); |
| 155 | |
| 156 | $response = json_decode( $body, true ); |
| 157 | |
| 158 | $demos = $response['body']; |
| 159 | |
| 160 | //quick fix convert from cloud format to kubio format to be easier to implement. |
| 161 | foreach ( $demos as &$demo ) { |
| 162 | $plugins = LodashBasic::get( $demo, 'plugins', array() ); |
| 163 | $newPlugins = array(); |
| 164 | foreach ( $plugins as $pluginName => $pluginLabel ) { |
| 165 | $pluginNameParts = explode( '/', $pluginName ); |
| 166 | if ( count( $pluginNameParts ) > 0 ) { |
| 167 | $newPlugins[] = array( |
| 168 | 'slug' => $pluginNameParts[0], |
| 169 | 'label' => $pluginLabel, |
| 170 | ); |
| 171 | } |
| 172 | } |
| 173 | $demo['plugins'] = $newPlugins; |
| 174 | } |
| 175 | if ( json_last_error() !== JSON_ERROR_NONE ) { |
| 176 | return array(); |
| 177 | } |
| 178 | |
| 179 | return $demos; |
| 180 | } |
| 181 | |
| 182 | } |
| 183 |