wp-optimize
/
vendor
/
team-updraft
/
common-libs
/
src
/
updraft-tasks
/
class-updraft-task-options.php
class-updraft-task-options.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.22, at vendor/team-updraft/common-libs/src/updraft-tasks/class-updraft-task-options.php
| 1 | <?php |
| 2 | /** |
| 3 | * The options framework for tasks |
| 4 | */ |
| 5 | |
| 6 | if (!defined('ABSPATH')) die('Access denied.'); |
| 7 | |
| 8 | if (!class_exists('Updraft_Task_Options')) : |
| 9 | |
| 10 | class Updraft_Task_Options { |
| 11 | |
| 12 | /** |
| 13 | * This method gets an option from the task meta table in the WordPress database |
| 14 | * |
| 15 | * @param int $instance_id the instance id of the task |
| 16 | * @param String $option the name of the option to get |
| 17 | * @param Mixed $default a value to return if the option is not currently set |
| 18 | * |
| 19 | * @return Mixed The option from the database |
| 20 | */ |
| 21 | public static function get_task_option($instance_id, $option, $default = null) { |
| 22 | |
| 23 | $tmp = Updraft_Task_Meta::get_task_meta($instance_id, 'task_options'); |
| 24 | |
| 25 | if (isset($tmp[$option])) { |
| 26 | $value = $tmp[$option]; |
| 27 | } else { |
| 28 | $value = $default; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Filters the value of an existing option. |
| 33 | * |
| 34 | * The dynamic portion of the hook name, `$option`, refers to the option name. |
| 35 | */ |
| 36 | return apply_filters("ud_task_option_{$option}", maybe_unserialize($value), $option, $default, $instance_id); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * This method is used to update a task option stored in the WordPress database |
| 41 | * |
| 42 | * @param int $instance_id the instance id of the task |
| 43 | * @param String $option the name of the option to update |
| 44 | * @param Mixed $value the value to save to the option |
| 45 | * |
| 46 | * @return Mixed the status of the update operation |
| 47 | */ |
| 48 | public static function update_task_option($instance_id, $option, $value) { |
| 49 | |
| 50 | $option = trim($option); |
| 51 | |
| 52 | if (empty($option)) return false; |
| 53 | |
| 54 | $old_value = self::get_task_option($instance_id, $option); |
| 55 | |
| 56 | /** |
| 57 | * Filters a specific option before its value is (maybe) serialized and updated. |
| 58 | */ |
| 59 | $value = apply_filters("ud_pre_update_task_option_{$option}", $value, $old_value, $option, $instance_id); |
| 60 | |
| 61 | $tmp = Updraft_Task_Meta::get_task_meta($instance_id, 'task_options'); |
| 62 | |
| 63 | if (!is_array($tmp)) $tmp = array(); |
| 64 | $tmp[$option] = maybe_serialize($value); |
| 65 | |
| 66 | $result = Updraft_Task_Meta::update_task_meta($instance_id, 'task_options', $tmp); |
| 67 | |
| 68 | if ($result) { |
| 69 | |
| 70 | /** |
| 71 | * Fires after the value of a specific option has been successfully updated. |
| 72 | */ |
| 73 | do_action("ud_update_task_option_{$option}", $value, $old_value, $option, $instance_id); |
| 74 | } |
| 75 | |
| 76 | return $result; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * This method is used to delete a task option stored in the WordPress database |
| 81 | * |
| 82 | * @param int $instance_id the instance id of the task |
| 83 | * @param String $option the option to delete |
| 84 | */ |
| 85 | public static function delete_task_option($instance_id, $option) { |
| 86 | |
| 87 | /** |
| 88 | * Fires immediately before an option is deleted. |
| 89 | */ |
| 90 | do_action("ud_before_delete_task_option", $option, $instance_id); |
| 91 | |
| 92 | $tmp = Updraft_Task_Meta::get_task_meta($instance_id, 'task_options'); |
| 93 | |
| 94 | if (is_array($tmp)) { |
| 95 | if (isset($tmp[$option])) unset($tmp[$option]); |
| 96 | } else { |
| 97 | $tmp = array(); |
| 98 | } |
| 99 | |
| 100 | $result = Updraft_Task_Meta::update_task_meta($instance_id, 'task_options', $tmp); |
| 101 | |
| 102 | if ($result) { |
| 103 | |
| 104 | /** |
| 105 | * Fires after a specific option has been successfully deleted. |
| 106 | */ |
| 107 | do_action("ud_delete_task_option_{$option}", $option); |
| 108 | } |
| 109 | |
| 110 | return $result; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * This method gets all options assoicated with a task |
| 115 | * |
| 116 | * @param int $instance_id the instance id of the task |
| 117 | * |
| 118 | * @return Mixed The options from the database |
| 119 | */ |
| 120 | public static function get_all_task_options($instance_id) { |
| 121 | |
| 122 | $value = Updraft_Task_Meta::get_task_meta($instance_id, 'task_options'); |
| 123 | return apply_filters("ud_all_task_options", maybe_unserialize($value), $instance_id); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | endif; |
| 128 |