/** * When the site's URL changes, automatically inform * the LivePress API of the change. */ function livepress_update_blog_name() { $settings = get_option('livepress'); $api_key = isset($settings['api_key']) ? $settings['api_key'] : ''; $comm = new LivePress_Communication($api_key); // Note: site_url is the admin url on VIP $comm->validate_on_livepress(site_url()); }
/** * Send an update (add/change/delete) to LivePress' API * * @param string $op Operation (append/prepend/replace/delete) * @param WP_Post $post Parent post object * @param WP_Post $update Update piece object * * @return array[] $region Object to send to editor */ protected function send_to_livepress_incremental_post_update($op, $post, $update, $update_meta) { if (!is_object($post)) { $post = get_post($post); } if (!is_object($update)) { $update = get_post($update); } // FIXME: may be better use $update->post_author there ? $user = wp_get_current_user(); if ($user->ID) { if (empty($user->display_name)) { $update_author = addslashes($user->user_login); } $update_author = addslashes($user->display_name); } else { $update_author = ''; } list($_, $piece_id, $piece_gen) = explode('__', $update->post_title, 3); global $wp_filter; // Remove all the_content filters so child posts are not filtered // removing share, vote and other per-post items from the live update stream. // Store the filters first for later restoration so filters still fire outside the update stream $stored_wp_filter_the_content = $wp_filter; $this->clear_most_the_content_filters(); $region = array('id' => $piece_id, 'lpg' => $piece_gen, 'op' => $op, 'content' => $update->post_content, 'proceed' => do_shortcode(apply_filters('the_content', $update->post_content)), 'prefix' => sprintf('<div id="livepress-update-%s" data-lpg="%d" class="livepress-update">', $piece_id, $piece_gen), 'suffix' => '</div>'); // Restore the_content filters and carry on $wp_filter = $stored_wp_filter_the_content; $message = array('op' => $op, 'post_id' => $post->ID, 'post_title' => $post->post_title, 'post_link' => get_permalink($post->ID), 'post_author' => $update_author, 'update_id' => 'livepress-update-' . $piece_id, 'updated_at' => get_gmt_from_date(current_time('mysql')) . 'Z', 'uuid' => $piece_id . ':' . $piece_gen, 'edit' => json_encode($region)); if ($op == 'replace') { $message['new_data'] = $region['prefix'] . $region['proceed'] . $region['suffix']; } elseif ($op == 'delete') { $region['content'] = ''; // remove content from update for delete $region['proceed'] = ''; } else { $message['data'] = $region['prefix'] . $region['proceed'] . $region['suffix']; } if (true !== $update_meta['draft']) { try { $job_uuid = $this->lp_comm->send_to_livepress_incremental_post_update($op, $message); LivePress_WP_Utils::save_on_post($post->ID, 'status_uuid', $job_uuid); } catch (livepress_communication_exception $e) { $e->log('incremental post update'); } } // Set the parent post as having been updated $status = array('automatic' => 1, 'live' => 1); update_post_meta($post->ID, '_livepress_live_status', $status); $region['status'] = $status; // add meta to the child update update_post_meta($update->ID, '_livepress_update_meta', $update_meta); $region['update_meta'] = $update_meta; return $region; }
/** * Sanitize form data. * * @param array $input Raw form input. * @return array Sanitized form input. */ function sanitize($input) { $sanitized_input = array(); if (isset($input['api_key'])) { $api_key = sanitize_text_field($input['api_key']); if (!empty($input['api_key'])) { $livepress_com = new LivePress_Communication($api_key); // Note: site_url is the admin url on VIP $validation = $livepress_com->validate_on_livepress(site_url()); $sanitized_input['api_key'] = $api_key; $sanitized_input['error_api_key'] = $validation != 1; if ($validation == 1) { // We pass validation, update blog parameters from LP side $blog = $livepress_com->get_blog(); $sanitized_input['blog_shortname'] = isset($blog->shortname) ? $blog->shortname : ''; $sanitized_input['post_from_twitter_username'] = isset($blog->twitter_username) ? $blog->twitter_username : ''; $sanitized_input['api_key'] = $api_key; } else { add_settings_error('api_key', 'invalid', esc_html__('Key is not valid', 'livepress')); } } else { $sanitized_input['api_key'] = $api_key; } } if (isset($input['feed_order']) && $input['feed_order'] == 'bottom') { $sanitized_input['feed_order'] = 'bottom'; } else { $sanitized_input['feed_order'] = 'top'; } if (isset($input['timestamp_format']) && $input['timestamp_format'] == 'timeof') { $sanitized_input['timestamp_format'] = 'timeof'; } else { $sanitized_input['timestamp_format'] = 'timeago'; } if (isset($input['update_format']) && $input['update_format'] == 'newstyle') { $sanitized_input['update_format'] = 'newstyle'; } else { $sanitized_input['update_format'] = 'default'; } if (isset($input['show']) && !empty($input['show'])) { $sanitized_input['show'] = array_map('sanitize_text_field', $input['show']); } else { $sanitized_input['show'] = array(); } if (isset($input['notifications']) && !empty($input['notifications'])) { $sanitized_input['notifications'] = array_map('sanitize_text_field', $input['notifications']); } else { $sanitized_input['notifications'] = array(); } if (isset($input['allow_remote_twitter'])) { $sanitized_input['allow_remote_twitter'] = 'allow'; } else { $sanitized_input['allow_remote_twitter'] = 'deny'; } if (isset($input['oauth_authorized_user'])) { $sanitized_input['oauth_authorized_user'] = sanitize_text_field($input['oauth_authorized_user']); } if (isset($input['allow_sms'])) { $sanitized_input['allow_sms'] = 'allow'; } else { $sanitized_input['allow_sms'] = 'deny'; } if (isset($input['post_to_twitter'])) { $sanitized_input['post_to_twitter'] = (bool) $input['post_to_twitter']; } if (isset($input['sharing_ui'])) { $sanitized_input['sharing_ui'] = 'display'; } else { $sanitized_input['sharing_ui'] = 'dont_display'; } if (isset($input['facebook_app_id'])) { $sanitized_input['facebook_app_id'] = sanitize_text_field($input['facebook_app_id']); } else { $sanitized_input['facebook_app_id'] = ''; } $merged_input = wp_parse_args($sanitized_input, (array) $this->settings); // For the settings not exposed return $merged_input; }
/** * Save the user settings. * * @author tddewey * * @param $user_id integer user ID being updated. * * @return integer returns the $user_id regardless of success or failure. */ function save_fields($user_id) { // Check permissions and nonce. if (!current_user_can('edit_user', $user_id) && wp_verify_nonce($_POST['_wpnonce'])) { return $user_id; } $old_lp_twitter = get_user_meta($user_id, 'lp_twitter', true); // returns empty string if not set, perfect $lp_twitter = isset($_POST['lp_twitter']) ? sanitize_text_field($_POST['lp_twitter']) : ''; $lp_user_password = isset($_POST['lp_user_password']) ? sanitize_text_field($_POST['lp_user_password']) : ''; if ($old_lp_twitter != $lp_twitter || '' !== $lp_user_password) { $user = get_userdata($user_id); $options = get_option(LivePress_Administration::$options_name); $livepress_com = new LivePress_Communication($options['api_key']); $error_message = ''; try { $return_code = $livepress_com->manage_remote_post_from_twitter($lp_twitter, $user->user_login); } catch (LivePress_Communication_Exception $e) { $return_code = $e->get_code(); $error_message = $e->getMessage(); } /* User not found at livepress, or password invalid, or WordPress password entered */ if ($return_code == 403 || '' !== $lp_user_password) { $la = new LivePress_Administration(); $error_message = $livepress_com->get_last_error_message(); if (!$la->enable_remote_post($user_id, $lp_user_password)) { $this->add_error('Error from the LivePress service: ', $error_message); } else { $error_message = ''; try { $return_code = $livepress_com->manage_remote_post_from_twitter($lp_twitter, $user->user_login); } catch (LivePress_Communication_Exception $e) { $return_code = $e->get_code(); $error_message = $e->getMessage(); } } } if ($return_code != 200 && $return_code != 'OK.') { if (strlen($error_message) > 0) { $this->add_error('Problem with setting twitter user: '******'Problem with setting twitter user: error #' . $return_code); // Show error code if no msg } } else { // No error if (empty($lp_twitter)) { // None left, delete meta delete_user_meta($user_id, 'lp_twitter'); } else { // Update meta with current lp_twitter update_user_meta($user_id, 'lp_twitter', $lp_twitter); } } } $old_lp_phone_number = get_user_meta($user_id, 'lp_phone_number', true); // returns empty string if not set, perfect $lp_phone_number = isset($_POST['lp_phone_number']) ? sanitize_text_field($_POST['lp_phone_number']) : ''; if ($old_lp_phone_number != $lp_phone_number) { $user = get_userdata($user_id); $options = get_option(LivePress_Administration::$options_name); $livepress_com = new LivePress_Communication($options['api_key']); $error_message = ''; try { $return_code = $livepress_com->set_phone_number($lp_phone_number, $user->user_login); } catch (LivePress_Communication_Exception $e) { $return_code = $e->get_code(); $error_message = $e->getMessage(); } if ($return_code == 403) { /* User not found at livepress, or password invalid */ $la = new LivePress_Administration(); $error_message = $livepress_com->get_last_error_message(); if (!$la->enable_remote_post($user_id, $lp_user_password)) { $this->add_error('Error from the LivePress service: ', $error_message); } else { $error_message = ''; try { $return_code = $livepress_com->set_phone_number($lp_phone_number, $user->user_login); } catch (LivePress_Communication_Exception $e) { $return_code = $e->get_code(); $error_message = $e->getMessage(); } } } if ($return_code != 200 && $return_code != 'OK.') { if (strlen($error_message) > 0) { $this->add_error('Problem with setting phone number: ' . $error_message); // Show existing error } else { $this->add_error('Problem with setting phone number: error #' . $return_code); // Show error code if no msg } } else { // No error if (empty($lp_phone_number)) { // None left, delete meta delete_user_meta($user_id, 'lp_phone_number'); } else { // Update meta with current lp_phone_number update_user_meta($user_id, 'lp_phone_number', $lp_phone_number); } } } return $user_id; }
/** * Print to ajax the status of the last OAuth request. * * @static */ public static function check_oauth_authorization_status() { self::die_if_not_allowed(); check_ajax_referer('lp_check_oauth_nonce'); $options = get_option(self::$options_name); $livepress_com = new LivePress_Communication($options['api_key']); $status = $livepress_com->is_authorized_oauth(); if ($status->status == 'authorized') { $options['post_to_twitter'] = true; $options['oauth_authorized_user'] = $status->username; update_option(self::$options_name, $options); } else { if ($status->status == 'unauthorized') { $options['post_to_twitter'] = false; $options['oauth_authorized_user'] = ''; update_option(self::$options_name, $options); } } header('Content-Type: application/json'); echo json_encode($status); die; }
function lp_update_shortlink() { check_ajax_referer('lp_update_shortlink'); $post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : null; $update_id = isset($_REQUEST['update_id']) ? intval($_REQUEST['update_id']) : null; $cache_key = 'lp_shortlink_aaaa' . LP_PLUGIN_VERSION . '_' . $update_id; $shortlink = LivePress_WP_Utils::get_from_post($post_id, $cache_key, true); $status_code = 200; if (!$shortlink) { global $post; $post = get_post($post_id); $post_parent_url = get_permalink($post); if (0 === preg_match('/\\/\\?/', $post_parent_url)) { $post_parent_url .= '?'; } else { $post_parent_url .= '&'; } $canonical_url = $post_parent_url . 'lpup=' . $update_id . '#livepress-update-' . $update_id; //$bitly_api = '7ea952a9826d091fbda8a4ca220ba634efe61e31'; // TODO: Allow to set up from web page $bitly_api = 'a68d0da03159457bff5f6b287d6cdecb88b108dd'; // datacompboy $get_shortlink = 'https://api-ssl.bitly.com/v3/shorten?access_token=' . $bitly_api . '&domain=bit.ly&longUrl=' . urlencode($canonical_url); $url = $get_shortlink; if (function_exists('vip_safe_wp_remote_get')) { $res = vip_safe_wp_remote_get($url, '', 5, 10, 20, array('reject_unsafe_urls' => false)); } else { $res = wp_remote_get($url, array('reject_unsafe_urls' => false)); } $response = json_decode($res['body'], true); $status_code = $response['status_code']; if (is_wp_error($res) || 200 !== $status_code) { $shortlink = $canonical_url; } else { if (!$res || !isset($response['data']) || !isset($response['data']['url'])) { $shortlink = $canonical_url; $status_code = 500; } else { $shortlink = $response['data']['url']; LivePress_WP_Utils::save_on_post($post_id, $cache_key, $shortlink); $options = get_option(LivePress_Administration::$options_name); $livepress_com = new LivePress_Communication($options['api_key']); $livepress_com->send_to_livepress_broadcast($post_id, array('shortlink' => array($update_id => $shortlink))); } } } wp_send_json_success(array('shortlink' => $shortlink, 'code' => $status_code)); }
private static function tracked_and_followed() { global $current_user; $options = get_option(LivePress_Administration::$options_name); $communication = new LivePress_Communication($options['api_key']); $params = array('post_id' => $_REQUEST['post_id'], 'format' => 'json'); $result = json_decode($communication->followed_tracked_twitter_accounts($params), true); // TODO: Handle errors $result['terms'] = self::get_searches($_REQUEST['post_id'], $current_user->ID, $result['terms']); return $result; }