Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action('acf/save_post', 'res_add_font_to_avia', 5);
- function res_add_font_to_avia($post_id)
- {
- if (isset($_POST['acf']['field_62f356532c89d'])) {
- $font_uploader = new avia_font_manager();
- $old_font = get_field('theme_options_font', 'option');
- if (is_array($old_font)) {
- $old_font_ID = (int)$old_font['ID'];
- } else {
- $old_font_ID = (int)$old_font;
- }
- $new_font_ID = (int)$_POST['acf']['field_62f356532c89d'];
- if ($new_font_ID) {
- // check if old font and new font are equal
- if ($new_font_ID === $old_font_ID) return;
- // add new font
- $font_name = res_upload_font($new_font_ID, $font_uploader);
- // Update font title to have reference for deletion
- $font = array(
- 'ID' => $new_font_ID,
- 'post_title' => $font_name,
- );
- wp_update_post($font);
- } else {
- // Delete font folder and font
- $font_name = get_the_title($old_font_ID);
- wp_delete_attachment($old_font_ID);
- if ($font_name) {
- $new_name = trailingslashit($font_uploader->paths['fontdir']) . $font_name;
- $font_uploader->delete_folder($new_name);
- $fonts = get_option('avia_builder_fonts');
- if (isset($fonts[$font_name])) {
- unset($fonts[$font_name]);
- update_option('avia_builder_fonts', $fonts);
- }
- }
- }
- }
- }
- function res_upload_font($attachment_id, $font_uploader)
- {
- if (!$attachment_id) return '';
- $path = realpath(get_attached_file($attachment_id));
- $unzipped = $font_uploader->zip_flatten($path, array('\.eot', '\.svg', '\.ttf', '\.woff', '\.woff2', '\.json'));
- if ($unzipped) {
- $font_uploader->create_config();
- }
- if ($font_uploader->font_name == 'unknown') {
- $font_uploader->delete_folder($font_uploader->paths['tempdir']);
- exit('Was not able to retrieve the Font name from your Uploaded Folder');
- }
- return $font_uploader->font_name;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement