IntegrationCatalog.php
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Models; |
| 4 | |
| 5 | /** |
| 6 | * The integration listing model. |
| 7 | */ |
| 8 | class IntegrationCatalog extends ExternalApiModel { |
| 9 | /** |
| 10 | * Rest API endpoint |
| 11 | * |
| 12 | * @var string |
| 13 | */ |
| 14 | protected $endpoint = 'v1'; |
| 15 | |
| 16 | /** |
| 17 | * Default query parameters |
| 18 | * |
| 19 | * @var array |
| 20 | */ |
| 21 | protected $default_query = [ |
| 22 | '_embed' => true, |
| 23 | '_fields' => 'id,slug,title,content,_links.wp:featuredmedia,_embedded.wp:featuredmedia,_links.wp:term,_embedded.wp:term,acf', |
| 24 | 'acf_format' => 'standard', |
| 25 | 'per_page' => 100, |
| 26 | ]; |
| 27 | |
| 28 | /** |
| 29 | * Base URL |
| 30 | * |
| 31 | * @var string |
| 32 | */ |
| 33 | protected $base_url = 'https://integrations-catalog.surecart.com/'; |
| 34 | |
| 35 | /** |
| 36 | * Get the activation type attribute. |
| 37 | * |
| 38 | * @return string |
| 39 | */ |
| 40 | public function getActivationTypeAttribute() { |
| 41 | if ( $this->acf['is_pre_installed'] ) { |
| 42 | return 'pre-installed'; |
| 43 | } |
| 44 | if ( $this->acf['plugin_slug'] && $this->acf['plugin_file'] ) { |
| 45 | return 'plugin'; |
| 46 | } |
| 47 | if ( $this->acf['theme_slug'] && ! $this->acf['activation_link'] ) { |
| 48 | return 'theme'; |
| 49 | } |
| 50 | if ( $this->acf['activation_link'] ) { |
| 51 | return 'external'; |
| 52 | } |
| 53 | return null; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get the button text attribute. |
| 58 | * |
| 59 | * @return string |
| 60 | */ |
| 61 | public function getButtonTextAttribute() { |
| 62 | if ( 'pre-installed' === $this->status ) { |
| 63 | return __( 'Pre-installed', 'surecart' ); |
| 64 | } |
| 65 | if ( 'active' === $this->status ) { |
| 66 | return __( 'Enabled', 'surecart' ); |
| 67 | } |
| 68 | if ( in_array( $this->activation_type, [ 'plugin', 'theme' ], true ) ) { |
| 69 | if ( 'inactive' === $this->status ) { |
| 70 | return __( 'Activate', 'surecart' ); |
| 71 | } |
| 72 | return __( 'Install & Activate', 'surecart' ); |
| 73 | } |
| 74 | return __( 'Activate', 'surecart' ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get the status attribute. |
| 79 | * |
| 80 | * @return string |
| 81 | */ |
| 82 | public function getStatusAttribute() { |
| 83 | if ( $this->acf['is_pre_installed'] ) { |
| 84 | return 'pre-installed'; |
| 85 | } |
| 86 | if ( $this->is_enabled ) { |
| 87 | return 'active'; |
| 88 | } |
| 89 | if ( 'plugin' === $this->activation_type ) { |
| 90 | if ( $this->is_plugin_installed && ! $this->is_plugin_active ) { |
| 91 | return 'inactive'; |
| 92 | } |
| 93 | if ( ! $this->is_plugin_installed ) { |
| 94 | return 'not-installed'; |
| 95 | } |
| 96 | } |
| 97 | return 'inactive'; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Get the is plugin installed attribute. |
| 102 | * |
| 103 | * @return bool |
| 104 | */ |
| 105 | public function getIsPluginInstalledAttribute() { |
| 106 | if ( empty( $this->acf['plugin_file'] ) ) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | $plugin_path = WP_PLUGIN_DIR . '/' . $this->acf['plugin_file']; |
| 111 | return file_exists( $plugin_path ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get the is plugin active attribute. |
| 116 | * |
| 117 | * @return bool |
| 118 | */ |
| 119 | public function getIsPluginActiveAttribute() { |
| 120 | if ( 'pie-calendar' === $this->slug ) { // Since Pie Calendar Free & Pro plugin had same plugin file name we need to check if the pro plugin is active explicitly. |
| 121 | return $this->isPieCalendarProPluginActive(); |
| 122 | } |
| 123 | |
| 124 | if ( empty( $this->acf['plugin_file'] ) ) { |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | return is_plugin_active( $this->acf['plugin_file'] ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get the is plugin active attribute. |
| 133 | * |
| 134 | * @return bool |
| 135 | */ |
| 136 | public function getIsThemeActiveAttribute() { |
| 137 | if ( empty( $this->acf['theme_slug'] ) ) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | return wp_get_theme()->get_template() === $this->acf['theme_slug']; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Get the is enabled attribute. |
| 146 | * |
| 147 | * @return bool |
| 148 | */ |
| 149 | public function getIsEnabledAttribute() { |
| 150 | if ( $this->getIsThemeActiveAttribute() ) { |
| 151 | return true; |
| 152 | } |
| 153 | if ( $this->getIsPluginActiveAttribute() ) { |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Get the logo URL. |
| 162 | * |
| 163 | * @return string |
| 164 | */ |
| 165 | public function getLogoUrlAttribute() { |
| 166 | // svg first. |
| 167 | if ( file_exists( SURECART_PLUGIN_DIR . '/images/integrations/' . $this->slug . '.svg' ) ) { |
| 168 | return untrailingslashit( \SureCart::core()->assets()->getUrl() ) . '/images/integrations/' . $this->slug . '.svg'; |
| 169 | } |
| 170 | |
| 171 | // then png. |
| 172 | if ( file_exists( SURECART_PLUGIN_DIR . '/images/integrations/' . $this->slug . '.png' ) ) { |
| 173 | return untrailingslashit( \SureCart::core()->assets()->getUrl() ) . '/images/integrations/' . $this->slug . '.png'; |
| 174 | } |
| 175 | |
| 176 | // then fallback to the featured media. |
| 177 | return $this->_embedded['wp:featuredmedia'][0]['media_details']['sizes']['medium']['source_url'] ?? $this->_embedded['wp:featuredmedia'][0]['source_url'] ?? ''; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Is Pie Calendar Pro plugin active. |
| 182 | * |
| 183 | * @return bool |
| 184 | */ |
| 185 | public function isPieCalendarProPluginActive() { |
| 186 | if ( ! function_exists( 'piecal_pro_classic_metabox' ) ) { |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | return true; |
| 191 | } |
| 192 | } |
| 193 |