/** * Displays the broadcast options form. * * @return void */ public function action_options() { $post = get_post($this->request->post('post_ID')); if ($post === null or get_post_meta($post->ID, '_social_notify', true) != '1') { if ($post === null) { wp_redirect(admin_url('index.php')); } return; } $errors = array(); $services = $this->social->services(); $accounts_selected = false; if ($this->request->post('social_action') !== null) { // this is coming from $_POST unmodified, WP normalizes with slashes - strip them. $service_accounts = stripslashes_deep($this->request->post('social_accounts')); $account_content = stripslashes_deep($this->request->post('social_account_content')); $account_content_meta = $account_service_meta = array(); foreach ($services as $key => $service) { if (count($service->accounts())) { if (isset($service_accounts[$key])) { $accounts_selected = true; foreach ($service_accounts[$key] as $account_id) { $account_id = explode('|', $account_id); if (empty($account_content[$key][$account_id[0]])) { if (isset($errors[$key])) { $errors[$key] = array(); } $errors[$key][$account_id[0]] = __('Please enter content to be broadcasted.', 'social'); } else { $account_content[$key][$account_id[0]] = $account_content[$key][$account_id[0]]; if (social_strlen($account_content[$key][$account_id[0]]) > $service->max_broadcast_length()) { $errors[$key][$account_id[0]] = sprintf(__('Content must not be longer than %s characters.', 'social'), $service->max_broadcast_length()); } else { if (!isset($account_content_meta[$key])) { $account_content_meta[$key] = $account_service_meta[$key] = array(); } $account_content_meta[$key][$account_id[0]] = $account_content[$key][$account_id[0]]; $account_service_meta[$key][$account_id[0]] = $service->get_broadcast_extras($account_id[0], $post); } } } } // TODO - refactor this to make it more efficient (and, as below to move into Facebook plugin) if ($key == 'facebook') { $pages = $this->request->post('social_facebook_pages'); if (!empty($pages) && is_array($pages) && count($pages)) { foreach ($service->accounts() as $account) { if (isset($pages[$account->id()])) { $accounts_selected = true; foreach ($pages[$account->id()] as $page_id) { if (empty($account_content[$key][$page_id])) { if (isset($errors[$key])) { $errors[$key] = array(); } $errors[$key][$page_id] = __('Please enter content to be broadcasted.', 'social'); } else { $account_content[$key][$page_id] = $account_content[$key][$page_id]; if (social_strlen($account_content[$key][$page_id]) > $service->max_broadcast_length()) { $errors[$key][$page_id] = sprintf(__('Content must not be longer than %s characters.', 'social'), $service->max_broadcast_length()); } else { if (!isset($account_content_meta[$key])) { $account_content_meta[$key] = array(); } $account_content_meta[$key][$page_id] = $account_content[$key][$page_id]; $account_service_meta[$key][$page_id] = $service->get_broadcast_extras($page_id, $post); } } } } } } } } } if (!in_array($post->post_status, array('future', 'pending')) and !$accounts_selected and !isset($errors['rebroadcast'])) { $errors['rebroadcast'] = __('Please select at least one account to broadcast to.', 'social'); } if (!count($errors)) { $broadcast_accounts = array(); foreach ($services as $key => $service) { if (isset($service_accounts[$key])) { $accounts = array(); foreach ($service_accounts[$key] as $account) { $account = explode('|', $account); $accounts[$account[0]] = (object) array('id' => $account[0], 'universal' => isset($account[1])); } if (!empty($accounts)) { $broadcast_accounts[$key] = $accounts; } } // TODO abstract to Facebook plugin. if ($key == 'facebook') { $pages = $this->request->post('social_facebook_pages'); if (is_array($pages)) { foreach ($service->accounts() as $account) { if (isset($pages[$account->id()])) { // TODO This could use some DRY love $universal_pages = $account->pages(); $personal_pages = $account->pages(null, true); foreach ($pages[$account->id()] as $page_id) { if (!isset($broadcast_accounts[$key])) { $broadcast_accounts[$key] = array(); } if (!isset($broadcast_accounts[$key][$page_id])) { if (isset($universal_pages[$page_id])) { $broadcast_accounts[$key][$page_id] = (object) array('id' => $page_id, 'name' => $universal_pages[$page_id]->name, 'universal' => true, 'page' => true); } else { if (isset($personal_pages[$page_id])) { $broadcast_accounts[$key][$page_id] = (object) array('id' => $page_id, 'name' => $personal_pages[$page_id]->name, 'universal' => false, 'page' => true); } } } } } } } } } // Store the content, add slashes since update_post_meta() strips them update_post_meta($post->ID, '_social_broadcast_content', addslashes_deep($account_content_meta)); update_post_meta($post->ID, '_social_broadcast_meta', addslashes_deep($account_service_meta)); update_post_meta($post->ID, '_social_broadcast_accounts', addslashes_deep($broadcast_accounts)); if (!in_array($this->request->post('social_action'), array('Schedule', 'Update'))) { $this->action_run($post); } $location = $this->request->post('location'); if ($location == null) { $location = get_edit_post_link($post->ID, false); } wp_redirect($location); exit; } } $step = 'Publish'; $step_text = __('Publish', 'social'); if ($this->request->post('social_broadcast') !== null or $this->request->post('social_action') !== null) { if ($this->request->post('social_action') === null and $this->request->post('social_broadcast') == 'Edit' or $this->request->post('social_action') == 'Update') { $step = 'Update'; $step_text = __('Update', 'social'); } else { if ($this->request->post('social_action') != 'Publish') { $step = 'Broadcast'; $step_text = __('Broadcast', 'social'); } } } else { if ($post->post_status == 'future' or $this->request->post('publish') == 'Schedule') { $step = 'Schedule'; $step_text = __('Schedule', 'social'); } } // find previous broadcasts $broadcasted_ids = get_post_meta($post->ID, '_social_broadcasted_ids', true); if (empty($broadcasted_ids)) { $broadcasted_ids = array(); } // consolidate accounts (merge child accounts into single list) $_services = array(); // the data we prep for the view will be in here $_defaults = array('name' => '', 'avatar' => '', 'field_name_content' => '', 'field_name_checked' => '', 'field_value_checked' => '', 'checked' => false, 'content' => '', 'broadcasts' => array(), 'error' => ''); foreach ($services as $key => $service) { $_services[$key] = array(); if (isset($broadcasted_ids[$key])) { $service_broadcasts = $broadcasted_ids[$key]; } else { $service_broadcasts = array(); } $accounts = $service->accounts(); if (count($accounts)) { $i = 0; foreach ($accounts as $account) { $data = array('name' => $account->name(), 'avatar' => $account->avatar(), 'field_name_content' => 'social_account_content[' . $key . '][' . $account->id() . ']', 'field_name_checked' => 'social_accounts[' . $key . '][]', 'field_value_checked' => $account->id() . ($account->universal() ? '|true' : ''), 'edit' => false, 'maxlength' => $service->max_broadcast_length()); if ($i == 0) { $data['edit'] = true; } $i++; // assign previous broadcasts if (isset($broadcasted_ids[$key]) && isset($broadcasted_ids[$key][$account->id()])) { foreach ($broadcasted_ids[$key][$account->id()] as $broadcasted_id => $broadcast) { // TODO - shouldn't need to do Facebook specific checks here if ($key == 'facebook' && isset($broadcast['page']) && !empty($broadcast['page']->id)) { // this was broadcast to a page, don't attach it here. continue; } $data['broadcasts'][] = $this->previous_broadcast_excerpt($broadcasted_id, $broadcast, $account, $service); // already broadcasted? not editable by default $data['edit'] = false; } } // assign errors if (isset($errors[$key]) && isset($errors[$key][$account->id()])) { $data['error'] = $errors[$key][$account->id()]; } $_services[$key][$account->id()] = array_merge($_defaults, $data); foreach ($account->child_accounts() as $child_account) { $data = array('name' => $child_account->name, 'avatar' => $service->page_image_url($child_account), 'field_name_content' => 'social_account_content[' . $key . '][' . $child_account->id . ']', 'field_name_checked' => 'social_facebook_pages[' . $account->id() . '][]', 'field_value_checked' => $child_account->id, 'edit' => false, 'maxlength' => $service->max_broadcast_length()); // assign previous broadcasts if (isset($broadcasted_ids[$key]) && isset($broadcasted_ids[$key][$account->id()])) { foreach ($broadcasted_ids[$key][$account->id()] as $broadcasted_id => $broadcast) { // TODO - shouldn't need to do Facebook specific checks here if ($key == 'facebook' && isset($broadcast['page']) && !empty($broadcast['page']->id) && $broadcast['page']->id == $child_account->id) { $data['broadcasts'][] = $this->previous_broadcast_excerpt($broadcasted_id, $broadcast, $account, $service); // already broadcasted? not editable by default $data['edit'] = false; } } } // assign errors if (isset($errors[$key]) && isset($errors[$key][$child_account->id])) { $data['error'] = $errors[$key][$child_account->id]; } $_services[$key][$child_account->id] = array_merge($_defaults, $data); $i++; } } } } $default_accounts = $this->social->default_accounts($post); $broadcast_accounts = array(); $_broadcast_accounts = get_post_meta($post->ID, '_social_broadcast_accounts', true); if (!empty($_broadcast_accounts) and count($_broadcast_accounts)) { foreach ($_broadcast_accounts as $service => $accounts) { foreach ($accounts as $account) { if (!isset($broadcast_accounts[$service])) { $broadcast_accounts[$service] = array(); } $broadcast_accounts[$service][$account->id] = true; } } } $broadcast_content = get_post_meta($post->ID, '_social_broadcast_content', true); if (empty($broadcast_content)) { $broadcast_content = array(); } // check to see if we have any previous broadcasts or saved content $previous_activity = 0; foreach ($_services as $key => $accounts) { $broadcast_default = $services[$key]->format_content($post, Social::option('broadcast_format')); // set content format and checked status for each foreach ($accounts as $id => $data) { $previous_activity += count($data['broadcasts']); // check for error - populate with previouly posted content if (count($errors)) { $content = stripslashes($_POST['social_account_content'][$key][$id]); $checked = isset($_POST['social_accounts']) && isset($_POST['social_accounts'][$key]) && in_array($data['field_value_checked'], $_POST['social_accounts'][$key]); // TODO - Facebook pages check, abstract this if (!$checked && $key == 'facebook') { if (isset($_POST['social_facebook_pages']) && is_array($_POST['social_facebook_pages'])) { foreach ($_POST['social_facebook_pages'] as $account) { if (in_array($data['field_value_checked'], $account)) { $checked = true; break; } } } } } else { $content = $broadcast_default; $checked = isset($default_accounts[$key]) && in_array($id, $default_accounts[$key]); // TODO - abstract this // check to see if a facebook page should be selected by default // our data structure currently makes this awkward if (!$checked && $key == 'facebook' && isset($default_accounts[$key]) && isset($default_accounts[$key]['pages']) && is_array($default_accounts[$key]['pages'])) { foreach ($default_accounts[$key]['pages'] as $page_parent_account) { if (in_array($id, $page_parent_account)) { $checked = true; break; } } } // check for saved broadcast if (isset($broadcast_content[$key]) && isset($broadcast_content[$key][$id])) { $content = $broadcast_content[$key][$id]; $previous_activity++; } if (count($broadcast_accounts) && isset($broadcast_accounts[$key]) && isset($broadcast_accounts[$key][$id])) { $checked = true; } if (count($data['broadcasts'])) { $checked = false; } // override for scheduled broadcasts if ($post->post_status == 'future') { $checked = count($broadcast_accounts) && isset($broadcast_accounts[$key]) && isset($broadcast_accounts[$key][$id]); } } $_services[$key][$id]['content'] = $content; $_services[$key][$id]['checked'] = $checked; if ($content != $broadcast_default) { $_services[$key][$id]['edit'] = true; } } } echo Social_View::factory('wp-admin/post/broadcast/options', array('clean' => $previous_activity ? '' : 'clean', 'services' => $services, '_services' => $_services, 'post' => $post, 'step' => $step, 'step_text' => $step_text, 'location' => $this->request->post('location'))); exit; }
/** * Formats a comment before it's broadcasted. * * @param WP_Comment $comment * @param array $format * @return string */ public function format_comment_content($comment, $format) { // Filter the format $format = apply_filters('social_comment_broadcast_format', $format, $comment, $this); $_format = $format; $available = $this->max_broadcast_length(); $used_tokens = array(); // Gather used tokens and subtract remaining characters from available length foreach (Social::comment_broadcast_tokens() as $token => $description) { $replaced = 0; $_format = str_replace($token, '', $_format, $replaced); if ($replaced) { $used_tokens[$token] = ''; } } $available = $available - social_strlen($_format); // Prep token replacement content foreach ($used_tokens as $token => $content) { switch ($token) { case '{url}': $url = social_get_shortlink($comment->comment_post_ID); if (empty($url)) { $url = home_url('?p=' . $comment->comment_post_ID); } $url .= '#comment-' . $comment->comment_ID; $url = apply_filters('social_comment_broadcast_permalink', $url, $comment, $this); $used_tokens[$token] = esc_url($url); break; case '{content}': $used_tokens[$token] = strip_tags($comment->comment_content); $used_tokens[$token] = str_replace(' ', '', $used_tokens[$token]); break; } } // if {url} is used, pre-allocate its length if (isset($used_tokens['{url}'])) { $available = $available - social_strlen($used_tokens['{url}']); } $used_tokens['{content}'] = apply_filters('social_format_comment_content', $used_tokens['{content}'], $comment, $format, $this); // Truncate content to size limit if (social_strlen($used_tokens['{content}']) > $available) { $used_tokens['{content}'] = social_substr($used_tokens['{content}'], 0, $available - 3) . '...'; } foreach ($used_tokens as $token => $replacement) { if (strpos($format, $token) !== false) { $format = str_replace($token, $replacement, $format); } } $format = apply_filters('social_comment_broadcast_content_formatted', $format, $comment, $this); return $format; }