/public/wp-content/plugins/the-events-calendar/common/src/Tribe/Settings_Manager.php

https://gitlab.com/kath.de/cibedo_cibedo.de · PHP · 349 lines · 184 code · 45 blank · 120 comment · 29 complexity · 7fe4989212d9d387a8c61912194e3a54 MD5 · raw file

  1. <?php
  2. class Tribe__Settings_Manager {
  3. protected static $network_options;
  4. public static $tribe_events_mu_defaults;
  5. /**
  6. * constructor
  7. */
  8. public function __construct() {
  9. $this->add_hooks();
  10. // Load multisite defaults
  11. if ( is_multisite() ) {
  12. $tribe_events_mu_defaults = array();
  13. if ( file_exists( WP_CONTENT_DIR . '/tribe-events-mu-defaults.php' ) ) {
  14. require_once WP_CONTENT_DIR . '/tribe-events-mu-defaults.php';
  15. }
  16. self::$tribe_events_mu_defaults = apply_filters( 'tribe_events_mu_defaults', $tribe_events_mu_defaults );
  17. }
  18. }
  19. public function add_hooks() {
  20. // option pages
  21. add_action( '_network_admin_menu', array( $this, 'init_options' ) );
  22. add_action( '_admin_menu', array( $this, 'init_options' ) );
  23. add_action( 'admin_menu', array( $this, 'add_help_admin_menu_item' ), 50 );
  24. add_action( 'tribe_settings_do_tabs', array( $this, 'do_setting_tabs' ) );
  25. add_action( 'tribe_settings_do_tabs', array( $this, 'do_network_settings_tab' ), 400 );
  26. add_action( 'tribe_settings_content_tab_help', array( $this, 'do_help_tab' ) );
  27. add_action( 'tribe_settings_validate_tab_network', array( $this, 'save_all_tabs_hidden' ) );
  28. }
  29. /**
  30. * Init the settings API and add a hook to add your own setting tabs
  31. *
  32. * @return void
  33. */
  34. public function init_options() {
  35. Tribe__Settings::instance();
  36. }
  37. /**
  38. * Create setting tabs
  39. *
  40. * @return void
  41. */
  42. public function do_setting_tabs() {
  43. include_once Tribe__Main::instance()->plugin_path . 'src/admin-views/tribe-options-general.php';
  44. include_once Tribe__Main::instance()->plugin_path . 'src/admin-views/tribe-options-display.php';
  45. $showNetworkTabs = $this->get_network_option( 'showSettingsTabs', false );
  46. new Tribe__Settings_Tab( 'general', esc_html__( 'General', 'tribe-common' ), $generalTab );
  47. new Tribe__Settings_Tab( 'display', esc_html__( 'Display', 'tribe-common' ), $displayTab );
  48. $this->do_licenses_tab();
  49. new Tribe__Settings_Tab(
  50. 'help',
  51. esc_html__( 'Help', 'tribe-common' ),
  52. array(
  53. 'priority' => 60,
  54. 'show_save' => false,
  55. )
  56. );
  57. }
  58. /**
  59. * Get all options for the Events Calendar
  60. *
  61. * @return array of options
  62. */
  63. public static function get_options() {
  64. $options = get_option( Tribe__Main::OPTIONNAME, array() );
  65. if ( has_filter( 'tribe_get_options' ) ) {
  66. _deprecated_function( 'tribe_get_options', '3.10', 'option_' . Tribe__Main::OPTIONNAME );
  67. $options = apply_filters( 'tribe_get_options', $options );
  68. }
  69. return $options;
  70. }
  71. /**
  72. * Get value for a specific option
  73. *
  74. * @param string $option_name name of option
  75. * @param string $default default value
  76. *
  77. * @return mixed results of option query
  78. */
  79. public static function get_option( $option_name, $default = '' ) {
  80. if ( ! $option_name ) {
  81. return null;
  82. }
  83. $options = self::get_options();
  84. $option = $default;
  85. if ( isset( $options[ $option_name ] ) ) {
  86. $option = $options[ $option_name ];
  87. } elseif ( is_multisite() && isset( self::$tribe_events_mu_defaults ) && is_array( self::$tribe_events_mu_defaults ) && in_array( $option_name, array_keys( self::$tribe_events_mu_defaults ) ) ) {
  88. $option = self::$tribe_events_mu_defaults[ $option_name ];
  89. }
  90. return apply_filters( 'tribe_get_single_option', $option, $default, $option_name );
  91. }
  92. /**
  93. * Saves the options for the plugin
  94. *
  95. * @param array $options formatted the same as from get_options()
  96. * @param bool $apply_filters
  97. *
  98. * @return void
  99. */
  100. public static function set_options( $options, $apply_filters = true ) {
  101. if ( ! is_array( $options ) ) {
  102. return;
  103. }
  104. if ( $apply_filters == true ) {
  105. $options = apply_filters( 'tribe-events-save-options', $options );
  106. }
  107. update_option( Tribe__Main::OPTIONNAME, $options );
  108. }
  109. /**
  110. * Set an option
  111. *
  112. * @param string $name
  113. * @param mixed $value
  114. *
  115. * @return void
  116. */
  117. public static function set_option( $name, $value ) {
  118. $newOption = array();
  119. $newOption[ $name ] = $value;
  120. $options = self::get_options();
  121. self::set_options( wp_parse_args( $newOption, $options ) );
  122. }
  123. /**
  124. * Get all network options for the Events Calendar
  125. *
  126. * @return array of options
  127. * @TODO add force option, implement in setNetworkOptions
  128. */
  129. public static function get_network_options() {
  130. if ( ! isset( self::$network_options ) ) {
  131. $options = get_site_option( Tribe__Main::OPTIONNAMENETWORK, array() );
  132. self::$network_options = apply_filters( 'tribe_get_network_options', $options );
  133. }
  134. return self::$network_options;
  135. }
  136. /**
  137. * Get value for a specific network option
  138. *
  139. * @param string $option_name name of option
  140. * @param string $default default value
  141. *
  142. * @return mixed results of option query
  143. */
  144. public static function get_network_option( $option_name, $default = '' ) {
  145. if ( ! $option_name ) {
  146. return null;
  147. }
  148. if ( ! isset( self::$network_options ) ) {
  149. self::get_network_options();
  150. }
  151. if ( isset( self::$network_options[ $option_name ] ) ) {
  152. $option = self::$network_options[ $option_name ];
  153. } else {
  154. $option = $default;
  155. }
  156. return apply_filters( 'tribe_get_single_network_option', $option, $default );
  157. }
  158. /**
  159. * Saves the network options for the plugin
  160. *
  161. * @param array $options formatted the same as from get_options()
  162. * @param bool $apply_filters
  163. *
  164. * @return void
  165. */
  166. public static function set_network_options( $options, $apply_filters = true ) {
  167. if ( ! is_array( $options ) ) {
  168. return;
  169. }
  170. if ( $apply_filters == true ) {
  171. $options = apply_filters( 'tribe-events-save-network-options', $options );
  172. }
  173. // @TODO use getNetworkOptions + force
  174. if ( update_site_option( Tribe__Main::OPTIONNAMENETWORK, $options ) ) {
  175. self::$network_options = apply_filters( 'tribe_get_network_options', $options );
  176. } else {
  177. self::$network_options = self::get_network_options();
  178. }
  179. }
  180. /**
  181. * Add the network admin options page
  182. *
  183. * @return void
  184. */
  185. public static function add_network_options_page() {
  186. $tribe_settings = Tribe__Settings::instance();
  187. add_submenu_page(
  188. 'settings.php', $tribe_settings->menuName, $tribe_settings->menuName, 'manage_network_options', 'tribe-common', array(
  189. $tribe_settings,
  190. 'generatePage',
  191. )
  192. );
  193. }
  194. /**
  195. * Render network admin options view
  196. *
  197. * @return void
  198. */
  199. public static function do_network_settings_tab() {
  200. include_once Tribe__Main::instance()->plugin_path . 'src/admin-views/tribe-options-network.php';
  201. new Tribe__Settings_Tab( 'network', esc_html__( 'Network', 'tribe-common' ), $networkTab );
  202. }
  203. /**
  204. * Registers the license key management tab in the Events > Settings screen,
  205. * only if premium addons are detected.
  206. */
  207. protected function do_licenses_tab() {
  208. $show_tab = ( current_user_can( 'activate_plugins' ) && $this->have_addons() );
  209. /**
  210. * Provides an oppotunity to override the decision to show or hide the licenses tab
  211. *
  212. * Normally it will only show if the current user has the "activate_plugins" capability
  213. * and there are some currently-activated premium plugins.
  214. *
  215. * @var bool
  216. */
  217. if ( ! apply_filters( 'tribe_events_show_licenses_tab', $show_tab ) ) {
  218. return;
  219. }
  220. /**
  221. * @var $licenses_tab
  222. */
  223. include Tribe__Main::instance()->plugin_path . 'src/admin-views/tribe-options-licenses.php';
  224. /**
  225. * Allows the fields displayed in the licenses tab to be modified.
  226. *
  227. * @var array
  228. */
  229. $license_fields = apply_filters( 'tribe_license_fields', $licenses_tab );
  230. new Tribe__Settings_Tab( 'licenses', esc_html__( 'Licenses', 'tribe-common' ), array(
  231. 'priority' => '40',
  232. 'fields' => $license_fields,
  233. 'network_admin' => is_network_admin() ? true : false,
  234. ) );
  235. }
  236. /**
  237. * Create the help tab
  238. */
  239. public function do_help_tab() {
  240. include_once Tribe__Main::instance()->plugin_path . 'src/admin-views/tribe-options-help.php';
  241. }
  242. /**
  243. * Add help menu item to the admin (unless blocked via network admin settings).
  244. *
  245. * @todo move to an admin class
  246. */
  247. public function add_help_admin_menu_item() {
  248. $hidden_settings_tabs = self::get_network_option( 'hideSettingsTabs', array() );
  249. if ( in_array( 'help', $hidden_settings_tabs ) ) {
  250. return;
  251. }
  252. $parent = Tribe__Settings::$parent_slug;
  253. $title = esc_html__( 'Help', 'tribe-common' );
  254. $slug = esc_url(
  255. apply_filters( 'tribe_settings_url',
  256. add_query_arg(
  257. array(
  258. 'page' => 'tribe-common',
  259. 'tab' => 'help',
  260. ),
  261. Tribe__Settings::$parent_page
  262. )
  263. )
  264. );
  265. add_submenu_page( $parent, $title, $title, 'manage_options', $slug, '' );
  266. }
  267. /**
  268. * Tries to discover if licensable addons are activated on the same site.
  269. *
  270. * @return bool
  271. */
  272. protected function have_addons() {
  273. $addons = apply_filters( 'tribe_licensable_addons', array() );
  274. return ! empty( $addons );
  275. }
  276. /**
  277. * Save hidden tabs
  278. *
  279. * @return void
  280. */
  281. public function save_all_tabs_hidden() {
  282. $all_tabs_keys = array_keys( apply_filters( 'tribe_settings_all_tabs', array() ) );
  283. $network_options = (array) get_site_option( Tribe__Main::OPTIONNAMENETWORK );
  284. if ( isset( $_POST['hideSettingsTabs'] ) && $_POST['hideSettingsTabs'] == $all_tabs_keys ) {
  285. $network_options['allSettingsTabsHidden'] = '1';
  286. } else {
  287. $network_options['allSettingsTabsHidden'] = '0';
  288. }
  289. $this->set_network_options( $network_options );
  290. }
  291. /**
  292. * Static Singleton Factory Method
  293. *
  294. * @return Tribe__Settings_Manager
  295. */
  296. public static function instance() {
  297. static $instance;
  298. if ( ! $instance ) {
  299. $class_name = __CLASS__;
  300. $instance = new $class_name;
  301. }
  302. return $instance;
  303. }
  304. }