FormHelper.php
172 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Core function for the plugin. |
| 5 | * |
| 6 | * @package EverestForms\Helpers |
| 7 | * @since 3.2.0 |
| 8 | */ |
| 9 | |
| 10 | namespace EverestForms\Helpers; |
| 11 | |
| 12 | /** |
| 13 | * FormHelper. |
| 14 | * |
| 15 | * @since 3.2.0 |
| 16 | */ |
| 17 | class FormHelper |
| 18 | { |
| 19 | /** |
| 20 | * Get all the form category list. |
| 21 | * |
| 22 | * @since 3.2.0 |
| 23 | * @param string $key The key. |
| 24 | */ |
| 25 | public static function get_all_form_tags($key = 'slug') |
| 26 | { |
| 27 | $form_tags = get_terms( |
| 28 | array( |
| 29 | 'taxonomy' => \EVF_Post_Types::TAGS_TAXONOMY, |
| 30 | 'hide_empty' => false, |
| 31 | ) |
| 32 | ); |
| 33 | |
| 34 | $form_tags = is_wp_error($form_tags) ? array() : (array) $form_tags; |
| 35 | $tags_options = wp_list_pluck($form_tags, 'name', $key); |
| 36 | |
| 37 | return $tags_options; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Particular form tags. |
| 42 | * |
| 43 | * @since 3.2.0 |
| 44 | * @param [type] $form_id The form id. |
| 45 | * @param string $key The option key type. |
| 46 | */ |
| 47 | public static function get_form_tags($form_id, $key = 'term_id') |
| 48 | { |
| 49 | $form_tags = wp_get_post_terms( |
| 50 | $form_id, |
| 51 | \EVF_Post_Types::TAGS_TAXONOMY, |
| 52 | true |
| 53 | ); |
| 54 | |
| 55 | $form_tags = is_wp_error($form_tags) ? array() : (array) $form_tags; |
| 56 | $tags_options = wp_list_pluck($form_tags, 'name', $key); |
| 57 | |
| 58 | return $tags_options; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get all the form tags based on the forms. |
| 63 | * |
| 64 | * @param [type] $form_ids The form list. |
| 65 | */ |
| 66 | public static function get_selected_forms_tags($form_ids) |
| 67 | { |
| 68 | $all_tags = array(); |
| 69 | foreach ($form_ids as $form_id) { |
| 70 | |
| 71 | $tags = self::get_form_tags($form_id); |
| 72 | $all_tags = $all_tags + $tags; |
| 73 | } |
| 74 | |
| 75 | return $all_tags; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Save CleanTalk settings. |
| 80 | * |
| 81 | * @since 3.3.0 |
| 82 | * |
| 83 | * @param string $access_key The access key. |
| 84 | * @return bool |
| 85 | */ |
| 86 | public static function evf_save_clean_talk_settings($access_key) |
| 87 | { |
| 88 | $clean_talk_request = array( |
| 89 | 'method_name' => 'notice_paid_till', |
| 90 | 'auth_key' => sanitize_text_field($access_key), |
| 91 | ); |
| 92 | |
| 93 | $response = wp_remote_post( |
| 94 | 'https://api.cleantalk.org/', |
| 95 | array( |
| 96 | 'body' => \http_build_query($clean_talk_request, true), |
| 97 | 'headers' => array( |
| 98 | 'Content-Type' => 'application/x-www-form-urlencoded', |
| 99 | ), |
| 100 | ) |
| 101 | ); |
| 102 | $response = json_decode(wp_remote_retrieve_body($response)); |
| 103 | if ($response->data->moderate == 1 && $response->data->valid == 1 && $response->data->product_id == 1) { |
| 104 | update_option('everest_forms_recaptcha_cleantalk_access_key', sanitize_text_field($access_key)); |
| 105 | return true; |
| 106 | } else { |
| 107 | return false; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Check the file type. |
| 113 | * |
| 114 | * @param [type] $ext The extension. |
| 115 | */ |
| 116 | public static function evf_file_upload_check_file_types($ext) |
| 117 | { |
| 118 | $supportedFileTypes = array( |
| 119 | 'pdf', |
| 120 | 'doc', |
| 121 | 'xls', |
| 122 | 'ppt', |
| 123 | 'mp3', |
| 124 | 'mp4', |
| 125 | 'zip', |
| 126 | ); |
| 127 | $newMsFileTypes = array('docx', 'xlsx', 'pptx'); |
| 128 | $imageFileTypes = array('jpg', 'jpeg', 'png', 'gif'); |
| 129 | |
| 130 | $fileIcon = null; |
| 131 | |
| 132 | if (in_array($ext, $supportedFileTypes)) { |
| 133 | $fileIcon = $ext; |
| 134 | } elseif (in_array($ext, $newMsFileTypes)) { |
| 135 | $fileIcon = substr($ext, 0, -1); |
| 136 | } elseif (! in_array($ext, $imageFileTypes)) { |
| 137 | $fileIcon = 'default'; |
| 138 | } |
| 139 | |
| 140 | return $fileIcon; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Remove the file. |
| 145 | * |
| 146 | * @param [type] $file_url The file url. |
| 147 | */ |
| 148 | public static function remove_file($file_url) |
| 149 | { |
| 150 | $uploads = wp_upload_dir(); |
| 151 | $base_dir = realpath($uploads['basedir']); |
| 152 | $path_from_url = wp_parse_url($file_url, PHP_URL_PATH); |
| 153 | |
| 154 | $uploaded_file = $uploads['basedir'] . preg_replace( |
| 155 | '/.*uploads/', |
| 156 | '/everest_forms_uploads', |
| 157 | $path_from_url |
| 158 | ); |
| 159 | |
| 160 | $normalized_path = wp_normalize_path($uploaded_file); |
| 161 | $resolved_path = realpath($normalized_path); |
| 162 | // Validate path is within allowed directory |
| 163 | if ($resolved_path && strpos($resolved_path, $base_dir) === 0) { |
| 164 | if (is_file($resolved_path)) { |
| 165 | wp_delete_file($resolved_path); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return; |
| 170 | } |
| 171 | } |
| 172 |