/** * Formats the broadcast content. * * @param object $post * @param string $format * @return string */ public function format_content($post, $format) { // Filter the format $format = apply_filters('social_broadcast_format', $format, $post, $this); $_format = $format; $available = $this->max_broadcast_length(); foreach (Social::broadcast_tokens() as $token => $description) { $_format = str_replace($token, '', $_format); } $available = $available - strlen($_format); $_format = explode(' ', $format); foreach (Social::broadcast_tokens() as $token => $description) { $content = ''; switch ($token) { case '{url}': $url = wp_get_shortlink($post->ID); if (empty($url)) { $url = home_url('?p=' . $post->ID); } $url = apply_filters('social_broadcast_permalink', $url, $post, $this); $content = esc_url($url); break; case '{title}': $content = htmlspecialchars_decode($post->post_title); break; case '{content}': $content = htmlspecialchars_decode(strip_tags($post->post_content)); $content = str_replace(array("\n", "\r", PHP_EOL, ' '), ' ', $content); $content = preg_replace('/\\s+/', ' ', $content); break; case '{author}': $user = get_userdata($post->post_author); $content = htmlspecialchars_decode($user->display_name); break; case '{date}': $content = get_date_from_gmt($post->post_date_gmt); break; } if (strlen($content) > $available) { if (in_array($token, array('{date}', '{author}'))) { $content = ''; } else { $content = substr($content, 0, $available - 3) . '...'; } } // Filter the content $content = apply_filters('social_format_content', $content, $post, $format, $this); foreach ($_format as $haystack) { if (strpos($haystack, $token) !== false and $available > 0) { $haystack = str_replace($token, $content, $haystack); $available = $available - strlen($haystack); $format = str_replace($token, $content, $format); break; } } } // Filter the content $format = apply_filters('social_broadcast_content_formatted', $format, $post, $this); return $format; }
<input type="text" class="regular-text" name="social_broadcast_format" id="social_broadcast_format" value="<?php echo esc_attr(Social::option('broadcast_format')); ?> " /> <p class="description"><?php _e('How you would like posts to be formatted when broadcasting to Twitter or Facebook?', 'social'); ?> </p> <div class="description"> <?php _e('Tokens:', 'social'); ?> <ul> <?php foreach (Social::broadcast_tokens() as $token => $description) { if (!empty($description)) { $description = ' - ' . $description; } ?> <li><b><?php echo esc_html($token); ?> </b><?php echo esc_html($description); ?> </li> <?php } ?> </ul>