ElementGenerator
1 year ago
Ajax.php
1 year ago
Backend.php
1 year ago
Editor.php
1 year ago
Frontend.php
1 year ago
Helper.php
1 year ago
Iframe.php
1 year ago
Pages.php
1 year ago
Helper.php
189 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Preview script for html markup generator |
| 5 | * |
| 6 | * @package tutor-droip-elements |
| 7 | */ |
| 8 | |
| 9 | namespace TutorLMSDroip; |
| 10 | |
| 11 | use Droip\Ajax\ExportImport; |
| 12 | |
| 13 | if (! defined('ABSPATH')) { |
| 14 | exit; // Exit if accessed directly. |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Class Helper |
| 19 | * This class is used to define all helper functions. |
| 20 | */ |
| 21 | class Helper |
| 22 | { |
| 23 | |
| 24 | /** |
| 25 | * Function to activate droip elements plugin |
| 26 | */ |
| 27 | public static function t_d_e_activate() |
| 28 | { |
| 29 | self::get_course_template_posts(); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * This function will verify nonce |
| 34 | * ACT like API calls auth middleware |
| 35 | * |
| 36 | * @param string $action ajax action name. |
| 37 | * |
| 38 | * @return void |
| 39 | */ |
| 40 | public static function verify_nonce($action = -1) |
| 41 | { |
| 42 | $nonce = sanitize_text_field(isset($_SERVER['HTTP_X_WP_NONCE']) ? wp_unslash($_SERVER['HTTP_X_WP_NONCE']) : null); |
| 43 | if (! wp_verify_nonce($nonce, $action)) { |
| 44 | wp_send_json_error('Not authorized'); |
| 45 | exit; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Get course template post |
| 51 | * |
| 52 | * @return mixed |
| 53 | */ |
| 54 | public static function get_course_template_posts() |
| 55 | { |
| 56 | $all_templates = get_posts( |
| 57 | array( |
| 58 | 'post_type' => 'droip_template', |
| 59 | 'posts_per_page' => -1, |
| 60 | 'post_status' => ['draft', 'publish'], |
| 61 | ) |
| 62 | ); |
| 63 | $data = []; |
| 64 | foreach ($all_templates as $key => $template) { |
| 65 | $conditions = get_post_meta($template->ID, 'droip_template_conditions', true); |
| 66 | if ($conditions) { |
| 67 | foreach ($conditions as $key2 => $condition) { |
| 68 | if ($condition['category'] === 'courses') { |
| 69 | $data[] = $template; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (count($data) === 0) { |
| 76 | //create a course template |
| 77 | $post_id = wp_insert_post( |
| 78 | array( |
| 79 | 'post_title' => 'Course Details', |
| 80 | 'post_name' => 'Course Details', |
| 81 | 'post_type' => 'droip_template' |
| 82 | ) |
| 83 | ); |
| 84 | $conditions = array( |
| 85 | array( |
| 86 | 'category' => 'courses', |
| 87 | 'taxonomy' => '*', |
| 88 | 'visibility' => 'show' |
| 89 | ) |
| 90 | ); |
| 91 | update_post_meta($post_id, 'droip_template_conditions', $conditions); |
| 92 | |
| 93 | if (class_exists('Droip\Ajax\ExportImport')) { |
| 94 | $template_path = TDE_ROOT_PATH . '/assets/course-details.zip'; |
| 95 | ExportImport::process_droip_template_zip($template_path, false, $post_id); |
| 96 | } |
| 97 | |
| 98 | $data[] = get_post($post_id); |
| 99 | } |
| 100 | return $data; |
| 101 | } |
| 102 | |
| 103 | public static function upload_layout_pack($obj) |
| 104 | { |
| 105 | try { |
| 106 | foreach ($obj['pages'] as $key => $page) { |
| 107 | $zip_path = self::download_zip_from_remote($page['src'], $page['ID'] . '.zip'); |
| 108 | |
| 109 | $post_id = self::get_post_id_for_template_import($obj['ID'], $page); |
| 110 | |
| 111 | if (isset($page['conditions'])) { |
| 112 | update_post_meta($post_id, 'droip_template_conditions', $page['conditions']); |
| 113 | } |
| 114 | if (class_exists('Droip\Ajax\ExportImport')) { |
| 115 | ExportImport::process_droip_template_zip($zip_path, false, $post_id); |
| 116 | unlink($zip_path); |
| 117 | } |
| 118 | } |
| 119 | } catch (\Throwable $th) { |
| 120 | return false; |
| 121 | } |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | private static function get_post_id_for_template_import($parent_id, $page_info) |
| 126 | { |
| 127 | global $wpdb; |
| 128 | $meta_key = 'droip_template_imported_' . $parent_id . '_' . $page_info['ID']; |
| 129 | $imported_flag = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}postmeta WHERE meta_key = '" . $meta_key . "'", OBJECT); |
| 130 | $post_id = null; |
| 131 | $post_status = null; |
| 132 | foreach ($imported_flag as $key => $p_meta) { |
| 133 | $post = get_post($p_meta->post_id); |
| 134 | if ($post && $post->post_type === $page_info['post_type']) { |
| 135 | $post_id = $p_meta->post_id; |
| 136 | $post_status = $post->post_status; |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if (! $post_id || 'trash' === $post_status) { |
| 142 | $post_id = wp_insert_post( |
| 143 | array( |
| 144 | 'post_title' => $page_info['title'], |
| 145 | 'post_name' => $page_info['title'], |
| 146 | 'post_type' => $page_info['post_type'], |
| 147 | 'post_status' => 'publish', |
| 148 | ) |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | update_post_meta($post_id, $meta_key, true); |
| 153 | update_post_meta($post_id, '_wp_page_template', DROIP_FULL_CANVAS_TEMPLATE_PATH); |
| 154 | update_post_meta($post_id, 'droip_include_wp_header', 'true'); |
| 155 | update_post_meta($post_id, 'droip_include_wp_footer', 'true'); |
| 156 | if ($page_info['ID'] === 'home') { |
| 157 | update_option('page_on_front', $post_id); |
| 158 | update_option('show_on_front', 'page'); |
| 159 | } |
| 160 | return $post_id; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * [download_zip description] |
| 165 | * |
| 166 | * @param [type] $remote_ile [$remote_ile description] |
| 167 | * @param [type] $new_name [$new_name description] |
| 168 | * |
| 169 | * @return [type] [return description] |
| 170 | */ |
| 171 | private static function download_zip_from_remote($remote_ile, $new_name) |
| 172 | { |
| 173 | try { |
| 174 | // Local path to save the downloaded file. |
| 175 | $local_file = wp_upload_dir()['basedir'] . '/' . $new_name; |
| 176 | // Download the file from the remote server. |
| 177 | $file_contents = file_get_contents($remote_ile); |
| 178 | // Save the file locally. |
| 179 | if ($file_contents !== false) { |
| 180 | file_put_contents($local_file, $file_contents); |
| 181 | return $local_file; |
| 182 | } |
| 183 | } catch (\Throwable $th) { |
| 184 | // throw $th; |
| 185 | } |
| 186 | return false; |
| 187 | } |
| 188 | } |
| 189 |