assets.php
5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* Assets Functions
*
* @package BadgeOS
* @subpackage Assets
* @author LearningTimes, LLC
* @license http://www.gnu.org/licenses/agpl.txt GNU AGPL v3.0
* @link https://credly.com
*/
/**
* Lists all the available assets packs.
*
* @return void
*/
function badgeos_assets_page() {
$assets = badgeos_assets_data();
echo '<div class="badgeos_assets">';
echo '<h1>' . esc_html__( 'Assets', 'badgeos' ) . '</h1>';
$root_url = badgeos_get_directory_url();
if ( is_object( $assets ) && isset( $assets ) ) {
foreach ( $assets as $key => $asset ) {
if ( 'Yes' === $asset->active ) {
?>
<div class="badgeos_item badgeos_item_<?php echo esc_attr( $key ); ?>">
<div class="badgeos-assets-image">
<img src="<?php echo esc_url( $asset->image ); ?>" alt="<?php echo esc_attr( $asset->title ); ?>" style="width:100%">
</div>
<h2><?php echo esc_html( $asset->title ); ?></h2>
<div class="badgeos-item-description"><?php echo esc_html( $asset->description ); ?></div>
<div class="badgeos-assets-message-divs">
<div class="badgeos_download_asset_success_message" style="display:none"><?php esc_html_e( 'Thank you for downloading the pack! The icons are in your media files and you can use them for your badges, points, or ranks', 'badgeos' ); ?></div>
<div class="badgeos_download_asset_failed_message" style="display:none"></div>
</div>
<p class="badgeos-item-button">
<input type="hidden" name="badgeos_assets_id" class="badgeos_assets_id" id="badgeos_assets_id" value="<?php echo esc_attr( $key ); ?>" />
<button class="btn_badgeos_download_assets" data-ajax_url="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>">
<?php esc_html_e( 'Download', 'badgeos' ); ?>
<img id="btn_badgeos_download_assets_loader" style="background-color:#000000; visibility:hidden;" src="<?php echo esc_url( $root_url . '/images/ajax-loader.gif' ); ?>" />
</button>
</p>
</div>
<?php
}
}
}
echo '</div>';
}
/**
* Ajax download handler script.
*
* @return void
*/
function badgeos_download_and_configure_asset() {
set_time_limit( 0 );
$assets_id = isset( $_POST['assets_id'] ) ? sanitize_text_field( wp_unslash( $_POST['assets_id'] ) ) : '';
$assets = badgeos_assets_data();
$assets = (array) $assets;
if ( ! empty( $assets_id ) ) {
if ( $assets[ $assets_id ] ) {
// $downloaded_assets_id = badgeos_utilities::get_option( 'badgeos_restapi_'.$assets_id );.
if ( 'downloaded' !== trim( $downloaded_assets_id ) ) {
if ( ! empty( $assets[ $assets_id ]->asset_url ) ) {
// If the function it's not available, require it.
if ( ! function_exists( 'download_url' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
// Now you can use it!.
$tmp_file = download_url( $assets[ $assets_id ]->asset_url );
$filename = basename( $assets[ $assets_id ]->asset_url );
// Sets file final destination.
$filepath = ABSPATH . 'wp-content/uploads/' . $filename;
// Copies the file to the final destination and deletes temporary file.
copy( $tmp_file, $filepath );
@unlink( $tmp_file );
WP_Filesystem();
$destination = wp_upload_dir();
$destination_path = $destination['path'];
if ( $unzipfile = unzip_file( $filepath, $destination_path ) ) {
$image_folder = trailingslashit( $destination_path ) . $assets[ $assets_id ]->asset_folder;
$files = list_files( $image_folder );
foreach ( $files as $file ) {
$image_name = basename( $file );
$wp_filetype = wp_check_filetype( $image_name, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $image_name ),
'post_content' => '',
'post_status' => 'inherit',
);
$attach_id = wp_insert_attachment( $attachment, $file );
require_once ABSPATH . 'wp-admin/includes/image.php';
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
}
// badgeos_utilities::update_option( 'badgeos_restapi_'.$assets_id, 'downloaded' );.
echo esc_html__( 'done', 'badgeos' );
} else {
echo esc_html__( 'Invalid zip archive provided.', 'badgeos' );
}
} else {
echo esc_html__( 'Please, provide an asset url.', 'badgeos' );
}
} else {
echo esc_html__( 'Asset is already downloaded on your media gallery.', 'badgeos' );
}
} else {
echo esc_html__( "Sorry, we're unable to reach the server right now please try later.", 'badgeos' );
}
} else {
echo esc_html__( "Sorry, we're unable to reach the server right now please try later.", 'badgeos' );
}
exit;
}
add_action( 'wp_ajax_badgeos_download_asset', 'badgeos_download_and_configure_asset' );
/**
* Ajax download handler script.
*/
function badgeos_assets_data() {
$request = wp_remote_get( 'https://badgeos.org/badgeos-assets-api.php' );
if ( is_wp_error( $request ) ) {
return false;
}
$body = wp_remote_retrieve_body( $request );
return json_decode( $body );
}