set_defaults(); // set default settings } /** * get_instance function * * Return singleton instance * * @return object **/ static function get_instance() { if( self::$_instance === NULL ) { // get the settings from the database self::$_instance = get_option( 'ai1ec_settings' ); // if there are no settings in the database // save default values for the settings if( ! self::$_instance ) { self::$_instance = new self(); delete_option( 'ai1ec_settings' ); add_option( 'ai1ec_settings', self::$_instance ); } else { self::$_instance->set_defaults(); // set default settings } } return self::$_instance; } /** * save function * * Save settings to the database. * * @return void **/ function save() { update_option( 'ai1ec_settings', $this ); update_option( 'start_of_week', $this->week_start_day ); update_option( 'ai1ec_cron_version', get_option( 'ai1ec_cron_version' ) + 1 ); update_option( 'timezone_string', $this->timezone ); } /** * set_defaults function * * Set default values for settings. * * @return void **/ function set_defaults() { $defaults = array( 'calendar_page_id' => 0, 'default_calendar_view' => 'month', 'calendar_css_selector' => '', 'week_start_day' => get_option( 'start_of_week' ), 'agenda_events_per_page' => get_option( 'posts_per_page' ), 'agenda_events_expanded' => FALSE, 'include_events_in_rss' => FALSE, 'allow_publish_to_facebook' => FALSE, 'facebook_credentials' => NULL, 'user_role_can_create_event' => NULL, 'show_publish_button' => FALSE, 'hide_maps_until_clicked' => FALSE, 'exclude_from_search' => FALSE, 'show_create_event_button' => FALSE, 'turn_off_subscription_buttons' => FALSE, 'inject_categories' => FALSE, 'input_date_format' => 'def', 'input_24h_time' => FALSE, 'cron_freq' => 'daily', 'timezone' => get_option( 'timezone_string' ), 'geo_region_biasing' => FALSE, 'show_data_notification' => TRUE, 'allow_statistics' => FALSE, 'disable_autocompletion' => FALSE, ); foreach( $defaults as $key => $default ) { if( ! isset( $this->$key ) ) $this->$key = $default; } } /** * Updates field values with corresponding values found in $params * associative array. * * @param string $settings_page Which settings page is being updated. * @param array $params Assoc. array of new settings, e.g. from $_REQUEST. * * @return void */ function update( $settings_page, $params ) { switch ($settings_page) { // ================== // = Settings page. = // ================== case 'settings': $field_names = array( 'default_calendar_view', 'calendar_css_selector', 'week_start_day', 'agenda_events_per_page', 'input_date_format', 'allow_events_posting_facebook', 'facebook_credentials', 'user_role_can_create_event', 'timezone', ); $checkboxes = array( 'agenda_events_expanded', 'include_events_in_rss', 'show_publish_button', 'hide_maps_until_clicked', 'exclude_from_search', 'show_create_event_button', 'turn_off_subscription_buttons', 'inject_categories', 'input_24h_time', 'geo_region_biasing', 'allow_statistics', 'disable_autocompletion' ); // Assign parameters to settings. foreach( $field_names as $field_name ) { if( isset( $params[$field_name] ) ) { $this->$field_name = $params[$field_name]; } } foreach( $checkboxes as $checkbox ) { $this->$checkbox = isset( $params[$checkbox] ) ? TRUE : FALSE; } // Validate specific parameters. $this->agenda_events_per_page = intval( $this->agenda_events_per_page ); if( $this->agenda_events_per_page <= 0 ) { $this->agenda_events_per_page = 1; } // Update special parameters. $this->update_page( 'calendar_page_id', $params ); break; // =============== // = Feeds page. = // =============== case 'feeds': // Assign parameters to settings. if( isset( $params['cron_freq'] ) ) { $this->cron_freq = $params['cron_freq']; } break; } } /** * Update setting of show_data_notification - whether to display data * collection notice on the admin side. * * @param boolean $value The new setting for show_data_notification. * @return void */ function update_notification( $value = FALSE ) { $this->show_data_notification = $value; update_option( 'ai1ec_settings', $this ); } /** * update_page function * * Update page for the calendar with the one specified by the drop-down box. * If the value is not numeric, user chose to auto-create a new page, * therefore do so. * * @param string $field_name * @param array $params * * @return void **/ function update_page( $field_name, &$params ) { if( ! is_numeric( $params[$field_name] ) && preg_match( '#^__auto_page:(.*?)$#', $params[$field_name], $matches ) ) { $this->$field_name = $params[$field_name] = $this->auto_add_page( $matches[1] ); } else { $this->$field_name = (int) $params[$field_name]; } } /** * auto_add_page function * * Auto-create a WordPress page with given name for use by this plugin. * * @param string page_name * * @return int the new page's ID. **/ function auto_add_page( $page_name ) { return wp_insert_post( array( 'post_title' => $page_name, 'post_type' => 'page', 'post_status' => 'publish', 'comment_status' => 'closed' ) ); } } // END class