| 1 |
<?php |
| 2 |
|
| 3 |
if(!class_exists('MaxGalleryOptions')) { |
| 4 |
|
| 5 |
class MaxGalleryOptions { |
| 6 |
private $_post_id; |
| 7 |
|
| 8 |
public function __construct($post_id) { |
| 9 |
$this->_post_id = $post_id; |
| 10 |
} |
| 11 |
|
| 12 |
public function get_post_id() { |
| 13 |
return $this->_post_id; |
| 14 |
} |
| 15 |
|
| 16 |
public function get_post_meta($meta_key) { |
| 17 |
return get_post_meta($this->get_post_id(), $meta_key, true); |
| 18 |
} |
| 19 |
|
| 20 |
public function delete_post_meta($meta_key) { |
| 21 |
delete_post_meta($this->get_post_id(), $meta_key); |
| 22 |
} |
| 23 |
|
| 24 |
public function save_post_meta($meta_key) { |
| 25 |
$post_id = $this->get_post_id(); |
| 26 |
|
| 27 |
$meta_old_value = get_post_meta($post_id, $meta_key, true); |
| 28 |
$meta_new_value = isset($_POST[$meta_key]) ? stripslashes($_POST[$meta_key]) : ''; |
| 29 |
|
| 30 |
// If the option is the saves count, we need to check if we have a template value. If we do, then |
| 31 |
// we increment the saves count; otherwise, we reset the saves count back to -1 to ensure gallery |
| 32 |
// options get their proper default values. |
| 33 |
if ($meta_key == $this->saves_count_key) { |
| 34 |
$meta_new_value = ($this->get_template() != '') ? ((int)$meta_new_value) + 1 : -1; |
| 35 |
} |
| 36 |
|
| 37 |
update_post_meta($post_id, $meta_key, $meta_new_value, $meta_old_value); |
| 38 |
} |
| 39 |
|
| 40 |
// These options are common to all templates |
| 41 |
public $custom_scripts_enabled_default = ''; |
| 42 |
public $custom_scripts_enabled_key = 'maxgallery_custom_scripts_enabled'; |
| 43 |
public $custom_scripts_url_key = 'maxgallery_custom_scripts_url'; |
| 44 |
public $custom_styles_enabled_default = ''; |
| 45 |
public $custom_styles_enabled_key = 'maxgallery_custom_styles_enabled'; |
| 46 |
public $custom_styles_url_key = 'maxgallery_custom_styles_url'; |
| 47 |
public $description_enabled_default = ''; |
| 48 |
public $description_enabled_key = 'maxgallery_description_enabled'; |
| 49 |
public $description_position_default = 'above'; |
| 50 |
public $description_position_key = 'maxgallery_description_position'; |
| 51 |
public $description_text_key = 'maxgallery_description_text'; |
| 52 |
public $reset_options_default = ''; |
| 53 |
public $reset_options_key = 'maxgallery_reset_options'; |
| 54 |
public $saves_count_default = -1; |
| 55 |
public $saves_count_key = 'maxgallery_saves_count'; |
| 56 |
public $template_key = 'maxgallery_template'; |
| 57 |
public $type_key = 'maxgallery_type'; |
| 58 |
|
| 59 |
public function get_custom_scripts_enabled() { |
| 60 |
$value = $this->get_post_meta($this->custom_scripts_enabled_key); |
| 61 |
if ($value == '') { |
| 62 |
$value = $this->custom_scripts_enabled_default; |
| 63 |
} |
| 64 |
|
| 65 |
return $value; |
| 66 |
} |
| 67 |
|
| 68 |
public function get_custom_scripts_url() { |
| 69 |
return $this->get_post_meta($this->custom_scripts_url_key); |
| 70 |
} |
| 71 |
|
| 72 |
public function get_custom_styles_enabled() { |
| 73 |
$value = $this->get_post_meta($this->custom_styles_enabled_key); |
| 74 |
if ($value == '') { |
| 75 |
$value = $this->custom_styles_enabled_default; |
| 76 |
} |
| 77 |
|
| 78 |
return $value; |
| 79 |
} |
| 80 |
|
| 81 |
public function get_custom_styles_url() { |
| 82 |
return $this->get_post_meta($this->custom_styles_url_key); |
| 83 |
} |
| 84 |
|
| 85 |
public function get_description_enabled() { |
| 86 |
$value = $this->get_post_meta($this->description_enabled_key); |
| 87 |
if ($value == '') { |
| 88 |
$value = $this->description_enabled_default; |
| 89 |
} |
| 90 |
|
| 91 |
return $value; |
| 92 |
} |
| 93 |
|
| 94 |
public function get_description_position() { |
| 95 |
$value = $this->get_post_meta($this->description_position_key); |
| 96 |
if ($value == '') { |
| 97 |
$value = $this->description_position_default; |
| 98 |
} |
| 99 |
|
| 100 |
return $value; |
| 101 |
} |
| 102 |
|
| 103 |
public function get_description_text() { |
| 104 |
return $this->get_post_meta($this->description_text_key); |
| 105 |
} |
| 106 |
|
| 107 |
public function get_reset_options() { |
| 108 |
$value = $this->get_post_meta($this->reset_options_key); |
| 109 |
if ($value == '') { |
| 110 |
$value = $this->reset_options_default; |
| 111 |
} |
| 112 |
|
| 113 |
return $value; |
| 114 |
} |
| 115 |
|
| 116 |
public function get_saves_count() { |
| 117 |
$value = $this->get_post_meta($this->saves_count_key); |
| 118 |
if ($value == '') { |
| 119 |
$value = $this->saves_count_default; |
| 120 |
} |
| 121 |
|
| 122 |
return $value; |
| 123 |
} |
| 124 |
|
| 125 |
public function get_template() { |
| 126 |
return $this->get_post_meta($this->template_key); |
| 127 |
} |
| 128 |
|
| 129 |
public function get_type() { |
| 130 |
return $this->get_post_meta($this->type_key); |
| 131 |
} |
| 132 |
|
| 133 |
public function is_new_gallery() { |
| 134 |
// Use get_post_meta() instead of get_type() because get_type() will return |
| 135 |
// the default if it's an empty string, but we want to know if it's actually |
| 136 |
// an empty string to know if this is a new gallery or not. |
| 137 |
return ($this->get_post_meta($this->type_key) == '') ? true : false; |
| 138 |
} |
| 139 |
|
| 140 |
public function is_image_gallery() { |
| 141 |
return ($this->get_type() == 'image') ? true : false; |
| 142 |
} |
| 143 |
|
| 144 |
public function is_video_gallery() { |
| 145 |
return ($this->get_type() == 'video') ? true : false; |
| 146 |
} |
| 147 |
|
| 148 |
public function is_reset_options() { |
| 149 |
if (isset($_POST[$this->reset_options_key]) && $_POST[$this->reset_options_key] == 'on') { |
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
public function save_options($options = null) { |
| 157 |
if ($this->is_new_gallery()) { |
| 158 |
$this->save_post_meta($this->type_key); |
| 159 |
} |
| 160 |
else { |
| 161 |
// Get the base options and merge in the options that were passed in, if any |
| 162 |
$base_options = $this->base_options(); |
| 163 |
$all_options = isset($options) ? array_merge($base_options, $options) : $base_options; |
| 164 |
|
| 165 |
foreach ($all_options as $option) { |
| 166 |
if ($this->is_reset_options()) { |
| 167 |
// Check to reset saves count back to 0 (instead of deleting it) |
| 168 |
if ($option == $this->saves_count_key) { |
| 169 |
update_post_meta($this->get_post_id(), $option, 0); |
| 170 |
} |
| 171 |
elseif ($option != $this->template_key) { // Don't reset the template |
| 172 |
$this->delete_post_meta($option); |
| 173 |
} |
| 174 |
} |
| 175 |
else { |
| 176 |
$this->save_post_meta($option); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
private function base_options() { |
| 183 |
return array( |
| 184 |
$this->template_key, // IMPORTANT: MUST ALWAYS COME FIRST |
| 185 |
$this->custom_scripts_enabled_key, |
| 186 |
$this->custom_scripts_url_key, |
| 187 |
$this->custom_styles_enabled_key, |
| 188 |
$this->custom_styles_url_key, |
| 189 |
$this->description_enabled_key, |
| 190 |
$this->description_position_key, |
| 191 |
$this->description_text_key, |
| 192 |
$this->saves_count_key |
| 193 |
); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
?> |