diff --git a/assets/js/admin-builder-archive.js b/assets/js/admin-builder-archive.js index 102e2e4d5c..45df5c12d3 100644 --- a/assets/js/admin-builder-archive.js +++ b/assets/js/admin-builder-archive.js @@ -289,6 +289,51 @@ __webpack_require__.r(__webpack_exports__); +window.addEventListener('DOMContentLoaded', function () { + var $ = jQuery; + var axios = __webpack_require__(/*! axios */ "./node_modules/axios/index.js").default; + + // Migration Link + $('.directorist_directory_template_library').on('click', function (e) { + e.preventDefault(); + var self = this; + $('.cptm-create-directory-modal__action').after("Installing Templetiq, Please wait.."); + var form_data = new FormData(); + form_data.append('action', 'directorist_directory_type_library'); + form_data.append('directorist_nonce', directorist_admin.directorist_nonce); + + // Response Success Callback + var responseSuccessCallback = function responseSuccessCallback(response) { + var _response$data; + if (response !== null && response !== void 0 && (_response$data = response.data) !== null && _response$data !== void 0 && _response$data.success) { + var _response$data$messag, _response$data2; + var msg = (_response$data$messag = response === null || response === void 0 || (_response$data2 = response.data) === null || _response$data2 === void 0 ? void 0 : _response$data2.message) !== null && _response$data$messag !== void 0 ? _response$data$messag : 'Imported successfully!'; + $('.directorist_template_notice').text(msg); + location.reload(); + return; + } + responseFaildCallback(response); + }; + + // Response Error Callback + var responseFaildCallback = function responseFaildCallback(response) { + var _response$data$messag2, _response$data3; + // console.log( { response } ); + + var msg = (_response$data$messag2 = response === null || response === void 0 || (_response$data3 = response.data) === null || _response$data3 === void 0 ? void 0 : _response$data3.message) !== null && _response$data$messag2 !== void 0 ? _response$data$messag2 : 'Something went wrong please try again'; + var alert_content = "\n
\n
\n \n
\n\n
".concat(msg, "
\n
\n "); + $('.cptm-directory-migration-form').find('.cptm-comfirmation-text').html(alert_content); + $(self).remove(); + }; + + // Send Request + axios.post(directorist_admin.ajax_url, form_data).then(function (response) { + responseSuccessCallback(response); + }).catch(function (response) { + responseFaildCallback(response); + }); + }); +}); /***/ }), diff --git a/assets/src/js/admin/multi-directory-archive.js b/assets/src/js/admin/multi-directory-archive.js index 356fcb59d1..aa473e41a6 100644 --- a/assets/src/js/admin/multi-directory-archive.js +++ b/assets/src/js/admin/multi-directory-archive.js @@ -1,4 +1,62 @@ // Scrips import './components/import-directory-modal'; import './components/delete-directory-modal'; -import './components/directory-migration-modal'; \ No newline at end of file +import './components/directory-migration-modal'; + +window.addEventListener('DOMContentLoaded', () => { + var $ = jQuery; + const axios = require('axios').default; + + // Migration Link + $( '.directorist_directory_template_library' ).on( 'click', function( e ) { + e.preventDefault(); + const self = this; + + $( '.cptm-create-directory-modal__action' ).after( "Installing Templetiq, Please wait.." ); + + let form_data = new FormData(); + form_data.append( 'action', 'directorist_directory_type_library' ); + form_data.append('directorist_nonce', directorist_admin.directorist_nonce); + + // Response Success Callback + const responseSuccessCallback = function ( response ) { + + if ( response?.data?.success ) { + let msg = ( response?.data?.message ) ?? 'Imported successfully!'; + + $( '.directorist_template_notice' ).text( msg ); + + location.reload(); + return; + } + + responseFaildCallback( response ); + }; + + // Response Error Callback + const responseFaildCallback = function ( response ) { + // console.log( { response } ); + + let msg = ( response?.data?.message ) ?? 'Something went wrong please try again'; + let alert_content = ` +
+
+ +
+ +
${msg}
+
+ `; + + $( '.cptm-directory-migration-form' ).find( '.cptm-comfirmation-text' ).html( alert_content ); + $( self ).remove(); + }; + + // Send Request + axios.post( directorist_admin.ajax_url, form_data ).then( response => { + responseSuccessCallback( response ); + }).catch( response => { + responseFaildCallback( response ); + }); + }); +}); \ No newline at end of file diff --git a/includes/classes/class-setup-wizard.php b/includes/classes/class-setup-wizard.php index 4eabea7ca3..847df449d6 100644 --- a/includes/classes/class-setup-wizard.php +++ b/includes/classes/class-setup-wizard.php @@ -95,12 +95,6 @@ public function directorist_setup_wizard() { $data['log'] = 'Importing ' . $type['name'] . ' type...'; - // if( isset( $_POST['required_plugins'] ) && ! empty( $type['required_plugins'] ) ) { - // foreach( $type['required_plugins'] as $plugin ) { - // $this->download_plugin( [ 'url' => $plugin ] ); - // } - // } - $dummy_data = $type['listing_data']; $builder_file_url = $type['url']; @@ -142,120 +136,6 @@ public function directorist_setup_wizard() { wp_send_json( $data ); } - protected static function is_varified_host( $extension_url ) { - $signed_hostnames = array( 'directorist.com' ); - - return in_array( parse_url( $extension_url, PHP_URL_HOST ), $signed_hostnames, true ); - } - - private static function download_plugin( array $args = array() ) { - $status = array( 'success' => false ); - - $default = array( - 'url' => '', - 'init_wp_filesystem' => true, - ); - $args = array_merge( $default, $args ); - - if ( empty( $args['url'] ) || ! self::is_varified_host( $args['url'] ) ) { - $status['success'] = false; - $status['message'] = __( 'Invalid download link', 'directorist' ); - - return $status; - } - - global $wp_filesystem; - - if ( $args['init_wp_filesystem'] ) { - - if ( ! function_exists( 'WP_Filesystem' ) ) { - include ABSPATH . 'wp-admin/includes/file.php'; - } - - WP_Filesystem(); - } - - $plugin_path = WP_CONTENT_DIR . '/plugins'; - $temp_dest = "{$plugin_path}/atbdp-temp-dir"; - $file_url = $args['url']; - $file_name = basename( $file_url ); - $tmp_file = download_url( $file_url ); - - if ( ! is_string( $tmp_file ) ) { - $status['success'] = false; - $status['tmp_file'] = $tmp_file; - $status['file_url'] = $file_url; - $status['message'] = 'Could not download the file'; - - return $status; - } - - // Make Temp Dir - if ( $wp_filesystem->exists( $temp_dest ) ) { - $wp_filesystem->delete( $temp_dest, true ); - } - - $wp_filesystem->mkdir( $temp_dest ); - - if ( ! file_exists( $temp_dest ) ) { - $status['success'] = false; - $status['message'] = __( 'Could not create temp directory', 'directorist' ); - - return $status; - } - - // Sets file temp destination. - $file_path = "{$temp_dest}/{$file_name}"; - - set_error_handler( - function ( $errno, $errstr, $errfile, $errline ) { - // error was suppressed with the @-operator - if ( 0 === error_reporting() ) { - return false; - } - - throw new ErrorException( $errstr, 0, $errno, $errfile, $errline ); - } - ); - - // Copies the file to the final destination and deletes temporary file. - try { - copy( $tmp_file, $file_path ); - } catch ( Exception $e ) { - $status['success'] = false; - $status['message'] = $e->getMessage(); - - return $status; - } - - @unlink( $tmp_file ); - unzip_file( $file_path, $temp_dest ); - - if ( "{$plugin_path}/" !== $file_path || $file_path !== $plugin_path ) { - @unlink( $file_path ); - } - - $extracted_file_dir = glob( "{$temp_dest}/*", GLOB_ONLYDIR ); - - foreach ( $extracted_file_dir as $dir_path ) { - $dir_name = basename( $dir_path ); - $dest_path = "{$plugin_path}/{$dir_name}"; - - // Delete Previous Files if Exists - if ( $wp_filesystem->exists( $dest_path ) ) { - $wp_filesystem->delete( $dest_path, true ); - } - } - - copy_dir( $temp_dest, $plugin_path ); - $wp_filesystem->delete( $temp_dest, true ); - - $status['success'] = true; - $status['message'] = __( 'The plugin has been downloaded successfully', 'directorist' ); - - return $status; - } - public function render_run_admin_setup_wizard_notice() { $setup_wizard = get_option( 'directorist_setup_wizard_completed' ); @@ -750,7 +630,7 @@ public function directorist_step_two_save() if( ! empty( $_post_data['active_gateways'] ) && in_array( 'paypal_gateway',$_post_data['active_gateways'] ) ) { - self::download_plugin( [ 'url' => 'https://directorist.com/wp-content/uploads/edd/2022/10/directorist-paypal.zip' ] ); + directorist_download_plugin( [ 'url' => 'https://directorist.com/wp-content/uploads/edd/2022/10/directorist-paypal.zip' ] ); $path = WP_PLUGIN_DIR . '/directorist-paypal/directorist-paypal.php'; diff --git a/includes/helper-functions.php b/includes/helper-functions.php index 53bbf6b6d4..816f2d005c 100644 --- a/includes/helper-functions.php +++ b/includes/helper-functions.php @@ -4418,3 +4418,113 @@ function directorist_delete_listing_empty_metadata( $listing_id, array $metadata delete_post_meta( $listing_id, $deletable_meta_key ); } } + +function directorist_download_plugin( array $args = array() ) { + $status = array( 'success' => false ); + + $default = array( + 'url' => '', + 'init_wp_filesystem' => true, + ); + $args = array_merge( $default, $args ); + + $allowed_host = array( 'directorist.com', 'wordpress.org', 'downloads.wordpress.org' ); + + if ( empty( $args['url'] ) || ! in_array( parse_url( $args['url'], PHP_URL_HOST ), $allowed_host, true ) ) { + $status['success'] = false; + $status['message'] = __( 'Invalid download link', 'directorist' ); + + return $status; + } + + global $wp_filesystem; + + if ( $args['init_wp_filesystem'] ) { + + if ( ! function_exists( 'WP_Filesystem' ) ) { + include ABSPATH . 'wp-admin/includes/file.php'; + } + + WP_Filesystem(); + } + + $plugin_path = WP_CONTENT_DIR . '/plugins'; + $temp_dest = "{$plugin_path}/atbdp-temp-dir"; + $file_url = $args['url']; + $file_name = basename( $file_url ); + $tmp_file = download_url( $file_url ); + + if ( ! is_string( $tmp_file ) ) { + $status['success'] = false; + $status['tmp_file'] = $tmp_file; + $status['file_url'] = $file_url; + $status['message'] = 'Could not download the file'; + + return $status; + } + + // Make Temp Dir + if ( $wp_filesystem->exists( $temp_dest ) ) { + $wp_filesystem->delete( $temp_dest, true ); + } + + $wp_filesystem->mkdir( $temp_dest ); + + if ( ! file_exists( $temp_dest ) ) { + $status['success'] = false; + $status['message'] = __( 'Could not create temp directory', 'directorist' ); + + return $status; + } + + // Sets file temp destination. + $file_path = "{$temp_dest}/{$file_name}"; + + set_error_handler( + function ( $errno, $errstr, $errfile, $errline ) { + // error was suppressed with the @-operator + if ( 0 === error_reporting() ) { + return false; + } + + throw new ErrorException( $errstr, 0, $errno, $errfile, $errline ); + } + ); + + // Copies the file to the final destination and deletes temporary file. + try { + copy( $tmp_file, $file_path ); + } catch ( Exception $e ) { + $status['success'] = false; + $status['message'] = $e->getMessage(); + + return $status; + } + + @unlink( $tmp_file ); + unzip_file( $file_path, $temp_dest ); + + if ( "{$plugin_path}/" !== $file_path || $file_path !== $plugin_path ) { + @unlink( $file_path ); + } + + $extracted_file_dir = glob( "{$temp_dest}/*", GLOB_ONLYDIR ); + + foreach ( $extracted_file_dir as $dir_path ) { + $dir_name = basename( $dir_path ); + $dest_path = "{$plugin_path}/{$dir_name}"; + + // Delete Previous Files if Exists + if ( $wp_filesystem->exists( $dest_path ) ) { + $wp_filesystem->delete( $dest_path, true ); + } + } + + copy_dir( $temp_dest, $plugin_path ); + $wp_filesystem->delete( $temp_dest, true ); + + $status['success'] = true; + $status['message'] = __( 'The plugin has been downloaded successfully', 'directorist' ); + + return $status; +} \ No newline at end of file diff --git a/includes/modules/multi-directory-setup/class-multi-directory-manager.php b/includes/modules/multi-directory-setup/class-multi-directory-manager.php index 837023e665..c3e3d682a9 100644 --- a/includes/modules/multi-directory-setup/class-multi-directory-manager.php +++ b/includes/modules/multi-directory-setup/class-multi-directory-manager.php @@ -35,6 +35,7 @@ public function run() { add_action( 'wp_ajax_save_post_type_data', [ $this, 'save_post_type_data' ] ); add_action( 'wp_ajax_save_imported_post_type_data', [ $this, 'save_imported_post_type_data' ] ); add_action( 'wp_ajax_directorist_force_migrate', [ $this, 'handle_force_migration' ] ); + add_action( 'wp_ajax_directorist_directory_type_library', [ $this, 'directorist_directory_type_library' ] ); add_filter( 'directorist_builder_layouts', [ $this, 'conditional_layouts' ] ); } @@ -206,6 +207,37 @@ public function handle_force_migration() { wp_send_json( $this->run_force_migration() ); } + public function directorist_directory_type_library() { + + if ( ! directorist_verify_nonce() ) { + wp_send_json([ + 'status' => [ + 'success' => false, + 'message' => __( 'Something is wrong! Please refresh and retryyy.', 'directorist' ), + ], + ], 200); + } + + if ( ! current_user_can( 'install_plugins' ) || ! current_user_can( 'activate_plugins' ) ) { + wp_send_json([ + 'status' => [ + 'success' => false, + 'message' => __( 'You are not allowed to add/activate new plugin', 'directorist' ), + ], + ], 200); + } + + $installed = directorist_download_plugin( [ 'url' => 'https://downloads.wordpress.org/plugin/templatiq.1.0.0.zip' ] ); + $path = WP_PLUGIN_DIR . '/templatiq/templatiq.php'; + + if( ! is_plugin_active( $path ) ){ + activate_plugin( $path ); + } + + $installed['redirect'] = admin_url( 'admin.php?page=templatiq' ); + wp_send_json( $installed ); + } + // run_force_migration public function run_force_migration() { $general_directory = term_exists( 'General', 'atbdp_listing_types' ); diff --git a/views/admin-templates/post-types-manager/all-listing-types.php b/views/admin-templates/post-types-manager/all-listing-types.php index 9489b0b654..558c71066d 100644 --- a/views/admin-templates/post-types-manager/all-listing-types.php +++ b/views/admin-templates/post-types-manager/all-listing-types.php @@ -50,7 +50,7 @@ - + - +
- + - + +