public function save() { if (!$this->validate()) { return new \WP_Error('validation-error', esc_html__('Could not validate record data.', 'stream')); } return wp_stream_get_instance()->db->insert((array) $this); }
/** * Version 3.0.0 * * Update from 1.4.9 * * @param string $db_version * @param string $current_version * * @return string */ function wp_stream_update_300($db_version, $current_version) { global $wpdb; // Get only the author_meta values that are double-serialized $plugin = wp_stream_get_instance(); $prefix = $plugin->install->table_prefix; return $current_version; }
/** * Class constructor. * * @param int $user_id The user ID. * @param array|string $user_meta The user meta array, or a serialized string of user meta. */ function __construct($user_id, $user_meta = array()) { $this->id = absint($user_id); $this->meta = maybe_unserialize($user_meta); if ($this->id) { $this->user = new \WP_User($this->id); } $this->plugin = wp_stream_get_instance(); }
public function __construct() { if (!class_exists('WP_Stream\\Plugin')) { add_action('admin_notices', array($this, 'stream_not_found_notice')); return false; } $this->stream = wp_stream_get_instance(); $this->options = $this->stream->settings->options; add_filter('wp_stream_settings_option_fields', array($this, 'options')); if (empty($this->options['papertrail_destination'])) { add_action('admin_notices', array($this, 'destination_undefined_notice')); } else { add_action('wp_stream_record_inserted', array($this, 'log'), 10, 2); } }
/** * Version 3.0.0 * * Update from 1.4.9 * * @param string $db_version * @param string $current_version * * @return string */ function wp_stream_update_auto_300($db_version, $current_version) { global $wpdb; // Get only the author_meta values that are double-serialized $wpdb->query("RENAME TABLE {$wpdb->base_prefix}stream TO {$wpdb->base_prefix}stream_tmp, {$wpdb->base_prefix}stream_context TO {$wpdb->base_prefix}stream_context_tmp"); $plugin = wp_stream_get_instance(); $plugin->install->install($current_version); $stream_entries = $wpdb->get_results("SELECT * FROM {$wpdb->base_prefix}stream_tmp"); foreach ($stream_entries as $entry) { $context = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->base_prefix}stream_context_tmp WHERE record_id = %s LIMIT 1", $entry->ID)); $new_entry = array('site_id' => $entry->site_id, 'blog_id' => $entry->blog_id, 'user_id' => $entry->author, 'user_role' => $entry->author_role, 'summary' => $entry->summary, 'created' => $entry->created, 'connector' => $context->connector, 'context' => $context->context, 'action' => $context->action, 'ip' => $entry->ip); if ($entry->object_id && 0 !== $entry->object_id) { $new_entry['object_id'] = $entry->object_id; } $wpdb->insert($wpdb->base_prefix . 'stream', $new_entry); } $wpdb->query("DROP TABLE {$wpdb->base_prefix}stream_tmp, {$wpdb->base_prefix}stream_context_tmp"); return $current_version; }
/** * Insert a record * * @param array $recordarr * * @return int */ public function insert($recordarr) { if (defined('WP_IMPORTING') && WP_IMPORTING) { return false; } /** * Filter allows modification of record information * * @param array $recordarr * * @return array */ $recordarr = apply_filters('wp_stream_record_array', $recordarr); if (empty($recordarr)) { return false; } global $wpdb; $fields = array('object_id', 'site_id', 'blog_id', 'user_id', 'user_role', 'created', 'summary', 'ip', 'connector', 'context', 'action'); $data = array_intersect_key($recordarr, array_flip($fields)); $data = array_filter($data); $result = $wpdb->insert($this->table, $data); if (1 !== $result) { /** * Fires on a record insertion error * * @param array $recordarr * @param mixed $result */ do_action('wp_stream_record_insert_error', $recordarr, $result); return $result; } $record_id = $wpdb->insert_id; // Insert record meta foreach ((array) $recordarr['meta'] as $key => $vals) { // If associative array, serialize it, otherwise loop on its members $vals = is_array($vals) && 0 !== key($vals) ? array($vals) : $vals; foreach ((array) $vals as $val) { $val = maybe_serialize($val); $this->insert_meta($record_id, $key, $val); } } // API.logit($recordarr); // API::logit('yeah'); // wp_stream_get_instance()->api->logit('yeah!!!'); wp_stream_get_instance()->api->new_records($recordarr); // $this->api->logit($recordarr); /** * Fires after a record has been inserted * * @param int $record_id * @param array $recordarr */ do_action('wp_stream_record_inserted', $record_id, $recordarr); return absint($record_id); }
/** * Catch registeration of post_types after initial loading, to cache its labels * * @action registered_post_type * * @param string $post_type Post type slug * @param array $args Arguments used to register the post type */ public function _registered_post_type($post_type, $args) { unset($args); $post_type_obj = get_post_type_object($post_type); $label = $post_type_obj->label; wp_stream_get_instance()->connectors->term_labels['stream_context'][$post_type] = $label; }
/** * Catch registration of taxonomies after inital loading, so we can cache its labels * * @action registered_taxonomy * * @param string $taxonomy Taxonomy slug * @param array|string $object_type Object type or array of object types * @param array|string $args Array or string of taxonomy registration arguments */ public function _registered_taxonomy($taxonomy, $object_type, $args) { unset($object_type); $taxonomy_obj = (object) $args; $label = get_taxonomy_labels($taxonomy_obj)->name; $this->context_labels[$taxonomy] = $label; wp_stream_get_instance()->connectors->term_labels['stream_context'][$taxonomy] = $label; }
/** * Log handler * * @param string $message sprintf-ready error message string * @param array $args sprintf (and extra) arguments to use * @param int $object_id Target object id * @param string $context Context of the event * @param string $action Action of the event * @param int $user_id User responsible for the event * * @return bool */ public function log($message, $args, $object_id, $context, $action, $user_id = null) { $class = get_called_class(); $connector = str_replace(array('WP_Stream\\', 'Connector_'), array('', ''), $class); $data = apply_filters('wp_stream_log_data', compact('connector', 'message', 'args', 'object_id', 'context', 'action', 'user_id')); if (!$data) { return false; } else { $connector = $data['connector']; $message = $data['message']; $args = $data['args']; $object_id = $data['object_id']; $context = $data['context']; $action = $data['action']; $user_id = $data['user_id']; } return call_user_func_array(array(wp_stream_get_instance()->log, 'log'), compact('connector', 'message', 'args', 'object_id', 'context', 'action', 'user_id')); }
/** * Checks for a Stream connection and displays an error or success message. * * @return void */ private function connection() { $query = wp_stream_get_instance()->db->query->query(array('records_per_page' => 1, 'fields' => 'created')); if (!$query) { WP_CLI::error(esc_html__('SITE IS DISCONNECTED', 'stream')); } }
/** * Add action links to Stream drop row in admin list screen * * @filter wp_stream_action_links_{connector} * * @param array $links Previous links registered * @param Record $record Stream record * * @return array Action links */ public function action_links($links, $record) { $context_labels = $this->get_context_labels(); $plugin = wp_stream_get_instance(); $rules = array('stream' => array('menu_slug' => 'wp_stream', 'submenu_slug' => $plugin->admin->settings_page_slug, 'url' => function ($rule, $record) use($plugin) { $option_key = $record->get_meta('option_key', true); $url_tab = null; if ('' !== $option_key) { foreach ($plugin->settings->get_fields() as $tab_name => $tab_properties) { foreach ($tab_properties['fields'] as $field) { $field_key = sprintf('%s_%s', $tab_name, $field['name']); if ($field_key === $option_key) { $url_tab = $tab_name; break 2; } } } } return add_query_arg(array('page' => $rule['submenu_slug'], 'tab' => $url_tab), admin_url('admin.php')); }, 'applicable' => function ($submenu, $record) { return $record->context === 'wp_stream'; }), 'background_header' => array('menu_slug' => 'themes.php', 'submenu_slug' => function ($record) { return str_replace('_', '-', $record->context); }, 'url' => function ($rule, $record) { return add_query_arg('page', $rule['submenu_slug']($record), admin_url($rule['menu_slug'])); }, 'applicable' => function ($submenu, $record) { return in_array($record->context, array('custom_header', 'custom_background')); }), 'general' => array('menu_slug' => 'options-general.php', 'submenu_slug' => function ($record) { return sprintf('options-%s.php', $record->context); }, 'url' => function ($rule, $record) { return admin_url($rule['submenu_slug']($record)); }, 'applicable' => function ($submenu, $record) { return !empty($submenu['options-general.php']); }), 'network' => array('menu_slug' => 'settings.php', 'submenu_slug' => function ($record) { return 'settings.php'; }, 'url' => function ($rule, $record) { return network_admin_url($rule['menu_slug']); }, 'applicable' => function ($submenu, $record) { if (!$record->blog_id) { return !empty($submenu['settings.php']); } return false; })); if ('settings' !== $record->context && in_array($record->context, array_keys($context_labels))) { global $submenu; $applicable_rules = array_filter($rules, function ($rule) use($submenu, $record) { return call_user_func($rule['applicable'], $submenu, $record); }); if (!empty($applicable_rules)) { // The first applicable rule wins $rule = array_shift($applicable_rules); $menu_slug = $rule['menu_slug']; $submenu_slug = is_object($rule['submenu_slug']) && $rule['submenu_slug'] instanceof Closure ? $rule['submenu_slug']($record) : $rule['submenu_slug']; $url = $rule['url']($rule, $record); if (isset($submenu[$menu_slug])) { $found_submenus = wp_list_filter($submenu[$menu_slug], array(2 => $submenu_slug)); } if (!empty($found_submenus)) { $target_submenu = array_pop($found_submenus); list($menu_title, $capability) = $target_submenu; if (current_user_can($capability)) { $url = apply_filters('wp_stream_action_link_url', $url, $record); $text = sprintf(esc_html__('Edit %s Settings', 'stream'), $context_labels[$record->context]); $field_name = $record->get_meta('option_key', true); if ('' === $field_name) { $field_name = $record->get_meta('option', true); } if ('' !== $field_name) { $url = sprintf('%s#%s%s', rtrim(preg_replace('/#.*/', '', $url), '/'), self::HIGHLIGHT_FIELD_URL_HASH_PREFIX, $field_name); } $links[$text] = $url; } } } } return $links; }
/** * Tracks comment creation * * @action wp_insert_comment * * @param int $comment_id * @param object $comment */ public function callback_wp_insert_comment($comment_id, $comment) { if (in_array($comment->comment_type, $this->get_ignored_comment_types())) { return; } $user_id = $this->get_comment_author($comment, 'id'); $user_name = $this->get_comment_author($comment, 'name'); $post_id = $comment->comment_post_ID; $post_type = get_post_type($post_id); $post_title = ($post = get_post($post_id)) ? "\"{$post->post_title}\"" : esc_html__('a post', 'stream'); $comment_status = 1 === $comment->comment_approved ? esc_html__('approved automatically', 'stream') : esc_html__('pending approval', 'stream'); $is_spam = false; // Auto-marked spam comments $options = wp_stream_get_instance()->settings->options; $ak_tracking = isset($options['advanced_akismet_tracking']) ? $options['advanced_akismet_tracking'] : false; if (class_exists('Akismet') && $ak_tracking && \Akismet::matches_last_comment($comment)) { $ak_last_comment = \Akismet::get_last_comment(); if ('true' === $ak_last_comment['akismet_result']) { $is_spam = true; $comment_status = esc_html__('automatically marked as spam by Akismet', 'stream'); } } $comment_type = mb_strtolower($this->get_comment_type_label($comment_id)); if ($comment->comment_parent) { $parent_user_id = get_comment_author($comment->comment_parent, 'id'); $parent_user_name = get_comment_author($comment->comment_parent, 'name'); $this->log(_x('Reply to %1$s\'s %5$s by %2$s on %3$s %4$s', "1: Parent comment's author, 2: Comment author, 3: Post title, 4: Comment status, 5: Comment type", 'stream'), compact('parent_user_name', 'user_name', 'post_title', 'comment_status', 'comment_type', 'post_id', 'parent_user_id'), $comment_id, $post_type, 'replied', $user_id); } else { $this->log(_x('New %4$s by %1$s on %2$s %3$s', '1: Comment author, 2: Post title 3: Comment status, 4: Comment type', 'stream'), compact('user_name', 'post_title', 'comment_status', 'comment_type', 'post_id', 'is_spam'), $comment_id, $post_type, $is_spam ? 'spammed' : 'created', $user_id); } }
<?php namespace WP_Stream; // Load Carbon to Handle dates much easier if (!class_exists('Carbon\\Carbon')) { require_once wp_stream_get_instance()->locations['inc_dir'] . 'lib/Carbon.php'; } use Carbon\Carbon; class Date_Interval { /** * Contains an array of all available intervals * * @var array $intervals */ public $intervals; /** * Class constructor */ public function __construct() { // Get all default intervals $this->intervals = $this->get_predefined_intervals(); } /** * @return mixed|void */ public function get_predefined_intervals() { $timezone = get_option('timezone_string');
public function admin_enqueue_scripts($hook) { if (0 === strpos($hook, 'seo_page_')) { $stream = wp_stream_get_instance(); $src = $stream->locations['url'] . '/ui/js/wpseo-admin.js'; wp_enqueue_script('stream-connector-wpseo', $src, array('jquery'), $stream->get_version()); } }
public function get_stream() { // Filters $allowed_params = array('connector', 'context', 'action', 'author', 'author_role', 'object_id', 'search', 'date', 'date_from', 'date_to', 'record__in', 'blog_id', 'ip'); $sections = isset($_POST['sections']) ? maybe_unserialize(base64_decode($_POST['sections'])) : array(); if (!is_array($sections)) { $sections = array(); } //return $sections; $other_tokens = isset($_POST['other_tokens']) ? maybe_unserialize(base64_decode($_POST['other_tokens'])) : array(); if (!is_array($other_tokens)) { $other_tokens = array(); } //return $other_tokens; unset($_POST['sections']); unset($_POST['other_tokens']); $args = array(); foreach ($allowed_params as $param) { if (self::$mainwpChildReports) { $paramval = mainwp_wp_stream_filter_input(INPUT_POST, $param); } else { $paramval = wp_stream_filter_input(INPUT_POST, $param); } if ($paramval || '0' === $paramval) { $args[$param] = $paramval; } } foreach ($args as $arg => $val) { if (!in_array($arg, $allowed_params)) { unset($args[$arg]); } } // to fix bug $exclude_connector_posts = true; if (isset($sections['body']) && isset($sections['body']['section_token']) && is_array($sections['body']['section_token'])) { foreach ($sections['body']['section_token'] as $sec) { if (strpos($sec, "[section.posts") !== false) { $exclude_connector_posts = false; break; } } } if ($exclude_connector_posts) { if (isset($sections['header']) && isset($sections['header']['section_token']) && is_array($sections['header']['section_token'])) { foreach ($sections['header']['section_token'] as $sec) { if (strpos($sec, "[section.posts") !== false) { $exclude_connector_posts = false; break; } } } } if ($exclude_connector_posts) { if (isset($sections['footer']) && isset($sections['footer']['section_token']) && is_array($sections['footer']['section_token'])) { foreach ($sections['footer']['section_token'] as $sec) { if (strpos($sec, "[section.posts") !== false) { $exclude_connector_posts = false; break; } } } } if ($exclude_connector_posts) { if (isset($other_tokens['body']) && is_array($other_tokens['body'])) { foreach ($other_tokens['body'] as $sec) { if (strpos($sec, "[post.") !== false) { $exclude_connector_posts = false; break; } } } } if ($exclude_connector_posts) { if (isset($other_tokens['header']) && is_array($other_tokens['header'])) { foreach ($other_tokens['header'] as $sec) { if (strpos($sec, "[post.") !== false) { $exclude_connector_posts = false; break; } } } } if ($exclude_connector_posts) { if (isset($other_tokens['footer']) && is_array($other_tokens['footer'])) { foreach ($other_tokens['footer'] as $sec) { if (strpos($sec, "[post.") !== false) { $exclude_connector_posts = false; break; } } } } if ($exclude_connector_posts) { $args['connector__not_in'] = array('posts'); } ///// end fix ///// $args['action__not_in'] = array('login'); // fix for Stream 3 if (3 !== self::$streamVersionNumber) { $args['fields'] = 'with-meta'; if (isset($args['date_from'])) { $args['date_from'] = date('Y-m-d H:i:s', $args['date_from']); } if (isset($args['date_to'])) { $args['date_to'] = date('Y-m-d H:i:s', $args['date_to']); } } else { if (isset($args['date_from'])) { $args['date_from'] = date('Y-m-d', $args['date_from']); } if (isset($args['date_to'])) { $args['date_to'] = date('Y-m-d', $args['date_to']); } } $args['records_per_page'] = 9999; // error_log(print_r($args, true)); if (self::$mainwpChildReports) { $records = mainwp_wp_stream_query($args); } else { if (149 === self::$streamVersionNumber) { $records = wp_stream_query($args); } else { if (3 === self::$streamVersionNumber) { $records = wp_stream_get_instance()->db->query->query($args); } } } if (!is_array($records)) { $records = array(); } //return $records; //$other_tokens_data = $this->get_other_tokens_data($records, $other_tokens); if (isset($other_tokens['header']) && is_array($other_tokens['header'])) { $other_tokens_data['header'] = $this->get_other_tokens_data($records, $other_tokens['header']); } if (isset($other_tokens['body']) && is_array($other_tokens['body'])) { $other_tokens_data['body'] = $this->get_other_tokens_data($records, $other_tokens['body']); } if (isset($other_tokens['footer']) && is_array($other_tokens['footer'])) { $other_tokens_data['footer'] = $this->get_other_tokens_data($records, $other_tokens['footer']); } $sections_data = array(); if (isset($sections['header']) && is_array($sections['header']) && !empty($sections['header'])) { foreach ($sections['header']['section_token'] as $index => $sec) { $tokens = $sections['header']['section_content_tokens'][$index]; $sections_data['header'][$index] = $this->get_section_loop_data($records, $tokens, $sec); } } if (isset($sections['body']) && is_array($sections['body']) && !empty($sections['body'])) { foreach ($sections['body']['section_token'] as $index => $sec) { $tokens = $sections['body']['section_content_tokens'][$index]; $sections_data['body'][$index] = $this->get_section_loop_data($records, $tokens, $sec); } } if (isset($sections['footer']) && is_array($sections['footer']) && !empty($sections['footer'])) { foreach ($sections['footer'] as $index => $sec) { $tokens = $sections['footer']['section_content_tokens'][$index]; $sections_data['footer'][$index] = $this->get_section_loop_data($records, $tokens, $sec); } } $information = array('other_tokens_data' => $other_tokens_data, 'sections_data' => $sections_data); return $information; }
/** * Log handler * * @param string $message sprintf-ready error message string * @param array $args sprintf (and extra) arguments to use * @param int $object_id Target object id * @param string $context Context of the event * @param string $action Action of the event * @param int $user_id User responsible for the event * * @return bool */ public function log($message, $args, $object_id, $context, $action, $user_id = null) { $connector = $this->name; $data = apply_filters('wp_stream_log_data', compact('connector', 'message', 'args', 'object_id', 'context', 'action', 'user_id')); if (!$data) { return false; } else { $connector = $data['connector']; $message = $data['message']; $args = $data['args']; $object_id = $data['object_id']; $context = $data['context']; $action = $data['action']; $user_id = $data['user_id']; } return call_user_func_array(array(wp_stream_get_instance()->log, 'log'), compact('connector', 'message', 'args', 'object_id', 'context', 'action', 'user_id')); }
/** * Return a Gravatar image as an HTML element. * * This function will not return an avatar if "Show Avatars" is unchecked in Settings > Discussion. * * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80 * * @return string|bool An img HTML element, or false if avatars are disabled */ function get_avatar_img($size = 80) { if (!get_option('show_avatars')) { return false; } if (0 === $this->id) { $stream = wp_stream_get_instance(); $url = $stream->locations['url'] . 'ui/stream-icons/wp-cli.png'; $avatar = sprintf('<img alt="%1$s" src="%2$s" class="avatar avatar-%3$s photo" height="%3$s" width="%3$s">', esc_attr($this->get_display_name()), esc_url($url), esc_attr($size)); } else { if ($this->is_deleted() && isset($this->meta['user_email'])) { $email = $this->meta['user_email']; $avatar = get_avatar($email, $size); } else { $avatar = get_avatar($this->id, $size); } } return $avatar; }
/** * Helper function to query the marketplace API via wp_remote_request. * * @param string The url to access. * @param string The method of the request. * @param array The headers sent during the request. * @param bool Allow API calls to be cached. * @param int Set transient expiration in seconds. * * @return object The results of the wp_remote_request request. */ protected function remote_request($url = '', $args = array(), $allow_cache = true, $expiration = 300) { if (empty($url) || empty(get_option('api_key'))) { return false; } // error_log("url: ".json_encode($url)); $defaults = array('headers' => array(), 'method' => 'GET', 'body' => '', 'sslverify' => true); $args = wp_parse_args($args, $defaults); $args['headers']['Stream-Site-API-Key'] = get_option('api_key'); $args['headers']['Content-Type'] = 'application/json'; add_filter('http_api_transports', array(__CLASS__, 'http_api_transport_priority'), 10, 3); $transient = 'wp_stream_' . md5($url); // error_log("args:".json_encode($args)); // error_log("url:".$url); if ('GET' === $args['method'] && $allow_cache) { if (false === ($request = get_transient($transient))) { $request = wp_remote_request($url, $args); set_transient($transient, $request, $expiration); } } else { $request = wp_remote_request($url, $args); } remove_filter('http_api_transports', array(__CLASS__, 'http_api_transport_priority'), 10); // Return early if the request is non blocking if (isset($args['blocking']) && false === $args['blocking']) { return true; } if (!is_wp_error($request)) { /** * Filter the request data of the API response. * * Does not fire on non-blocking requests. * * @since 2.0.0 * * @param string $url * @param array $args * * @return array */ $data = apply_filters('wp_stream_api_request_data', json_decode($request['body']), $url, $args); // Loose comparison needed if (200 == $request['response']['code'] || 201 == $request['response']['code']) { return $data; } else { // Disconnect if unauthorized or no longer exists, loose comparison needed if (403 == $request['response']['code'] || 410 == $request['response']['code']) { WP_Stream_Admin::remove_api_authentication(); } $this->errors['errors']['http_code'] = $request['response']['code']; } if (isset($data->error)) { $this->errors['errors']['api_error'] = $data->error; } } else { $this->errors['errors']['remote_request_error'] = $request->get_error_message(); wp_stream_get_instance()->admin->notice(sprintf('<strong>%s</strong> %s.', __('Stream API Error.', 'stream'), $this->errors['errors']['remote_request_error'])); } if (!empty($this->errors)) { delete_transient($transient); } return false; }