function wp_ozh_yourls_newpost($post) { global $wp_ozh_yourls; $post_id = $post->ID; $url = get_permalink($post_id); if ($post->post_type != 'post' && $post->post_type != 'page') { return; } // Generate short URL ? if (!wp_ozh_yourls_generate_on($post->post_type)) { return; } $title = get_the_title($post_id); $url = get_permalink($post_id); $short = wp_ozh_yourls_get_new_short_url($url); // Tweet short URL ? if (!wp_ozh_yourls_tweet_on($post->post_type)) { return; } if (!get_post_custom_values('yourls_tweeted', $post_id)) { // Not tweeted yet $tweet = wp_ozh_yourls_maketweet($short, $title); if (wp_ozh_yourls_send_tweet($tweet)) { update_post_meta($post_id, 'yourls_tweeted', 1); } } }
function wp_ozh_yourls_drawbox($post) { $type = $post->post_type; $status = $post->post_status; $id = $post->ID; $title = $post->post_title; if ($type != 'post' && $type != 'page') { return; } // Not sure this can actually happen since add_meta_box() should take care of this. Just in case. // Too early, young Padawan if ($status != 'publish') { echo '<p>When you publish this post, you will be able here to (re)promote it via Twitter.</p> <p>Depending on <a href="options-general.php?page=ozh_yourls">configuration</a>, this also happens automagically when you press "Publish" of course :)</p>'; return; } $shorturl = wp_ozh_yourls_geturl($id); // Bummer, could not generate a short URL if (!shorturl) { echo '<p>Bleh. The URL shortening service you configured could not be reached as of now. This might be a temporary problem, please try again later!</p>'; return; } global $wp_ozh_yourls; $action = 'Tweet this'; $promote = "Promote this {$type}"; $tweeted = get_post_meta($id, 'yourls_tweeted', true); $account = $wp_ozh_yourls['twitter_login']; wp_nonce_field('yourls', '_ajax_yourls', false); echo ' <input type="hidden" id="yourls_post_id" value="' . $id . '" /> <input type="hidden" id="yourls_shorturl" value="' . $shorturl . '" /> <input type="hidden" id="yourls_twitter_account" value="' . $account . '" />'; echo '<p><strong>Short URL</strong></p>'; echo '<div id="yourls-shorturl">'; echo "<p>This {$type}'s short URL: <strong><a href='{$shorturl}'>{$shorturl}</a></strong></p>\n\t<p>You can click Reset to generate another short URL if you picked another URL shortening service in the <a href='options-general.php?page=ozh_yourls'>plugin options</a></p>"; echo '<p style="text-align:right"><input class="button" id="yourls_reset" type="submit" value="Reset short URL" /></p>'; echo '</div>'; echo '<p><strong>' . $promote . ' on <a href="http://twitter.com/' . $account . '">@' . $account . '</a>: </strong></p> <div id="yourls-promote">'; if ($tweeted) { $action = 'Retweet this'; $promote = "Promote this {$type} again"; echo '<p><em>Note:</em> this post has already been tweeted. Not that there\'s something wrong to promote it again, of course :)</p>'; } echo '<p><textarea id="yourls_tweet" rows="1" style="width:100%">' . wp_ozh_yourls_maketweet($shorturl, $title) . '</textarea></p> <p style="text-align:right"><input class="button" id="yourls_promote" type="submit" value="' . $action . '" /></p> </div>'; ?> <script type="text/javascript"> /* <![CDATA[ */ (function($){ var yourls = { // Send a tweet send: function() { var post = {}; post['yourls_tweet'] = $('#yourls_tweet').val(); post['yourls_post_id'] = $('#yourls_post_id').val(); post['yourls_twitter_account'] = $('#yourls_twitter_account').val(); post['action'] = 'yourls-promote'; post['_ajax_nonce'] = $('#_ajax_yourls').val(); $('#yourls-promote').html('<p>Please wait...</p>'); $.ajax({ type : 'POST', url : '<?php echo admin_url('admin-ajax.php'); ?> ', data : post, success : function(x) { yourls.success(x, 'yourls-promote'); }, error : function(r) { yourls.error(r, 'yourls-promote'); } }); }, // Reset short URL reset: function() { var post = {}; post['yourls_post_id'] = $('#yourls_post_id').val(); post['yourls_shorturl'] = $('#yourls_shorturl').val(); post['action'] = 'yourls-reset'; post['_ajax_nonce'] = $('#_ajax_yourls').val(); $('#yourls-shorturl').html('<p>Please wait...</p>'); $.ajax({ type : 'POST', url : '<?php echo admin_url('admin-ajax.php'); ?> ', data : post, success : function(x) { yourls.success(x, 'yourls-shorturl'); yourls.update(x); }, error : function(r) { yourls.error(r, 'yourls-shorturl'); } }); }, // Update short URL in the tweet textarea update: function(x) { var r = wpAjax.parseAjaxResponse(x); r = r.responses[0]; var oldurl = r.supplemental.old_shorturl; var newurl = r.supplemental.shorturl; var bg = jQuery('#yourls_tweet').css('backgroundColor'); if (bg == 'transparent') {bg = '#fff';} $('#yourls_tweet') .val( $('#yourls_tweet').val().replace(oldurl, newurl) ) .animate({'backgroundColor':'#ff8'}, 500, function(){ jQuery('#yourls_tweet').animate({'backgroundColor':bg}, 500) }); }, // Ajax: success success : function(x, div) { if ( typeof(x) == 'string' ) { this.error({'responseText': x}, div); return; } var r = wpAjax.parseAjaxResponse(x); if ( r.errors ) this.error({'responseText': wpAjax.broken}, div); r = r.responses[0]; $('#'+div).html('<p>'+r.data+'</p>'); }, // Ajax: failure error : function(r, div) { var er = r.statusText; if ( r.responseText ) er = r.responseText.replace( /<.[^<>]*?>/g, '' ); if ( er ) $('#'+div).html('<p>Error during Ajax request: '+er+'</p>'); } }; $(document).ready(function(){ $('#yourls_promote').click(function(e) { yourls.send(); e.preventDefault(); }); $('#yourls_reset').click(function(e) { yourls.reset(); e.preventDefault(); }); $('#edit-slug-box').append('<span id="yourls-shorturl-button"><a onclick="prompt(\'Short URL:\', \'<?php echo $shorturl; ?> \'); return false;" class="button" href="#">Get Short URL</a></span>'); $('#yourls-shorturl-button a').css('border-color','#bbf'); }) })(jQuery); /* ]]> */ </script> <?php }