CR-Post2PingFM Plugin Tweaked for WordPress MU

Although this is not strictly related to Second Life itself, I need some place to put a tweaked version of Arief Bayu Purwanto’s CR Post2PingFM plugin, which required some tweaking to work well with WordPress MU.

The problem has to do with setting and reading options properly in a multiblog environment. WordPress and WordPress MU handle this differently, thus the original, untweaked plugin didn’t work properly.

Thanks to Marcus Cudd for helping me to debug it!

You have to enclose it in the usual PHP tags, which this code plugin sadly eats up.

Warning: Beachbum noticed quite correctly that the single quotes in the code get unfortunately converted by WordPress into “typographical quotes”, so you have to be extra-careful when copying & pasting this! Thanks for the tip, Beachbum 🙂

<?php
/*
Plugin Name: CR Post to Ping.fm
Plugin URI: http://bayu.freelancer.web.id/2009/03/31/wordpress-plugin-cr-post2pingfm/
Description: This plugin will submit your new post to ping.fm account.
Version: 0.7.2
Author: Arief Bayu Purwanto, tweaked by Gwyneth Llewelyn to work with WordPress MU 2.8.6
Author URI: http://bayu.freelancer.web.id/
 */

define('API_KEY', '41121eb3a56f921bc2957b2458d65bad');

add_action('publish_post', 'cr_post_2_pingfm_submit_to_ping_fm');
add_action('admin_menu', 'cr_post_2_pingfm_submit_config_admin');
add_action('admin_init', 'cr_post_2_pingfm_settings_api_init');

function cr_post_2_pingfm_settings_api_init()
{
    register_setting('cr_post_2_pingfm_settings', 'cr_post_2_pingfm_options');
}

function cr_post_2_pingfm_submit_to_ping_fm($postId)
{
    $continue = false;
    $categories = array();
    $postMode = get_option('cl_post_pingfm_publish_mode', false);
    $republish = false;

    if(!wp_is_post_revision($postId))
    {
        $this_post_submitted = get_option('cr_post_2_pingfm_submit_post_submitted_'.$postId, false);
        if(!$this_post_submitted)
        {
            $continue = true;
        }
        else
        {
            if("all" == $postMode)
            {
                $continue = true;
                $republish = true;
            }
        }

        if($continue)
        {
            $theCats = get_the_category($postId);
            foreach($theCats as $cats)
            {
                $categories[] = $cats->cat_ID;
                $categories[] = $cats->category_nicename;
            }
            $continue = isCategoriesAllowedToPing($postId, $categories);
        }
       
        if($continue)
        {
            update_option('cr_post_2_pingfm_submit_post_submitted_'.$postId, true);
            submitPingFM($postId, $republish);
        }
    }
}

function cr_post_2_pingfm_submit_config_admin()
{
    add_options_page('CR Post2Pingfm', 'CR Post2Pingfm', 8, __FILE__, 'cr_post_2_pingfm_submit_config_form');
}

