cosmoswp.php
120 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | if ( ! class_exists( 'Advanced_Import_Theme_CosmosWP' ) ) { |
| 7 | |
| 8 | /** |
| 9 | * Functions related to About Block |
| 10 | * |
| 11 | * @package Advanced Import |
| 12 | * @subpackage Advanced_Import_Theme_CosmosWP |
| 13 | * @since 1.0.5 |
| 14 | */ |
| 15 | |
| 16 | class Advanced_Import_Theme_CosmosWP extends Advanced_Import_Theme_Template_Library_Base { |
| 17 | |
| 18 | /** |
| 19 | * Theme name |
| 20 | * This class is crated for cosmoswp theme |
| 21 | * |
| 22 | * @since 1.0.5 |
| 23 | * @access private |
| 24 | */ |
| 25 | private $theme = 'cosmoswp'; |
| 26 | |
| 27 | /** |
| 28 | * Theme author |
| 29 | * This class is crated for cosmoswp theme |
| 30 | * Double check for author |
| 31 | * |
| 32 | * @since 1.0.5 |
| 33 | * @access private |
| 34 | */ |
| 35 | private $theme_author = 'cosmoswp'; |
| 36 | |
| 37 | /** |
| 38 | * Gets an instance of this object. |
| 39 | * Prevents duplicate instances which avoid artefacts and improves performance. |
| 40 | * |
| 41 | * @static |
| 42 | * @access public |
| 43 | * @since 1.0.5 |
| 44 | * @return object |
| 45 | */ |
| 46 | public static function get_instance() { |
| 47 | |
| 48 | // Store the instance locally to avoid private static replication |
| 49 | static $instance = null; |
| 50 | |
| 51 | // Only run these methods if they haven't been ran previously |
| 52 | if ( null === $instance ) { |
| 53 | $instance = new self(); |
| 54 | } |
| 55 | |
| 56 | // Always return the instance |
| 57 | return $instance; |
| 58 | |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Load block library |
| 63 | * Used for blog template loading |
| 64 | * |
| 65 | * @since 1.0.5 |
| 66 | * @package CosmosWP |
| 67 | * @author CosmosWP <info@cosmoswp.com> |
| 68 | * |
| 69 | * @param$current_demo_list array |
| 70 | * @return array |
| 71 | */ |
| 72 | public function add_template_library( $current_demo_list ) { |
| 73 | /*common check*/ |
| 74 | if ( strtolower( advanced_import_get_current_theme_author() ) != $this->theme_author ) { |
| 75 | return $current_demo_list; |
| 76 | } |
| 77 | if ( advanced_import_get_current_theme_slug() != $this->theme ) { |
| 78 | return $current_demo_list; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | For cosmoswp only. |
| 83 | check if cosmoswp template library function exist*/ |
| 84 | if ( function_exists( 'run_cosmoswp_template_library' ) ) { |
| 85 | return $current_demo_list; |
| 86 | } |
| 87 | |
| 88 | /*finally fetch template library data from live*/ |
| 89 | $templates_list = array(); |
| 90 | |
| 91 | $url = 'https://www.demo.cosmoswp.com/wp-json/cosmoswp-demo-api/v1/fetch_templates/'; |
| 92 | $body_args = array( |
| 93 | /*API version*/ |
| 94 | 'api_version' => wp_get_theme()['Version'], |
| 95 | /*lang*/ |
| 96 | 'site_lang' => get_bloginfo( 'language' ), |
| 97 | ); |
| 98 | $raw_json = wp_safe_remote_get( |
| 99 | $url, |
| 100 | array( |
| 101 | 'timeout' => 100, |
| 102 | 'body' => $body_args, |
| 103 | ) |
| 104 | ); |
| 105 | |
| 106 | if ( ! is_wp_error( $raw_json ) ) { |
| 107 | $demo_server = json_decode( wp_remote_retrieve_body( $raw_json ), true ); |
| 108 | if ( json_last_error() === JSON_ERROR_NONE ) { |
| 109 | if ( is_array( $demo_server ) ) { |
| 110 | $templates_list = $demo_server; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return array_merge( $current_demo_list, $templates_list ); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | Advanced_Import_Theme_CosmosWP::get_instance()->run(); |
| 120 |