| @@ -1,0 +1,305 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace ABlocks\traits; | |
| 4 | + | |
| 5 | +use ABlocks\Helper; | |
| 6 | +use ABlocks\import\ImageParserFactory; | |
| 7 | +use ABlocks\import\WP_Import; | |
| 8 | +use Plugin_Upgrader; | |
| 9 | +use Theme_Upgrader; | |
| 10 | +use WP_Ajax_Upgrader_Skin; | |
| 11 | +use WP_Error; | |
| 12 | + | |
| 13 | +trait Importer { | |
| 14 | + | |
| 15 | + /** | |
| 16 | + * Check plugin installed and active. | |
| 17 | + * | |
| 18 | + * @param string $slug Plugin Slug. | |
| 19 | + * | |
| 20 | + * @return object | |
| 21 | + */ | |
| 22 | + public static function check_plugin( string $slug ): object { | |
| 23 | + $installed = self::get_plugin_name( $slug ); | |
| 24 | + if ( is_wp_error( $installed ) ) { | |
| 25 | + return (object) array( | |
| 26 | + 'installed' => false, | |
| 27 | + 'activated' => false, | |
| 28 | + ); | |
| 29 | + } | |
| 30 | + | |
| 31 | + if ( is_plugin_active( $installed ) ) { | |
| 32 | + return (object) array( | |
| 33 | + 'installed' => true, | |
| 34 | + 'activated' => true, | |
| 35 | + ); | |
| 36 | + } | |
| 37 | + | |
| 38 | + return (object) array( | |
| 39 | + 'installed' => true, | |
| 40 | + 'activated' => false, | |
| 41 | + ); | |
| 42 | + } | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * Check theme installed and active. | |
| 46 | + * | |
| 47 | + * @param string $slug Theme Slug. | |
| 48 | + * | |
| 49 | + * @return object | |
| 50 | + */ | |
| 51 | + public static function check_theme( string $slug ): object { | |
| 52 | + $installed_themes = wp_get_themes(); | |
| 53 | + if ( ! isset( $installed_themes[ $slug ] ) ) { | |
| 54 | + return (object) array( | |
| 55 | + 'installed' => false, | |
| 56 | + 'activated' => false, | |
| 57 | + ); | |
| 58 | + } | |
| 59 | + | |
| 60 | + if ( get_template() === $slug ) { | |
| 61 | + return (object) array( | |
| 62 | + 'installed' => true, | |
| 63 | + 'activated' => true, | |
| 64 | + ); | |
| 65 | + } | |
| 66 | + | |
| 67 | + return (object) array( | |
| 68 | + 'installed' => true, | |
| 69 | + 'activated' => false, | |
| 70 | + ); | |
| 71 | + } | |
| 72 | + | |
| 73 | + /** | |
| 74 | + * Import template from XML file. | |
| 75 | + * | |
| 76 | + * @param string $file_path File path. | |
| 77 | + * @param bool $with_attachments with attachments ?. | |
| 78 | + * | |
| 79 | + * @return true|WP_Error | |
| 80 | + */ | |
| 81 | + public static function import_template( string $file_path, bool $with_attachments = true ) { | |
| 82 | + require_once ABSPATH . 'wp-admin/includes/class-wp-importer.php'; | |
| 83 | + | |
| 84 | + if ( ! file_exists( $file_path ) ) { | |
| 85 | + return new WP_Error( | |
| 86 | + 'invalid_file', | |
| 87 | + __( 'Invalid content file location provided!', 'ablocks' ), | |
| 88 | + ); | |
| 89 | + } | |
| 90 | + | |
| 91 | + // Import the XML file. | |
| 92 | + $importer = new WP_Import(); | |
| 93 | + $result = $importer->import( $file_path, $with_attachments ); | |
| 94 | + | |
| 95 | + // Delete the temporary file if it exists using wp_delete_file. | |
| 96 | + if ( file_exists( $file_path ) && dirname( $file_path ) === sys_get_temp_dir() ) { | |
| 97 | + wp_delete_file( $file_path ); | |
| 98 | + } | |
| 99 | + | |
| 100 | + if ( is_wp_error( $result ) ) { | |
| 101 | + return $result; | |
| 102 | + } | |
| 103 | + | |
| 104 | + return true; | |
| 105 | + } | |
| 106 | + | |
| 107 | + public static function remote_to_tmp_file( string $file_url ) { | |
| 108 | + $tmp_file = $file_url; | |
| 109 | + if ( wp_http_validate_url( $file_url ) ) { | |
| 110 | + $tmp_file = download_url( $file_url ); | |
| 111 | + if ( is_wp_error( $tmp_file ) ) { | |
| 112 | + return $tmp_file; | |
| 113 | + } | |
| 114 | + } | |
| 115 | + return $tmp_file; | |
| 116 | + } | |
| 117 | + | |
| 118 | + /** | |
| 119 | + * Install/Activate Free Plugins. | |
| 120 | + * | |
| 121 | + * @param string $plugin_slug Slug that match on wp.org repo. | |
| 122 | + * | |
| 123 | + * @return true|WP_Error | |
| 124 | + */ | |
| 125 | + public static function install_and_active_plugin( string $plugin_slug ) { | |
| 126 | + // Include required WordPress files for plugin management. | |
| 127 | + self::require_dependencies(); | |
| 128 | + require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; | |
| 129 | + | |
| 130 | + // Check if the plugin is already installed. | |
| 131 | + $installed_plugins = get_plugins(); | |
| 132 | + foreach ( $installed_plugins as $plugin_file => $plugin_data ) { | |
| 133 | + if ( strpos( $plugin_file, $plugin_slug . '/' ) === 0 ) { | |
| 134 | + $activation = activate_plugin( $plugin_file, false, false, true ); | |
| 135 | + if ( is_wp_error( $activation ) ) { | |
| 136 | + return $activation; | |
| 137 | + } | |
| 138 | + return true; | |
| 139 | + } | |
| 140 | + } | |
| 141 | + | |
| 142 | + // Get plugin information from WordPress.org | |
| 143 | + $plugin_info = plugins_api('plugin_information', array( | |
| 144 | + 'slug' => $plugin_slug, | |
| 145 | + 'fields' => array( | |
| 146 | + 'sections' => false, | |
| 147 | + 'tested' => false, | |
| 148 | + ), | |
| 149 | + )); | |
| 150 | + | |
| 151 | + if ( is_wp_error( $plugin_info ) ) { | |
| 152 | + return $plugin_info; | |
| 153 | + } | |
| 154 | + | |
| 155 | + // Set up the installer | |
| 156 | + $upgrader = new Plugin_Upgrader( new WP_Ajax_Upgrader_Skin() ); | |
| 157 | + $result = $upgrader->install( $plugin_info->download_link ); | |
| 158 | + | |
| 159 | + if ( is_wp_error( $result ) ) { | |
| 160 | + return $result; | |
| 161 | + } | |
| 162 | + | |
| 163 | + // Activate the plugin. | |
| 164 | + $plugin_name = self::get_plugin_name( $plugin_slug ); | |
| 165 | + if ( is_wp_error( $plugin_name ) ) { | |
| 166 | + return $plugin_name; | |
| 167 | + } | |
| 168 | + | |
| 169 | + if ( ! is_plugin_active( $plugin_name ) ) { | |
| 170 | + $activation = activate_plugin( $plugin_name, false, false, true ); | |
| 171 | + | |
| 172 | + if ( is_wp_error( $activation ) ) { | |
| 173 | + return $activation; | |
| 174 | + } | |
| 175 | + } | |
| 176 | + | |
| 177 | + return true; | |
| 178 | + } | |
| 179 | + | |
| 180 | + /** | |
| 181 | + * Install/Activate Free Themes. | |
| 182 | + * | |
| 183 | + * @param string $theme_slug Slug that match on wp.org repo. | |
| 184 | + * | |
| 185 | + * @return true|WP_Error | |
| 186 | + */ | |
| 187 | + public static function install_and_active_theme( string $theme_slug ) { | |
| 188 | + // Include required WordPress files for theme management. | |
| 189 | + require_once ABSPATH . 'wp-admin/includes/theme.php'; | |
| 190 | + self::require_dependencies(); | |
| 191 | + | |
| 192 | + // Check if the theme is already installed. | |
| 193 | + $installed_themes = wp_get_themes(); | |
| 194 | + if ( isset( $installed_themes[ $theme_slug ] ) ) { | |
| 195 | + switch_theme( $theme_slug ); | |
| 196 | + return true; | |
| 197 | + } | |
| 198 | + | |
| 199 | + // Fetch theme information from wp.org repo. | |
| 200 | + $theme_info = themes_api('theme_information', array( | |
| 201 | + 'slug' => $theme_slug, | |
| 202 | + 'fields' => array( | |
| 203 | + 'sections' => false, | |
| 204 | + 'tested' => false, | |
| 205 | + ), | |
| 206 | + )); | |
| 207 | + | |
| 208 | + if ( is_wp_error( $theme_info ) ) { | |
| 209 | + return $theme_info; | |
| 210 | + } | |
| 211 | + | |
| 212 | + // Set up the installer. | |
| 213 | + $upgrader = new Theme_Upgrader( new WP_Ajax_Upgrader_Skin() ); | |
| 214 | + $result = $upgrader->install( $theme_info->download_link ); | |
| 215 | + | |
| 216 | + if ( is_wp_error( $result ) ) { | |
| 217 | + return $result; | |
| 218 | + } | |
| 219 | + | |
| 220 | + // Activate the theme | |
| 221 | + if ( wp_get_theme( $theme_slug )->exists() ) { | |
| 222 | + switch_theme( $theme_slug ); | |
| 223 | + return true; | |
| 224 | + } | |
| 225 | + | |
| 226 | + return new WP_Error( 400, 'Theme installed but could not be activated' ); | |
| 227 | + } | |
| 228 | + | |
| 229 | + /** | |
| 230 | + * Get plugin name from directory name. | |
| 231 | + * | |
| 232 | + * @param string $directory Directory/slug. | |
| 233 | + * | |
| 234 | + * @return WP_Error|string | |
| 235 | + */ | |
| 236 | + public static function get_plugin_name( string $directory ) { | |
| 237 | + $plugin_data = get_plugins( '/' . $directory ); | |
| 238 | + $plugin_root_file = count( $plugin_data ) > 0 ? array_key_first( $plugin_data ) : false; | |
| 239 | + if ( ! $plugin_root_file ) { | |
| 240 | + return new WP_Error( 400, 'Invalid plugin folder!' ); | |
| 241 | + } | |
| 242 | + | |
| 243 | + return $directory . '/' . $plugin_root_file; | |
| 244 | + } | |
| 245 | + | |
| 246 | + /** | |
| 247 | + * Get theme demos config with raw demo data. | |
| 248 | + * | |
| 249 | + * @return false|array | |
| 250 | + */ | |
| 251 | + public static function get_theme_demo_config() { | |
| 252 | + return apply_filters( 'ablocks/register_theme', [ | |
| 253 | + 'menu_title' => __( 'aBlocks Templates', 'ablocks' ), | |
| 254 | + 'page_title' => __( 'aBlocks Templates', 'ablocks' ), | |
| 255 | + 'menu_slug' => ABLOCKS_PLUGIN_SLUG . '-demo-import', | |
| 256 | + 'preloaded_demo' => true, | |
| 257 | + 'preloaded_demo_category' => [], // set blank array for all. | |
| 258 | + 'demos' => [], | |
| 259 | + ] ); | |
| 260 | + } | |
| 261 | + | |
| 262 | + /** | |
| 263 | + * Parse remote images from serialized blocks content. | |
| 264 | + * | |
| 265 | + * @param string $block_content Serialized block content. | |
| 266 | + * | |
| 267 | + * @return string | |
| 268 | + */ | |
| 269 | + public static function parse_remote_images( string $block_content ): string { | |
| 270 | + $blocks = parse_blocks( $block_content ); | |
| 271 | + $blocks = ImageParserFactory::instance()->parse( $blocks ); | |
| 272 | + | |
| 273 | + $serialized_blocks = ''; | |
| 274 | + foreach ( $blocks as $block ) { | |
| 275 | + $serialized_blocks .= serialize_block( $block ); | |
| 276 | + } | |
| 277 | + return $serialized_blocks; | |
| 278 | + } | |
| 279 | + | |
| 280 | + /** | |
| 281 | + * Emit a Server-Sent Events message. | |
| 282 | + * | |
| 283 | + * @param mixed $data Data to be JSON-encoded and sent in the message. | |
| 284 | + */ | |
| 285 | + public static function emit_sse_message( $data ) { | |
| 286 | + echo "event: message\n"; | |
| 287 | + echo 'data: ' . wp_json_encode( $data ) . "\n\n"; | |
| 288 | + | |
| 289 | + // Extra padding. | |
| 290 | + echo esc_html( ':' . str_repeat( ' ', 2048 ) . "\n\n" ); | |
| 291 | + | |
| 292 | + flush(); | |
| 293 | + } | |
| 294 | + | |
| 295 | + /** | |
| 296 | + * Commonly used dependencies here. | |
| 297 | + * | |
| 298 | + * @return void | |
| 299 | + */ | |
| 300 | + private static function require_dependencies() { | |
| 301 | + require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 302 | + require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; | |
| 303 | + } | |
| 304 | + | |
| 305 | +} | |