Advertisement
InfinityExistz

post.php

Jun 30th, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.04 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Add The Slide options box to the post CRUD page
  5.  *
  6.  * @author Eric Lubow <elubow@simplereach.com>
  7.  * @param None
  8.  * @return None
  9.  */
  10. function srslide_post_meta_box()
  11. {
  12.     if (function_exists('add_meta_box')) {
  13.         add_meta_box('srslide',__('The Slide', 'srslide'), 'srslide_meta','post');
  14.     add_meta_box('srslide',__('The Slide', 'srslide'), 'srslide_meta','page');
  15.     }
  16. }
  17.  
  18.  
  19. /**
  20.  * The code for The Slide options box on the post CRUD page
  21.  *
  22.  * @author Eric Lubow <elubow@simplereach.com>
  23.  * @param None
  24.  * @return None
  25.  */
  26. function srslide_meta()
  27. {
  28.     global $post;
  29.     $post_id = $post;
  30.     if (is_object($post_id)) {
  31.         $post_id = $post_id->ID;
  32.     }
  33.  
  34.     wp_register_style('srslide.css', SRSLIDE_PLUGIN_URL . 'srslide.css');
  35.     wp_enqueue_style('srslide.css');
  36.     wp_nonce_field( plugin_basename(__FILE__), 'srslide_noncename' );
  37.  
  38.     $title          = htmlspecialchars(stripcslashes(get_post_meta($post_id, '_srslide_title', true)));
  39.     $article_icon       = htmlspecialchars(stripcslashes(get_post_meta($post_id, '_srslide_article_icon', true)));
  40.     $tags           = htmlspecialchars(stripcslashes(get_post_meta($post_id, '_srslide_tags', true)));
  41.     $disable_slide_on_post  = htmlspecialchars(stripcslashes(get_post_meta($post_id, '_srslide_disable_on_post', true)));
  42.     if ($post_end_date             = htmlspecialchars(stripcslashes(get_post_meta($post_id, '_srslide_post_end_date', true)))) {
  43.     $end_date_pattern = '/(\d+)\-(\d+)\-(\d+)\s+(\d+):(\d+):(\d+)/';
  44.     if (preg_match($end_date_pattern,$post_end_date,$matches)) {
  45.             $post_end_year = $matches[1];
  46.         $post_end_month = $matches[2];
  47.         $post_end_day = $matches[3];
  48.         $post_end_hour = $matches[4];
  49.         $post_end_minute = $matches[5];
  50.         $post_end_meridian = 1;
  51.         if ($post_end_hour > 12) {
  52.             $post_end_hour -= 12;
  53.             $post_end_meridian = 2;
  54.         }
  55.     }
  56.     }
  57.     ?>
  58.     <div id="srslide_meta_box">
  59.     <input value='srslide_edit' type='hidden' name='srslide_edit' />
  60.         <table>
  61.             <tr>
  62.                 <th><?php print __('Title:', 'srslide') ?></th>
  63.                 <td><input value="<?php echo $title ?>" type="text" name="srslide_title" size="60" /></td>
  64.             </tr>
  65.             <tr>
  66.                 <th scope="row"><?php print __('Article Icon URL:', 'srslide') ?></th>
  67.                 <td><input value="<?php echo $article_icon ?>" type="text" name="srslide_article_icon" size="60" /></td>
  68.             </tr>
  69.             <tr>
  70.                 <th scope="row"><?php print __('Tags:', 'srslide') ?></th>
  71.                 <td><input value="<?php echo $tags ?>" type="text" name="srslide_tags" size="60" /></td>
  72.             </tr>
  73.             <tr>
  74.                 <th scope="row"><?php print __('End Date:', 'srslide') ?></th>
  75.                 <td>
  76.                         <select name="srslide_post_end_month" id="srslide_post_end_month" value="">
  77. <?php
  78. $month_list = array('0' => '-----', '01' => 'January', '02' => 'February',
  79.             '03' => 'March', '04' => 'April', '05' => 'May',
  80.             '06' => 'June', '07' => 'July', '08' => 'August',
  81.             '09' => 'September', '10' => 'October', '11' => 'November',
  82.             '12' => 'December');
  83. foreach ($month_list as $k=>$v) {
  84.     echo "\t\t\t\t".'<option value="'.$k.'"';
  85.     if ($k == $post_end_month) {
  86.         echo ' selected >';
  87.     }
  88.     else {
  89.         echo '>';
  90.     }
  91.     echo $v.'</option>'."\n";
  92. }
  93. ?>
  94.                         </select>
  95.                         <select name="srslide_post_end_day" id="srslide_post_end_day" value="">
  96.                 <option value='0'>------</option>
  97. <?php
  98. for ($i=1; $i <= 31; $i++) {
  99.     $j = sprintf("%02d",$i);
  100.         echo "\t\t\t\t".'<option value="'.$j.'"';
  101.         if ($i == $post_end_day) {
  102.                 echo ' selected';
  103.         }
  104.         echo '>'.$j.'</option>'."\n";
  105. }
  106. ?>
  107.                         </select>
  108.                         <select name="srslide_post_end_year" id="srslide_post_end_year" value="">
  109. <?php
  110. $year = (int) date("Y");
  111. $maxyear = $year + 5;
  112. for ($i = $year;$i < $maxyear;$i++) {
  113.         echo "\t\t\t\t".'<option value="'.$i.'"';
  114.     if ($i == $post_end_year) {
  115.         echo ' selected';
  116.     }
  117.     echo '>'.$i.'</option>'."\n";
  118. }
  119. ?>
  120.                         </select>
  121.  
  122.                         <select name="srslide_post_end_hour" id="srslide_post_end_hour" value="">
  123. <?php
  124. for ($i = 1;$i < 13;$i++) {
  125.         echo "\t\t\t\t".'<option value="'.$i.'"';
  126.         if ($i == $post_end_hour) {
  127.                 echo ' selected';
  128.         }
  129.         echo '>'.$i.'</option>'."\n";
  130. }
  131. ?>
  132.                         </select>
  133.                         <select name="srslide_post_end_minute" id="srslide_post_end_minute" value="">
  134. <?php
  135. for ($i = 0;$i < 60;$i++) {
  136.     $j = sprintf("%02d",$i);
  137.         echo "\t\t\t\t".'<option value="'.$j.'"';
  138.         if ($i == $post_end_minute) {
  139.                 echo ' selected';
  140.         }
  141.         echo '>'.$j.'</option>'."\n";
  142. }
  143. ?>
  144.                         </select>
  145. <input type="radio" name="srslide_post_end_meridian" value="1" <?php echo ($post_end_meridian != 2) ? 'Checked' : ''; ?>>&nbsp;AM&nbsp;&nbsp;
  146. <input type="radio" name="srslide_post_end_meridian" value="2" <?php echo ($post_end_meridian == 2) ? 'Checked' : ''; ?>>&nbsp;PM&nbsp;&nbsp;
  147.  
  148.  
  149.  
  150.                 </td>
  151.             </tr>
  152.             <tr>
  153.                 <th scope="row"><?php print __('Disable Slide on post:', 'srslide') ?></th>
  154.                 <td><input name="srslide_disable_on_post" id="srslide_disable_on_post" type="checkbox"  <?php if ($disable_slide_on_post) echo 'checked="1"'; ?> /></td>
  155.             </tr>
  156.  
  157.         </table>
  158.     </div>
  159.     <?php
  160. }
  161.  
  162. /**
  163.  * Save the post meta data for The Slide
  164.  *
  165.  * @author Eric Lubow <elubow@simplereach.com>
  166.  * @param Integer Post ID
  167.  * @return None
  168.  */
  169. function srslide_post_save_data($post_id)
  170. {
  171.     // Ignore if auto-saving
  172.     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  173.         return $post_id;
  174.     }
  175.  
  176.     // verify this came from the our screen and with proper authorization,
  177.     // because save_post can be triggered at other times
  178.     $srslide_noncename = (!empty($_POST['srslide_noncename'])) ? $_POST['srslide_noncename'] : '';
  179.     if (!wp_verify_nonce($srslide_noncename, plugin_basename(__FILE__))) {
  180.         return $post_id;
  181.     }
  182.  
  183.     // Check permissions
  184.     $post_type = (!empty($_POST['post_type'])) ? $_POST['post_type'] : '';
  185.     if ('page' == $post_type) {
  186.         if (!current_user_can( 'edit_page', $post_id ))
  187.             return $post_id;
  188.     } else {
  189.         if (!current_user_can( 'edit_post', $post_id ))
  190.             return $post_id;
  191.     }
  192.  
  193.     // OK, we're auth'd, time to make the donuts
  194.     global $post;
  195.     $id = $post;
  196.     if (is_object($id)) {
  197.         $id = $post->ID;
  198.     }
  199.  
  200.     $disable_slide_on_post  = (!empty($_POST['srslide_disable_on_post']))   ? $_POST['srslide_disable_on_post'] : '';
  201.     $title          = (!empty($_POST['srslide_title']))         ? $_POST['srslide_title'] : '';
  202.     $tags           = (!empty($_POST['srslide_tags']))      ? $_POST['srslide_tags'] : '';
  203.     $icon           = (!empty($_POST['srslide_article_icon']))  ? $_POST['srslide_article_icon'] : '';
  204.     $post_end_month     = (!empty($_POST['srslide_post_end_month']))    ? $_POST['srslide_post_end_month'] : '';
  205.     $post_end_day             = (!empty($_POST['srslide_post_end_day']))    ? $_POST['srslide_post_end_day'] : '';
  206.     $post_end_year             = (!empty($_POST['srslide_post_end_year']))    ? $_POST['srslide_post_end_year'] : '';
  207.     $post_end_hour             = (!empty($_POST['srslide_post_end_hour']))    ? $_POST['srslide_post_end_hour'] : '';
  208.     $post_end_minute             = (!empty($_POST['srslide_post_end_minute']))    ? $_POST['srslide_post_end_minute'] : '';
  209.     $post_end_meridian             = (!empty($_POST['srslide_post_end_meridian']))    ? $_POST['srslide_post_end_meridian'] : '';
  210.  
  211.     // Clear everything out first to start with a clean slate
  212.     delete_post_meta($id, '_srslide_title');
  213.     delete_post_meta($id, '_srslide_tags');
  214.     delete_post_meta($id, '_srslide_article_icon');
  215.     delete_post_meta($id, '_srslide_disable_on_post');
  216.     delete_post_meta($id, '_srslide_post_end_date');
  217.  
  218.     // Ignore if the slide should be hidden
  219.     add_post_meta($id, '_srslide_disable_on_post', !empty($disable_slide_on_post));
  220.     // if(isset($disable_slide_on_post) && !empty($disable_slide_on_post)) {
  221.     //     add_post_meta($id, '_srslide_disable_on_post', true);
  222.     //     // We don't just return here because we want to save the data anyway
  223.     //     // SimpleReach will index this post even though The Slide is disabled
  224.     // } else {
  225.     //     // Add this anyway to ensure we show the slide
  226.     //     add_post_meta($id, '_srslide_disable_on_post', false);
  227.     // }
  228.  
  229.     // Set to post title if the field is empty
  230.     if (empty($title) && ($post->post_status == 'publish')) {
  231.         if (!empty($_POST['post_title'])) {
  232.             add_post_meta($id, '_srslide_title', $_POST['post_title']);
  233.         }
  234.     } else {
  235.         add_post_meta($id, '_srslide_title', $title);
  236.     }
  237.  
  238.     // Set to post tags if the field is empty
  239.     if (empty($tags) && ($post->post_status == 'publish')) {
  240.         $tagset = array();
  241.         foreach (wp_get_post_tags($id) as $tag) {
  242.         $tagset[] = $tag->name;
  243.         }
  244.         add_post_meta($id, '_srslide_tags', join(",", $tagset));
  245.     } else {
  246.         add_post_meta($id, '_srslide_tags', $tags);
  247.     }
  248.  
  249.     if (empty($icon) && ($post->post_status == 'publish')) {
  250.         $thumbnail_id = get_post_meta( $post->ID, '_thumbnail_id', true );
  251.         if ($thumbnail_id) {
  252.             list($img, $h, $w) = wp_get_attachment_image_src($thumbnail_id);
  253.             add_post_meta($id, '_srslide_article_icon', $img);
  254.         }
  255.     } else {
  256.         add_post_meta($id, '_srslide_article_icon', $icon);
  257.     }
  258.  
  259.     if (($post_end_month > 0) && ($post_end_day > 0)) {
  260.         if (checkdate($post_end_month,$post_end_day,$post_end_year)) {
  261.             if ($post_end_meridian == 2) {
  262.                 $post_end_hour += 12;
  263.             }
  264.             $post_end_date = sprintf("%04d-%02d-%02d %02d:%02d:00",$post_end_year,$post_end_month,$post_end_day,$post_end_hour,$post_end_minute);
  265.             add_post_meta($id,'_srslide_post_end_date',$post_end_date);
  266.         }
  267.     }
  268. }
  269. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement