PHPSEEKER.ORG

A free resort for PHP Developers

Lorem Ipsum Plugin

Please enter only posts related to string type. Thanks :)

Lorem Ipsum Plugin

Postby toshio » Wed Apr 01, 2009 3:04 pm

Really cool plugin!

Code: Select all
<?php
/*
Plugin Name: Lorem
Plugin URI: http://redalt.com/wiki/Lorem Plugin
Description: Creates a bunch of posts with generated text.
Version: 1.0
Author: Owen Winkler
Author URI: http://www.asymptomatic.net
Update Server:  http://redalt.com/
Min WP Version: 2.0
Max WP Version: 2.1
License: MIT License - http://www.opensource.org/licenses/mit-license.php
Copyright: Copyright 2006 Owen Winkler
*/

function lorem_menu()
{
   add_theme_page('Lorem Post Generator', 'Lorem', 'edit_post', basename(__FILE__), 'lorem_options');
}

function lorem_get_pgraph()
{
   $start = array("Nam quis nulla", "Integer malesuada", "In an enim", "Sed vel lectus", "Donec odio urna,", "Phasellus rhoncus", "Aenean id ", "Vestibulum fermentum", "Pellentesque ipsum",  "Nulla non", "Proin in tellus", "Vivamus luctus", "Maecenas sollicitudin", "Etiam egestas", "Lorem ipsum dolor sit amet,", "Nullam feugiat,", "Aliquam erat volutpat", "Mauris pretium",);
   $mid = array(" a arcu imperdiet", " tempus molestie,", " porttitor ut,", " iaculis quis,", " metus id velit", " lacinia neque", " sed nisl molestie", " sit amet nibh", " consectetuer adipiscing", " turpis at pulvinar vulputate,", " erat libero tristique tellus,", " nec bibendum odio risus"," pretium quam", " ullamcorper nec,", " rutrum non,", " nonummy ac,", " augue id magna",);
   $end = array(" nulla.  "," malesuada.  "," lectus.  "," sem.  "," pulvinar.  "," faucibus fringilla.  "," dignissim sagittis.  "," egestas leo.  "," metus.  "," erat.  "," elit.  "," sit amet ante.  "," volutpat.  "," urna.  "," rutrum.  ",);

   $ipsum_text = '';
   $lines = rand(1,6);
   for($l = 0; $l < $lines; $l++) {
      $line = $start[rand(0,count($start))];
      $mids = rand(1,3);
      for($z = 0; $z < $mids; $z++) $line .= $mid[rand(0,count($mid))];
      $line .= $end[rand(0,count($end))];
      $ipsum_text .= $line;
   }
   $ipsum_text .= "\n\n";
   return $ipsum_text;   
}

function lorem_num2word($i)
{
   $word = '';
   $phon = array('do', 're', 'mi', 'fa', 'so', 'la', 'ti', 'ko', 'fu', 'jan');
   do {
      $word = $phon[$i % 10] . $word;
      $i = floor($i/10);
   } while($i >0);
   return $word;
}

function lorem_get_title()
{
   $text = lorem_get_pgraph(1);
   $text = strtolower($text);
   $text = preg_replace('/[^a-z\s]/', '', $text);
   $text = explode(' ', $text);
   $words = rand(2, 8);
   for($i = 0; $i < $words; $i++) {
      $title .= $text[rand(0, count($text))] . ' ';
   }
   $title = ucwords(trim($title));
   return $title;
}

function lorem_get_thumb_tag($tags)
{
   global $thumbs;
   
   if(!is_array($thumbs)) {
      $searchurl = 'http://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=420fb7714e08dbcc97ac8228df21d985&license=4,2&per_page=10&tags=' . urlencode(implode(',', $tags));
      require_once(ABSPATH . 'wp-includes/class-snoopy.php');
      $client = new Snoopy();
      $client->agent = 'WordPres Codex/Support Search Plugin';
      $client->read_timeout = 2;
      $client->use_gzip = true;
      @$client->fetch($searchurl);
      preg_match_all('/<photo.*id="([0-9]+)".*owner="([^"]+)".*secret="([0-9a-f]+)".*server="([0-9]+)".*title="([^"]+)".*\/>/', $client->results, $matches, PREG_SET_ORDER);
      foreach($matches as $match)
      {
         list($fulltag, $id, $owner, $secret, $server, $title) = $match;
         $imgurl = "http://static.flickr.com/{$server}/{$id}_{$secret}_m.jpg";
         $flickrurl = "http://flickr.com/photos/{$owner}/{$id}";
         $styles = array (
            ' style="float:left;"',
            ' style="float:right;"',
            ' style="display:block;"',
         );
         $style = $styles[rand(0,2)];
         $thumbs[] = "<a href=\"{$flickrurl}\"{$style}><img src=\"{$imgurl}\" alt=\"{$title}\"></a>";
      }
   }
   return $thumbs[rand(0,count($thumbs))];      
}

function lorem_get_content($min, $max, $more, $features, $imgtags)
{
   $lipsum_text = '';
   $howmany = rand($min, $max);
   for($i = 0; $i < $howmany; $i++) {
      if(isset($features['thumb'])) {
         if(rand(1, $max - $i + 1) == 1) {
            $lipsum_text .= lorem_get_thumb_tag(explode(' ',$imgtags));
            unset($features['thumb']);
         }
      }
      $lipsum_text .= lorem_get_pgraph();
      if(isset($features['ol'])) {
         if(rand(1, $max - $i + 1) == 1) {
            $listitems = rand(3,10);
            $lipsum_text .= "<ol>\n";
            for($z = 0; $z < $listitems; $z++) {
               $lipsum_text .= "\t<li>" . lorem_get_title() . "</li>\n";
            }
            $lipsum_text .= "</ol>\n";
            unset($features['ol']);
         }
      }
      if(isset($features['ul'])) {
         if(rand(1, $max - $i + 1) == 1) {
            $listitems = rand(3,10);
            $lipsum_text .= "<ul>\n";
            for($z = 0; $z < $listitems; $z++) {
               $lipsum_text .= "\t<li>" . lorem_get_title() . "</li>\n";
            }
            $lipsum_text .= "</ul>\n";
            unset($features['ul']);
         }
      }      
      
      switch($more) {
      case 'none':
         break;
      case 'some':
         if(rand(1,2) == 1) break;
      case 'all':
         if($i==0) {
            $lipsum_text .= '<!--more-->';
         }
      }
   }
   return $lipsum_text;
}

function lorem_options()
{
   global $wpdb;

   $options = get_option('lorem');
   if(isset($_POST['restore'])) {
      if(isset($options['posts'])) foreach($options['posts'] as $postid) wp_delete_post($postid);
      if(isset($options['users'])) foreach($options['users'] as $userid) wp_delete_user($userid);
      if(isset($options['categories'])) foreach($options['categories'] as $catid) wp_delete_category($catid);
      $options = array();
      echo '<div id="message" class="updated fade"><p>Restored blog.</p></div>';
      update_option('lorem', $options);
   }
   if(isset($_POST['create'])) {
      remove_action('publish_post', 'generic_ping');
      $authors = intval($_POST['authors']);
      $newusers = array();
      for($z = 0;$z < $authors; $z++) {
         $uid = wp_create_user(lorem_num2word(rand(10000,99999)), lorem_num2word(rand(10000,99999)));
         $u = new WP_User($uid);
         $u->add_cap('edit_posts');
         $newusers[] = $uid;
         $options['users'][] = $uid;
      }
      if(isset($_POST['users']))   $newusers = array_merge($newusers, $_POST['users']);
      
      $cats = intval($_POST['newcats']);
      $newcategories = array();
      for($z = 0;$z < $authors; $z++) {
         $cid = wp_create_category(lorem_num2word(rand(10000,99999)));
         $newcategories[] = $cid;
         $options['categories'][] = $cid;
      }
      if(isset($_POST['post_category'])) $newcategories = array_merge($newcategories, $_POST['post_category']);
      
      $count = intval($_POST['count']);
      for($z = 0;$z < $count; $z++) {
         $post_content = lorem_get_content($_POST['paramin'], $_POST['paramax'], $_POST['more'], $_POST['features'], $_POST['imgtags']);
         $post_title = lorem_get_title();
         $post_status = 'publish';
         $post_date = date('Y-m-d H:i:s', current_time('timestamp') - 60 * ($count - $z));
         $post_author = $newusers[rand(0,count($newusers)-1)];
         $post_category = array($newcategories[rand(0,count($newcategories)-1)]);
         $post_data = compact('post_content','post_title','post_status','post_date','post_author','post_category');
         $post_data = add_magic_quotes($post_data);
         $options['posts'][] = wp_insert_post($post_data);
      }
      echo '<div id="message" class="updated fade"><p>Created ' . $count . ' posts.</p></div>';
      update_option('lorem', $options);
   }
   ?>
      <div class="wrap">
         <h2>Lorem</h2>
         <form method="post">
            <h3>Create Posts</h3>
            <table class="optiontable">
            <tr>
               <th>Create how many posts:</th>
               <td><input type="text" name="count" value="5" size="4" /></td>
            </tr>
            <tr>
               <th>Minimum/Maximum paragraphs:</th>
               <td><input type="text" name="paramin" value="2" size="4" /> / <input type="text" name="paramax" value="5" size="4" /></td>
            </tr>
            <tr>
               <th>Use &lt;--more--&gt;:</th>
               <td>
                  <label><input type="radio" name="more" value="none" /> On No Posts</label><br />
                  <label><input type="radio" name="more" value="some" checked="checked" /> On Some Posts</label><br />
                  <label><input type="radio" name="more" value="all" /> On All Posts</label>
               </td>
            </tr>
            <tr>
               <th>Use which authors:</th>
               <td>
                  <label>Create <input type="text" name="authors" value="0" size="4" /> new authors</label><br />
                  <?php
                     $userids = $wpdb->get_col("SELECT ID FROM $wpdb->users;");
                     foreach($userids as $user_id) {
                        $profileuser = new WP_User($user_id);
                        if($profileuser->has_cap('publish_posts')) {
                           echo "<label style=\"width:200px;display:block;\"> <input type=\"checkbox\" name=\"users[]\" value=\"{$user_id}\" />{$profileuser->display_name}</label> ";
                        }
                     }
                  ?>
               </td>
            </tr>
            <tr>
               <th>Use which categories:</th>
               <td>
                  <label>Create <input type="text" name="newcats" value="0" size="4" /> new categories</label>
                  <style type="text/css">
                  #categorychecklist span {
                     display: block;
                  }
                  </style>
                  <div id="categorychecklist">
                  <?php
                     dropdown_categories(get_settings('default_category'));
                  ?>
                  </div>
               </td>
            </tr>
            <tr>
               <th>Randomly include these features:</th>
               <td>
                  <label><input type="checkbox" name="features[ol]" value="1" /> Ordered List</label><br />
                  <label><input type="checkbox" name="features[ul]" value="1" /> Unordered List</label><br />
                  <label><input type="checkbox" name="features[thumb]" value="1" /> Linked Thumbnail Images from Flickr</label> <label>Tagged: <input type="text" name="imgtags" value="fun" /></label>
               </td>
            </tr>
            </table>
            <p class="submit"><input type="submit" name="create" value="Create" /></p>
         </form>
      </div>
      <div class="wrap">
         <h3>Restore</h3>
         <form method="post">
            <p>Delete all created posts, categories, authors, etc.</p>
            <p class="submit"><input type="submit" name="restore" value="Restore" /></p>
         </form>      
      </div>
   <?php
}

add_action('admin_menu', 'lorem_menu');

?>


Enjoy it :mrgreen:
toshio
Site Admin
 
Posts: 34
Joined: Sat Dec 13, 2008 8:22 pm

Return to Working with Text (string)

Who is online

Users browsing this forum: No registered users and 1 guest

cron