Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement/templetiq #1810

Merged
merged 4 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions assets/js/admin-builder-archive.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 59 additions & 1 deletion assets/src/js/admin/multi-directory-archive.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,62 @@
// Scrips
import './components/import-directory-modal';
import './components/delete-directory-modal';
import './components/directory-migration-modal';
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( "<span class='directorist_template_notice'>Installing Templetiq, Please wait..</span>" );

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 = `
<div class="cptm-section-alert-content">
<div class="cptm-section-alert-icon cptm-alert-error">
<span class="fa fa-times"></span>
</div>

<div class="cptm-section-alert-message">${msg}</div>
</div>
`;

$( '.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 );
});
});
});
122 changes: 1 addition & 121 deletions includes/classes/class-setup-wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down Expand Up @@ -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' );
Expand Down Expand Up @@ -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';

Expand Down
110 changes: 110 additions & 0 deletions includes/helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading