DragDropBuilder
5 years ago
EmailSettings
5 years ago
ShortcodeBuilder
5 years ago
AbstractSettingsPage.php
5 years ago
AddNewForm.php
5 years ago
AdminFooter.php
5 years ago
ExtensionsSettingsPage.php
5 years ago
FormList.php
5 years ago
Forms.php
5 years ago
GeneralSettings.php
5 years ago
IDUserColumn.php
5 years ago
MemberDirectories.php
5 years ago
MembersDirectoryList.php
5 years ago
ToolsSettingsPage.php
5 years ago
index.php
5 years ago
FormList.php
425 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Admin\SettingsPages; |
| 4 | |
| 5 | use ProfilePress\Core\Base; |
| 6 | use ProfilePress\Core\Classes\ExtensionManager; |
| 7 | use ProfilePress\Core\Classes\FormRepository as FR; |
| 8 | use ProfilePress\Core\Themes\Shortcode\ThemesRepository as ShortcodeThemesRepository; |
| 9 | use ProfilePress\Core\Themes\DragDrop\ThemesRepository as DragDropThemesRepository; |
| 10 | |
| 11 | if ( ! class_exists('WP_List_Table')) { |
| 12 | require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php'); |
| 13 | } |
| 14 | |
| 15 | class FormList extends \WP_List_Table |
| 16 | { |
| 17 | private $table; |
| 18 | |
| 19 | /** @var \wpdb */ |
| 20 | private $wpdb; |
| 21 | |
| 22 | public function __construct($wpdb) |
| 23 | { |
| 24 | $this->wpdb = $wpdb; |
| 25 | $this->table = Base::form_db_table(); |
| 26 | parent::__construct(array( |
| 27 | 'singular' => esc_html__('form', 'wp-user-avatar'), //singular name of the listed records |
| 28 | 'plural' => esc_html__('forms', 'wp-user-avatar'), //plural name of the listed records |
| 29 | 'ajax' => false //does this table support ajax? |
| 30 | ) |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | public function get_forms($per_page, $current_page = 1, $form_type = '') |
| 35 | { |
| 36 | $per_page = absint($per_page); |
| 37 | $current_page = absint($current_page); |
| 38 | $form_type = ! empty($form_type) ? sanitize_text_field($form_type) : FR::LOGIN_TYPE; |
| 39 | |
| 40 | $offset = ($current_page - 1) * $per_page; |
| 41 | $sql = "SELECT * FROM $this->table"; |
| 42 | $args = []; |
| 43 | |
| 44 | $sql .= " WHERE form_type = %s"; |
| 45 | $args[] = $form_type; |
| 46 | |
| 47 | $sql .= " ORDER BY date DESC"; |
| 48 | |
| 49 | $args[] = $per_page; |
| 50 | |
| 51 | $sql .= " LIMIT %d"; |
| 52 | if ($current_page > 1) { |
| 53 | $args[] = $offset; |
| 54 | $sql .= " OFFSET %d"; |
| 55 | } |
| 56 | |
| 57 | $result = $this->wpdb->get_results( |
| 58 | $this->wpdb->prepare($sql, $args), |
| 59 | 'ARRAY_A' |
| 60 | ); |
| 61 | |
| 62 | if ( ! ExtensionManager::is_premium()) { |
| 63 | $shortcode_premium_theme_classes = wp_list_pluck(ShortcodeThemesRepository::premiumThemes(), 'theme_class'); |
| 64 | $dnd_premium_theme_classes = wp_list_pluck(DragDropThemesRepository::premiumThemes(), 'theme_class'); |
| 65 | |
| 66 | $filtered = []; |
| 67 | foreach ($result as $form) { |
| 68 | |
| 69 | $builder_type = $form['builder_type']; |
| 70 | $form_type = $form['form_type']; |
| 71 | $form_id = $form['form_id']; |
| 72 | |
| 73 | $form_class = FR::get_form_class($form_id, $form_type); |
| 74 | |
| 75 | if ($builder_type == FR::SHORTCODE_BUILDER_TYPE && |
| 76 | ! in_array($form_class, $shortcode_premium_theme_classes) |
| 77 | ) { |
| 78 | $filtered[] = $form; |
| 79 | } |
| 80 | |
| 81 | if ($builder_type == FR::DRAG_DROP_BUILDER_TYPE && |
| 82 | ! in_array($form_class, $dnd_premium_theme_classes) |
| 83 | ) { |
| 84 | $filtered[] = $form; |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | $result = $filtered; |
| 90 | } |
| 91 | |
| 92 | return $result; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns the count of records in the database. |
| 97 | * |
| 98 | * @param string $form_type |
| 99 | * |
| 100 | * @return null|string |
| 101 | */ |
| 102 | public function record_count($form_type = '') |
| 103 | { |
| 104 | $form_type = ! empty($form_type) ? sanitize_text_field($form_type) : FR::LOGIN_TYPE; |
| 105 | |
| 106 | $sql = "SELECT COUNT(*) FROM $this->table"; |
| 107 | if ( ! empty($form_type)) { |
| 108 | $form_type = esc_sql($form_type); |
| 109 | $sql .= " WHERE form_type = '$form_type'"; |
| 110 | } |
| 111 | |
| 112 | return $this->wpdb->get_var($sql); |
| 113 | } |
| 114 | |
| 115 | public static function customize_url($form_id, $form_type, $builder_type = FR::SHORTCODE_BUILDER_TYPE) |
| 116 | { |
| 117 | $slug = $form_type == FR::MEMBERS_DIRECTORY_TYPE ? PPRESS_MEMBER_DIRECTORIES_SLUG : PPRESS_FORMS_SETTINGS_SLUG; |
| 118 | |
| 119 | $url = admin_url( |
| 120 | sprintf( |
| 121 | 'admin.php?page=%s&view=edit-shortcode-%s&id=%d', |
| 122 | $slug, |
| 123 | $form_type, |
| 124 | $form_id |
| 125 | ) |
| 126 | ); |
| 127 | |
| 128 | if ($builder_type == FR::DRAG_DROP_BUILDER_TYPE) { |
| 129 | |
| 130 | $url = admin_url( |
| 131 | sprintf( |
| 132 | 'admin.php?page=%s&view=drag-drop-builder&form-type=%s&id=%d', |
| 133 | $slug, |
| 134 | $form_type, |
| 135 | $form_id |
| 136 | ) |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | return $url; |
| 141 | } |
| 142 | |
| 143 | public static function delete_url($form_id, $form_type) |
| 144 | { |
| 145 | return admin_url( |
| 146 | sprintf( |
| 147 | 'admin.php?page=pp-forms&action=delete&form_type=%s&id=%d&_wpnonce=%s', |
| 148 | $form_type, |
| 149 | $form_id, |
| 150 | ppress_create_nonce() |
| 151 | ) |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | public static function clone_url($form_id, $form_type) |
| 156 | { |
| 157 | return admin_url( |
| 158 | sprintf( |
| 159 | 'admin.php?page=pp-forms&action=clone&form_type=%s&id=%d&_wpnonce=%s', |
| 160 | $form_type, |
| 161 | $form_id, |
| 162 | ppress_create_nonce() |
| 163 | ) |
| 164 | ); |
| 165 | } |
| 166 | |
| 167 | public static function preview_url($form_id, $form_type) |
| 168 | { |
| 169 | return add_query_arg(['pp_preview_form' => $form_id, 'type' => $form_type], home_url()); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Text displayed when no email optin form is available |
| 174 | */ |
| 175 | public function no_items() |
| 176 | { |
| 177 | printf( |
| 178 | esc_html__('No form is currently available. %sConsider creating one%s', 'wp-user-avatar'), |
| 179 | '<a href="' . add_query_arg('view', 'add-new-form', PPRESS_FORMS_SETTINGS_PAGE) . '">', |
| 180 | '</a>' |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Associative array of columns |
| 186 | * |
| 187 | * @return array |
| 188 | */ |
| 189 | public function get_columns() |
| 190 | { |
| 191 | $columns = array( |
| 192 | 'cb' => '<input type="checkbox" />', |
| 193 | 'title' => esc_html__('Title', 'wp-user-avatar'), |
| 194 | 'shortcode' => esc_html__('Shortcode', 'wp-user-avatar'), |
| 195 | 'builder' => esc_html__('Builder Type', 'wp-user-avatar'), |
| 196 | 'date' => esc_html__('Date', 'wp-user-avatar') |
| 197 | ); |
| 198 | |
| 199 | return $columns; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Render the bulk edit checkbox |
| 204 | * |
| 205 | * @param array $item |
| 206 | * |
| 207 | * @return string |
| 208 | */ |
| 209 | function column_cb($item) |
| 210 | { |
| 211 | return sprintf( |
| 212 | '<input type="checkbox" name="form_id[]" value="%s" />', $item['form_id'] |
| 213 | ); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Method for Title column |
| 218 | * |
| 219 | * @param array $item an array of DB data |
| 220 | * |
| 221 | * @return string |
| 222 | */ |
| 223 | public function column_title($item) |
| 224 | { |
| 225 | $form_id = absint($item['form_id']); |
| 226 | $form_type = sanitize_text_field($item['form_type']); |
| 227 | $builder_type = sanitize_text_field($item['builder_type']); |
| 228 | $customize_url = self::customize_url($form_id, $form_type, $builder_type); |
| 229 | $delete_url = self::delete_url($form_id, $form_type); |
| 230 | $clone_url = self::clone_url($form_id, $form_type); |
| 231 | $preview_url = self::preview_url($form_id, $form_type); |
| 232 | |
| 233 | $actions = array( |
| 234 | 'delete' => sprintf("<a class='pp-form-delete' href='%s'>%s</a>", $delete_url, esc_attr__('Delete', 'wp-user-avatar')), |
| 235 | 'clone' => sprintf("<a href='%s'>%s</a>", $clone_url, esc_attr__('Duplicate', 'wp-user-avatar')), |
| 236 | // using form prefix cos there is a preview admin class that floats element to the right |
| 237 | 'form-preview' => sprintf("<a target='_blank' href='%s'>%s</a>", $preview_url, esc_attr__('Preview', 'wp-user-avatar')) |
| 238 | ); |
| 239 | |
| 240 | $name = '<strong><a href="' . $customize_url . '">' . $item['name'] . '</a></strong>'; |
| 241 | |
| 242 | |
| 243 | return $name . $this->row_actions($actions); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Method for Shortcode column |
| 248 | * |
| 249 | * @param array $item an array of DB data |
| 250 | * |
| 251 | * @return string |
| 252 | */ |
| 253 | public function column_shortcode($item) |
| 254 | { |
| 255 | $form_type = sanitize_text_field($item['form_type']); |
| 256 | |
| 257 | $shortcode = sprintf('[profilepress-%s id="%s"]', $form_type, absint($item['form_id'])); |
| 258 | |
| 259 | $output = '<input type="text" onfocus="this.select();" readonly="readonly" value="' . esc_attr($shortcode) . '" class="shortcode-in-list-table" />'; |
| 260 | |
| 261 | return $output; |
| 262 | } |
| 263 | |
| 264 | public function column_builder($item) |
| 265 | { |
| 266 | $builder_type = esc_html__('Shortcode', 'wp-user-avatar'); |
| 267 | if ($item['builder_type'] == FR::DRAG_DROP_BUILDER_TYPE) { |
| 268 | $builder_type = esc_html__('Drag & Drop', 'wp-user-avatar'); |
| 269 | } |
| 270 | |
| 271 | return $builder_type; |
| 272 | } |
| 273 | |
| 274 | public function column_default($item, $column_name) |
| 275 | { |
| 276 | switch ($column_name) { |
| 277 | case 'date' : |
| 278 | $value = mysql2date('F j, Y', $item['date']); |
| 279 | break; |
| 280 | default: |
| 281 | $value = $item[$column_name]; |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | return apply_filters('ppress_forms_table_column', $value, $item, $column_name); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Columns to make sortable. |
| 290 | * |
| 291 | * @return array |
| 292 | */ |
| 293 | public function get_sortable_columns() |
| 294 | { |
| 295 | $sortable_columns = array( |
| 296 | 'name' => array('name', true), |
| 297 | ); |
| 298 | |
| 299 | return $sortable_columns; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Returns an associative array containing the bulk action |
| 304 | * |
| 305 | * @return array |
| 306 | */ |
| 307 | public function get_bulk_actions() |
| 308 | { |
| 309 | $actions = array( |
| 310 | 'bulk-delete' => esc_html__('Delete', 'wp-user-avatar'), |
| 311 | ); |
| 312 | |
| 313 | return $actions; |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Handles data query and filter, sorting, and pagination. |
| 318 | * |
| 319 | * @param string $form_type |
| 320 | */ |
| 321 | public function prepare_items($form_type = '') |
| 322 | { |
| 323 | if (isset($_GET['page']) && $_GET['page'] == PPRESS_FORMS_SETTINGS_SLUG && ! empty($_GET['form-type'])) { |
| 324 | $form_type = sanitize_text_field($_GET['form-type']); |
| 325 | } |
| 326 | |
| 327 | $this->_column_headers = $this->get_column_info(); |
| 328 | /** Process bulk action */ |
| 329 | $this->process_actions(); |
| 330 | $per_page = $this->get_items_per_page('forms_per_page', 10); |
| 331 | $current_page = $this->get_pagenum(); |
| 332 | $total_items = self::record_count($form_type); |
| 333 | $this->set_pagination_args([ |
| 334 | 'total_items' => $total_items, //WE have to calculate the total number of items |
| 335 | 'per_page' => $per_page //WE have to determine how many items to show on a page |
| 336 | ]); |
| 337 | |
| 338 | $this->items = $this->get_forms($per_page, $current_page, $form_type); |
| 339 | } |
| 340 | |
| 341 | public function process_actions() |
| 342 | { |
| 343 | // Bail if user is not an admin or without admin privileges. |
| 344 | if ( ! current_user_can('manage_options')) return; |
| 345 | |
| 346 | if ('delete' === $this->current_action()) { |
| 347 | |
| 348 | if ( ! ppress_verify_nonce()) wp_nonce_ays(ppress_nonce_action_string()); |
| 349 | |
| 350 | $form_id = absint($_GET['id']); |
| 351 | $form_type = sanitize_text_field($_GET['form_type']); |
| 352 | |
| 353 | FR::delete_form($form_id, $form_type); |
| 354 | |
| 355 | $url = PPRESS_FORMS_SETTINGS_PAGE; |
| 356 | |
| 357 | if (isset($_GET['form_type'])) { |
| 358 | |
| 359 | $url = add_query_arg('form-type', sanitize_text_field($_GET['form_type']), $url); |
| 360 | |
| 361 | if (FR::MEMBERS_DIRECTORY_TYPE == $_GET['form_type']) { |
| 362 | $url = PPRESS_MEMBER_DIRECTORIES_SETTINGS_PAGE; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | |
| 367 | wp_safe_redirect($url); |
| 368 | exit; |
| 369 | |
| 370 | } |
| 371 | |
| 372 | if ('clone' === $this->current_action()) { |
| 373 | |
| 374 | if ( ! ppress_verify_nonce()) wp_nonce_ays(ppress_nonce_action_string()); |
| 375 | |
| 376 | $form_id = absint($_GET['id']); |
| 377 | $form_type = sanitize_text_field($_GET['form_type']); |
| 378 | |
| 379 | FR::clone_form($form_id, $form_type); |
| 380 | |
| 381 | $url = PPRESS_FORMS_SETTINGS_PAGE; |
| 382 | |
| 383 | if (isset($_GET['form_type'])) { |
| 384 | $url = add_query_arg('form-type', sanitize_text_field($_GET['form_type']), $url); |
| 385 | |
| 386 | if (FR::MEMBERS_DIRECTORY_TYPE == $_GET['form_type']) { |
| 387 | $url = PPRESS_MEMBER_DIRECTORIES_SETTINGS_PAGE; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | wp_safe_redirect($url); |
| 392 | exit; |
| 393 | } |
| 394 | |
| 395 | // Detect when a bulk action is being triggered... |
| 396 | if ('bulk-delete' == $this->current_action()) { |
| 397 | check_admin_referer('bulk-forms'); |
| 398 | $form_ids = $_POST['form_id']; |
| 399 | |
| 400 | foreach ($form_ids as $form_id) { |
| 401 | $form_id = absint($form_id); |
| 402 | $form_type = ! empty($_GET['form-type']) ? sanitize_text_field($_GET['form-type']) : FR::LOGIN_TYPE; |
| 403 | if (isset($_GET['page']) && $_GET['page'] == PPRESS_MEMBER_DIRECTORIES_SLUG) { |
| 404 | $form_type = FR::MEMBERS_DIRECTORY_TYPE; |
| 405 | } |
| 406 | |
| 407 | FR::delete_form($form_id, $form_type); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * @return FormList |
| 414 | */ |
| 415 | public static function get_instance() |
| 416 | { |
| 417 | static $instance = null; |
| 418 | |
| 419 | if (is_null($instance)) { |
| 420 | $instance = new static($GLOBALS['wpdb']); |
| 421 | } |
| 422 | |
| 423 | return $instance; |
| 424 | } |
| 425 | } |