function cr_post_2_pingfm_submit_config_form() {
?><div class="wrap">
<h2>CR Post2Pingfm</h2>

<form method="post" action="options.php">
<?php settings_fields('cr_post_2_pingfm_settings'); ?>
<?php $options = get_option('cr_post_2_pingfm_options'); ?>

<table class="form-table">

<tr valign="top">
<th scope="row">Ping.fm Application Key</th>
<td><input type="text" size="45" name="cr_post_2_pingfm_options[cl_post_pingfm_api_key]" value="<?php echo $options['cl_post_pingfm_api_key']; ?>" /></td>
</tr>

<tr valign="top">
<th scope="row">Ping Template</th>
<td><input type="text" size="45" name="cr_post_2_pingfm_options[cl_post_pingfm_message_template]" value="<?php echo ($options['cl_post_pingfm_message_template'] ? $options['cl_post_pingfm_message_template'] : 'I just post {title} on {url}'); ?>" />

Template Tags: <strong>{title}</strong> for <em>Post Title</em> and <strong>{url}</strong> for <em>Permalink URL</em>

eg: <em>I just post {title} on {url}</em></td>
</tr>

<tr valign="top">
<th scope="row">Ping mode</th>
<td>
    Allow submit new post to ping.fm:

    <input type="radio" name="cr_post_2_pingfm_options[cl_post_pingfm_ping_mode]" value="all" <?php if($options['cl_post_pingfm_ping_mode'] == "all") { echo 'checked="checked"'; }?> />For all categories (<em>all</em>)

    <input type="radio" name="cr_post_2_pingfm_options[cl_post_pingfm_ping_mode]" value="allow" <?php if($options['cl_post_pingfm_ping_mode'] == "allow") { echo 'checked="checked"'; }?> />For this categories (<em>allow</em>)

    <input type="radio" name="cr_post_2_pingfm_options[cl_post_pingfm_ping_mode]" value="deny" <?php if($options['cl_post_pingfm_ping_mode'] == "deny") { echo 'checked="checked"'; }?> />For categories except this one (<em>deny</em>)

    Category list (<em>category ID and slug is supported</em>)<input type="text" name="cr_post_2_pingfm_options[cl_post_pingfm_ping_mode_category]" value="<?php echo $options['cl_post_pingfm_ping_mode_category']; ?>" />(comma separated, eg: <em>1,23,random-caregory,10,rants</em>)

    Category list is ignored if mode <em>all</em> is selected.
</td>
</tr>

<tr valign="top">
<th score="row">Publish mode</th>
<td>
    Allow submit to ping.fm on the following condition:

    <input type="radio" name="cr_post_2_pingfm_options[cl_post_pingfm_publish_mode]" value="once" <?php if($options['cl_post_pingfm_publish_mode'] == "once") { echo 'checked="checked"'; }?> />Only submit for the <strong>first time</strong>(<em>once</em>)

    <input type="radio" name="cr_post_2_pingfm_options[cl_post_pingfm_publish_mode]" value="all" <?php if($options['cl_post_pingfm_publish_mode'] == "all") { echo 'checked="checked"'; }?> />For everytime you push <strong>publish</strong> button(<em>all</em>)

</td>
</tr>

<tr valign="top">
<th score="row">Re-Publish template</th>
<td>
    This option only used if you choose <em>all</em> in <strong>publish mode</strong>.

    <input type="radio" name="cr_post_2_pingfm_options[cl_post_pingfm_republish_template]" value="above" <?php if($options['cl_post_pingfm_republish_template'] == "above") { echo 'checked="checked"'; }?> />Use Ping Template above

    <input type="radio" name="cr_post_2_pingfm_options[cl_post_pingfm_republish_template]" value="this" <?php if($options['cl_post_pingfm_republish_template'] == "this") { echo 'checked="checked"'; }?> />Use this template
    <input type="text" size="45" name="cr_post_2_pingfm_options[cl_post_pingfm_ping_republish_template_text]" value="<?php echo ($options['cl_post_pingfm_ping_republish_template_text'] ? $options['cl_post_pingfm_ping_republish_template_text'] : 'republished {title} on {url}'); ?>" />

    The above template tags, apply.
</td>
</tr>
</table>

<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="cl_post_pingfm_message_template,cl_post_pingfm_api_key,cl_post_pingfm_ping_mode_category,cl_post_pingfm_ping_mode,cl_post_pingfm_publish_mode,cl_post_pingfm_ping_republish_template_text,cl_post_pingfm_republish_template" />

<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>

</form>
</div><?php
}

function submitPingFM($postId, $republish = false)
{
    $post = get_post($postId);
    // error_log(date("c") . " - Entering loop for postId: $postId which contains >>>" . print_r($post, TRUE) . "<<<\n", 3, "/var/tmp/pingfm-" . date("Ymd") . ".log");
    include_once('PHPingFM.php');
   
    // compatibility with WordPress MU
   
    $settings = get_option('cr_post_2_pingfm_options');
    $my_API_key = $settings['cl_post_pingfm_api_key'];
    $ping_template = $settings['cl_post_pingfm_message_template'];
    $cl_post_pingfm_republish_template = $settings['cl_post_pingfm_republish_template'];
/*    
    $my_API_key = get_option('cl_post_pingfm_api_key');
    $ping_template = get_option('cl_post_pingfm_message_template');
    $cl_post_pingfm_republish_template = get_option('cl_post_pingfm_republish_template');
*/
    // error_log(date("c") . " - settings: >>>" . print_r($settings, TRUE) ."<<< my_API_key: $my_API_key, ping_template: >>>$ping_template<<< cl_post_pingfm_republish_template: >>>$cl_post_pingfm_republish_template<<<\n", 3, "/var/tmp/pingfm-" . date("Ymd") . ".log");

    if($republish)
    {
        if("this" == $cl_post_pingfm_republish_template)
        {
            $ping_template = $settings['cl_post_pingfm_ping_republish_template_text'];
            // $ping_template = get_option('cl_post_pingfm_ping_republish_template_text');
        }
    }
   

    $pfm = new PHPingFM(API_KEY, $my_API_key, false);
    $arrTemplate = array(
        '{title}' => $post->post_title,
        '{url}' => get_permalink($postId),
    );

    foreach($arrTemplate as $template => $template_data)
    {
        $ping_template = str_replace($template, $template_data, $ping_template);
    }
   
    // error_log(date("c") . " - Ping template: '" . $ping_template . "'\n", 3, "/var/tmp/pingfm-" . date("Ymd") . ".log");
   
    $result = $pfm->post("status", $ping_template);
}

function isCategoriesAllowedToPing($postId, $categories)
{
    $pingMode = get_option('cl_post_pingfm_ping_mode');
    $pingCats = get_option('cl_post_pingfm_ping_mode_category');
    $pingCats = array_map("trim", explode(",", $pingCats));
    if(!is_array($pingCats)) $pingCats = array();

    if("allow" == $pingMode)
    {
        foreach($categories as $cats)
        {
            if(in_array($cats, $pingCats))
            {
                return true;
            }
        }
    }
    else if("deny" == $pingMode)
    {
        foreach($categories as $cats)
        {
            if(in_array($cats, $pingCats))
            {
                return false;
            }
        }
        return true;
    }
    else
    {
        return true;
    }
}
Code language: PHP (php)
Print Friendly, PDF & Email