| 1 |
<?php |
| 2 |
|
| 3 |
class SimpleTags_Plugin |
| 4 |
{ |
| 5 |
public static $options = null; |
| 6 |
|
| 7 |
/** |
| 8 |
* Add initial ST options in DB, init roles/permissions |
| 9 |
* |
| 10 |
* @return void |
| 11 |
* @author WebFactory Ltd |
| 12 |
*/ |
| 13 |
public static function activation() |
| 14 |
{ |
| 15 |
// Put default options |
| 16 |
$options_from_table = get_option(STAGS_OPTIONS_NAME); |
| 17 |
if (empty($options_from_table)) { |
| 18 |
add_option(STAGS_OPTIONS_NAME, self::load_default_option()); |
| 19 |
} |
| 20 |
|
| 21 |
// Init roles |
| 22 |
if (function_exists('get_role')) { |
| 23 |
$role = get_role('administrator'); |
| 24 |
if (null !== $role && ! $role->has_cap('simple_tags')) { |
| 25 |
$role->add_cap('simple_tags'); |
| 26 |
} |
| 27 |
|
| 28 |
if (null !== $role && ! $role->has_cap('admin_simple_tags')) { |
| 29 |
$role->add_cap('admin_simple_tags'); |
| 30 |
} |
| 31 |
|
| 32 |
$role = get_role('editor'); |
| 33 |
if (null !== $role && ! $role->has_cap('simple_tags')) { |
| 34 |
$role->add_cap('simple_tags'); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
// disable media tag |
| 39 |
taxopress_deactivate_taxonomy('media_tag'); |
| 40 |
|
| 41 |
// add activated option |
| 42 |
add_option('taxopress_activate', true); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Do nothing :) |
| 47 |
*/ |
| 48 |
public static function deactivation() |
| 49 |
{ |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Load default option from specific file |
| 54 |
* |
| 55 |
* @return array |
| 56 |
*/ |
| 57 |
private static function load_default_option() |
| 58 |
{ |
| 59 |
$default_options = (array) include STAGS_DIR . '/inc/helper.options.default.php'; |
| 60 |
|
| 61 |
// add taxopress ai post type and taxonomies options so we can have all post types. TODO: This need to be a filter |
| 62 |
foreach (get_post_types(['public' => true], 'names') as $post_type => $post_type_object) { |
| 63 |
if ($post_type == 'post') { |
| 64 |
$opt_default_value = 'post_tag'; |
| 65 |
} else { |
| 66 |
$opt_default_value = 0; |
| 67 |
} |
| 68 |
$default_options['taxopress_ai_' . $post_type . '_metabox_default_taxonomy'] = $opt_default_value; |
| 69 |
$default_options['taxopress_ai_' . $post_type . '_metabox_display_option'] = 'default'; |
| 70 |
|
| 71 |
$default_options['taxopress_ai_' . $post_type . '_metabox_orderby'] = 'count'; |
| 72 |
$default_options['taxopress_ai_' . $post_type . '_metabox_order'] = 'desc'; |
| 73 |
$default_options['taxopress_ai_' . $post_type . '_metabox_maximum_terms'] = 45; |
| 74 |
$default_options['taxopress_ai_' . $post_type . '_metabox_show_post_count'] = 0; |
| 75 |
$default_options['taxopress_ai_' . $post_type . '_metabox_show_term_slug'] = 0; |
| 76 |
|
| 77 |
$default_options['taxopress_ai_' . $post_type . '_minimum_term_length'] = 2; |
| 78 |
$default_options['taxopress_ai_' . $post_type . '_maximum_term_length'] = 40; |
| 79 |
|
| 80 |
$default_options['taxopress_ai_' . $post_type . '_support_private_taxonomy'] = 0; |
| 81 |
$default_options['taxopress_ai_' . $post_type . '_exclusions'] = ''; |
| 82 |
$default_options['enable_taxopress_ai_' . $post_type . '_metabox'] = $opt_default_value; |
| 83 |
$default_options['taxopress_ai_' . $post_type . '_metabox_filters'] = 1; |
| 84 |
|
| 85 |
foreach (['post_terms', 'existing_terms', 'suggest_local_terms', 'create_terms'] as $taxopress_ai_tab) { |
| 86 |
$default_options['enable_taxopress_ai_' . $post_type . '_' . $taxopress_ai_tab . '_tab'] = ($post_type == 'post') ? 1 : 0; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
// add metabox post type and taxonomies options so we can have all post types. TODO: This need to be a filter |
| 91 |
$tax_names = array_keys(get_taxonomies([], 'names')); |
| 92 |
foreach (taxopress_get_all_wp_roles() as $role_name => $role_info) { |
| 93 |
if (in_array($role_name, ['administrator', 'editor', 'author', 'contributor'])) { |
| 94 |
$enable_acess_default_value = 1; |
| 95 |
} else { |
| 96 |
$enable_acess_default_value = 0; |
| 97 |
} |
| 98 |
$default_options['enable_' . $role_name . '_metabox'] = $enable_acess_default_value; |
| 99 |
$default_options['enable_restrict_' . $role_name . '_metabox'] = $enable_acess_default_value; |
| 100 |
|
| 101 |
// ONLY admin + editor can edit the metabox label by default |
| 102 |
$default_options['enable_edit_' . $role_name . '_metabox'] = in_array($role_name, ['administrator', 'editor']) ? 1 : 0; |
| 103 |
|
| 104 |
$options['enable_metabox_' . $role_name . ''] = $tax_names; |
| 105 |
$options['remove_taxonomy_metabox_' . $role_name . ''] = []; |
| 106 |
} |
| 107 |
|
| 108 |
return $default_options; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Load plugin option, combine DB options with default |
| 113 |
*/ |
| 114 |
private static function load_option() |
| 115 |
{ |
| 116 |
$saved_option = wp_parse_args((array) get_option(STAGS_OPTIONS_NAME), self::load_default_option()); |
| 117 |
|
| 118 |
self::$options = $saved_option; |
| 119 |
} |
| 120 |
|
| 121 |
/* |
| 122 |
* Get all options into an array |
| 123 |
* |
| 124 |
* @return array |
| 125 |
*/ |
| 126 |
public static function get_option() |
| 127 |
{ |
| 128 |
if (null === self::$options) { |
| 129 |
self::load_option(); |
| 130 |
} |
| 131 |
|
| 132 |
return self::$options; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Get one option value from all options |
| 137 |
* |
| 138 |
* @param string $key |
| 139 |
* |
| 140 |
* @return bool|mixed |
| 141 |
*/ |
| 142 |
public static function get_option_value($key = '') |
| 143 |
{ |
| 144 |
if (null === self::$options) { |
| 145 |
self::load_option(); |
| 146 |
} |
| 147 |
|
| 148 |
return isset(self::$options[ $key ]) ? self::$options[ $key ] : false; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Update one option value from all options |
| 153 |
* |
| 154 |
* @param string $key |
| 155 |
* @param string $value |
| 156 |
* @param bool $auto_update |
| 157 |
*/ |
| 158 |
public static function set_option_value($key = '', $value = '', $auto_update = true) |
| 159 |
{ |
| 160 |
if (null === self::$options) { |
| 161 |
self::load_option(); |
| 162 |
} |
| 163 |
|
| 164 |
if (isset(self::$options[ $key ])) { |
| 165 |
self::$options[ $key ] = $value; |
| 166 |
|
| 167 |
if (true === $auto_update) { |
| 168 |
self::update_option(); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Update all options |
| 175 |
* |
| 176 |
* @param $value |
| 177 |
* @param bool $auto_update |
| 178 |
*/ |
| 179 |
public static function set_option($value, $auto_update = true) |
| 180 |
{ |
| 181 |
self::$options = $value; |
| 182 |
|
| 183 |
if (true === $auto_update) { |
| 184 |
self::update_option(); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Set default option into DB |
| 190 |
*/ |
| 191 |
public static function set_default_option() |
| 192 |
{ |
| 193 |
self::$options = self::load_default_option(); |
| 194 |
self::update_option(); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Update options into DB |
| 199 |
* |
| 200 |
* @return bool |
| 201 |
*/ |
| 202 |
public static function update_option() |
| 203 |
{ |
| 204 |
if (null === self::$options) { |
| 205 |
self::load_option(); |
| 206 |
} |
| 207 |
|
| 208 |
return update_option(STAGS_OPTIONS_NAME, self::$options); |
| 209 |
} |
| 210 |
} |
| 211 |
|