/** * retrieves settings option from DB or defaults if they do not exist * * @return array settings data array */ function clgs_get_settings() { $settings_defaults = clgs_settings_defaults(); if (clgs_is_network_mode()) { unset($settings_defaults['manager_role']); $settings = get_site_option(CLGS_SETTINGS, array()); } else { $settings = get_option(CLGS_SETTINGS, array()); } $args = wp_parse_args($settings, $settings_defaults); // needed? return $args; }
/** * inits setting fields for settings page * * @global array $clgs_settings_structure * * @return void */ function clgs_settings_init() { global $clgs_settings_structure; if (clgs_is_network_mode()) { unset($clgs_settings_structure['manager_role']); } else { register_setting(CLGS_SETTINGS, CLGS_SETTINGS, 'clgs_sanitize'); } add_settings_section(CLGS_GROUP, null, null, CLGS_OPTION_PAGE); foreach ($clgs_settings_structure as $key => $rule) { add_settings_field($key, __($rule['desc'], 'custom-logging-service'), 'clgs_field_render', CLGS_OPTION_PAGE, CLGS_GROUP, [$key]); } }
/** * returns notification bubble for log page menu entry * * @global Clgs_DB $clgs_db * * @return string bubble markup */ function clgs_unseen_field() { global $clgs_db; $where = array('seen' => false, 'min_severity' => clgs_get_settings()['notification_severity_filter']); if (clgs_is_network_mode() && !is_main_site()) { $where['blog_id'] = get_current_blog_id(); } $unseen = $clgs_db->get_entries($where, true); if ($unseen > 0) { $title = sprintf(__('%d unseen Log entries', 'custom-logging-service'), $unseen); return " <span class=\"awaiting-mod count-{$unseen}\" title=\"{$title}\"><span>{$unseen}</span></span>"; } else { return ''; } }
/** * writes a new log entry in the specified category. * * @global Clgs_DB $clgs_db * @global Clgs_Last_Log $clgs_last_log * * @param string $category a registered category name * @param string $text the logged message, can contain HTML same as comments * (filtered by wp_kses_data) * @param int $severity one of defined severity levels (see above); if missing * defaults to CLGS_NOCATEGORY * @param mixed $user user id, slug or WP user object are aceptable; if missing * defaults to current user (or a placeholder if none is logged in) * @param int $blog_id blog id; if missing defaults to current blog * @param mixed $date a UNIX timestamp or a string recognized by strtotime(); * if missing defaults to current time * * @return boolean false if entering the log failed. */ function clgs_log($category, $text, $severity = null, $user = null, $blog_id = null, $date = null) { global $clgs_db, $clgs_last_log; // category must pass validation if (is_null($date)) { $date = time(); } $rules = array('category' => array('sanitize' => 'string', 'validate' => 'registered'), 'text' => array('sanitize' => 'kses_string', 'validate' => 'length'), 'date' => array('sanitize' => 'time', 'validate' => 'sanitation')); $must = clgs_evaluate(compact('category', 'text', 'date'), $rules, 'block'); if (!$must) { return false; } // all others have a default $rules = array('user' => array('sanitize_function' => 'clgs_to_user', 'validate' => 'exists', 'default' => is_user_logged_in() ? wp_get_current_user()->display_name : ' — '), 'blog_id' => array('sanitize' => 'int', 'validate' => 'positive', 'default' => get_current_blog_id()), 'severity' => array('sanitize' => 'int', 'validate' => 'severity', 'default' => CLGS_NOSEVERITY)); $sane = clgs_evaluate(compact('date', 'user', 'blog_id', 'severity'), $rules); // get blog name if (clgs_is_network_mode()) { switch_to_blog($sane['blog_id']); $blog_name = get_bloginfo('name'); restore_current_blog(); } else { $blog_name = get_bloginfo('name'); } $data = array('category' => $must['category'], 'blog_id' => $sane['blog_id'], 'blog_name' => $blog_name, 'date' => $must['date'], 'user_name' => $sane['user'], 'text' => $must['text'], 'severity' => $sane['severity']); if ($clgs_last_log->compare($data)) { $clgs_last_log->write(); $first = $clgs_last_log->data['date']; $data['text'] = '(' . $clgs_last_log->count . '× ' . __('since ', 'custom-logging-service') . '<span data-date="' . $first . '"></span>):<br/>' . $data['text']; $ok = (bool) $clgs_db->update_entry($clgs_last_log->entry_id, $data); } else { $entry_id = $clgs_db->insert_entry($data); $ok = (bool) $entry_id; if ($ok) { $clgs_last_log->set($data, $entry_id); } } return $ok; }
/** * Set configuration data for the table * * @access public * * @param array $data list of selection and ordering arguments * * @return array */ function set_attributes($attrs) { extract($attrs); $this->where = compact('seen', 'min_severity', 'category'); if (isset($entry_id)) { // rename for database $this->where['id'] = $entry_id; } if (clgs_is_network_mode() && !is_main_site()) { $this->where['blog_id'] = get_current_blog_id(); } $this->order = array('dir' => $order, 'by' => $orderby); }