bondcemil

Untitled

Jul 5th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.20 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Draft to Post
  4. Plugin URI: http://www.yakupgovler.com
  5. Description: Taslak yazılarınızı belirli aralıklarla yayımlar.
  6. Author:  Yakup Gövler
  7. Version: 1.0
  8. Author URI: http://www.yakupgovler.com
  9. */
  10.  
  11. function yg_d2p_options_page() {
  12.     add_options_page('Draft to Publish Settings', 'Draft to Publish', 10, __FILE__, 'yg_d2p_options');
  13. }
  14. add_action('admin_menu', 'yg_d2p_options_page');
  15.  
  16. function yg_is_d2p_activated () {
  17.     $crons = _get_cron_array();
  18.     if ( !empty($crons) ) {
  19.         foreach ( $crons as $timestamp => $cron ) {
  20.         if (isset($cron['yg_publish'])) {
  21.               return true;
  22.         }
  23.         }
  24.   }
  25.     return false;
  26. }
  27. function yg_d2p_save_settings() {
  28.  if ($_POST['submit']) {
  29.   $activate = (bool) ($_POST['activate']);
  30.   $pt = esc_attr($_POST['post_type']);
  31.     $error = false;
  32.     if (!($pt=='draft' || $pt=='pending')) $error = true;
  33.  
  34.     $delay = (int) $_POST['delay'];
  35.     if (!($delay==999 || $delay >= 5 && $delay <= 300)) $error=true;
  36.    
  37.     $cats = str_replace(' ', '', esc_attr($_POST['cats']));
  38.     $order = esc_attr($_POST['order']);
  39.     if (!($order == 'ASC' || $order == 'DESC' || $order == 'RAND')) $error=true;
  40.    
  41.     if (!$error) {
  42.      $options = array('activate'=>$activate, 'delay'=>$delay, 'post_type'=>$pt, 'cats'=>$cats, 'order'=>$order);
  43.      if (!update_option('yg_d2p_options', $options)) add_option('yg_d2p_options', $options);
  44.      if ($delay == 999) $delay = mt_rand(60, 180);
  45.      wp_clear_scheduled_hook('yg_publish');
  46.      if ($activate) wp_schedule_single_event(time() + $delay*60, 'yg_publish', array());
  47. ?>
  48.  <div id="message" class="updated fade"><p><strong>Ayarlar kaydedildi. <?php if ($activate) echo "Eklenti Aktifleştirildi!"; else echo "Eklenti Pasifleştirildi!"; ?></strong></p></div>
  49. <?php
  50.   }
  51.  }
  52. }
  53.  
  54. function yg_d2p_options() {
  55.     if ($_POST['submit']) {
  56.      check_admin_referer( 'd2p_options' );
  57.      yg_d2p_save_settings();
  58.     }
  59.     $options = get_option('yg_d2p_options');
  60.     $activate = (bool) $options['activate'];
  61.     $delay = (int) $options['delay'];
  62.     $post_type = esc_attr($options['post_type']);
  63.     $cats = esc_attr($options['cats']);
  64.     $order = esc_attr($options['order']);
  65.  
  66.     $delays = array(5, 10, 15, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 180, 240, 300);
  67.     ?>
  68.     <div class="wrap">
  69.         <div id="icon-edit-pages" class="icon32"><br /></div>
  70.     <h2>Draft to Publish Settings</h2>
  71.  
  72.         <form method="post" action="">
  73.        <?php wp_nonce_field('d2p_options'); ?>
  74.             <table class="form-table">
  75.                 <tr valign="top">
  76.                 <th scope="row">
  77.                       Eklentinin Şu anki Durumu</th>
  78.                 <td><?php
  79.                if (yg_is_d2p_activated ()) echo "<strong>Aktif</strong>"; else echo "<strong>Pasif</strong>";
  80.              ?>        
  81.                 </td>
  82.                 </tr>
  83.  
  84.                 <tr valign="top">
  85.                 <th scope="row">
  86.                       <label for="post_type">Eklentiyi Aktifleştir</label></th>
  87.                 <td><input type="checkbox" name="activate" id="activate" value="1" <?php checked( $activate ); ?> />
  88.                 </td>
  89.                 </tr>
  90.            
  91.                 <tr valign="top">
  92.                 <th scope="row">
  93.                       <label for="post_type">Yayımlanacak yazı türü</label></th>
  94.                 <td><select name="post_type" id="post_type">
  95.                             <option value="draft" <?php if ($post_type=='draft') echo " selected = 'selected'";?>><?php esc_attr_e('Draft') ?></option>
  96.                             <option value="pending" <?php if ($post_type=='pending') echo " selected = 'selected'";?>><?php esc_attr_e('Kontrol Bekleyen') ?></option>
  97.                         </select>
  98.                 </td>
  99.                 </tr>
  100.  
  101.              
  102.                 <tr valign="top">
  103.                 <th scope="row">
  104.                       <label for="delay">Yazıları şu gecikmeyle yayımla</label></th>
  105.                 <td>
  106.                         <select name="delay" id="delay">
  107.                         <?php
  108.                           foreach ($delays as $d) {
  109.                              $selected = '';
  110.                              if ($delay == $d) $selected = " selected='selected'";
  111.                              echo '<option value="' . $d . '" '. $selected . '>' . $d . '</option>'."\n";
  112.                             }
  113.                         ?>
  114.                             <option value="999" <?php if ($delay==999) echo " selected='selected'"?>><?php esc_attr_e('Random') ?> *</option>
  115.                         </select> dakika
  116.                         <br />
  117.                         <small>(* Rastgele seçiminde gecikme en az 60, en fazla 180 dakikadır.)</small>
  118.                 </td>
  119.                 </tr>
  120.                      
  121.                 <tr valign="top">
  122.                 <th scope="row">
  123.                       <label for="cats">Yazıların alınacağı kategoriler </label></th>
  124.                 <td><input type="text" name="cats" id="cats" value="<?php echo $cats;?>" /><br />
  125.                         <small>(Kategorilerin ID'lerini virgül ile ayırarak yazınız. Örn: 5,8,17)</small>
  126.                 </td>
  127.                 </tr>
  128.                      
  129.                 <tr valign="top">
  130.                 <th scope="row">
  131.                       <label for="order">Yazıları yayımlama sırası</label></th>
  132.                         <td>
  133.                         <select name="order" id="order">
  134.                             <option value="ASC"<?php if ($order=='ASC') echo " selected='selected'"?>>Eskiden Yeniye Doğru</option>
  135.                             <option value="DESC"<?php if ($order=='DESC') echo " selected='selected'"?>>Yeniden Eskiye Doğru</option>
  136.                             <option value="RAND"<?php if ($order=='RAND') echo " selected='selected'"?>>Rastgele</option>
  137.                         </select><br />
  138.                         <small>(Sıralamalar ID tabanlı olup, veritabanına kayıt sırası takip edilir. Yazı tarihi gözardı edilir.)</small>
  139.                 </td>
  140.                 </tr>
  141.              
  142.             </table>
  143.            
  144.           <input type="submit" name="submit" class="button-primary" id="delay" value="<?php esc_attr_e('Save Changes') ?>" />
  145.  
  146.         </form>
  147.    
  148.  
  149. <?php
  150. }
  151.  
  152.  
  153. function yg_draft_to_publish($postt='') {
  154.       global $wpdb;
  155.  
  156.         $options = get_option('yg_d2p_options');
  157.         $activate = (bool) $options['activate'];
  158.         $delay = (int) $options['delay'];
  159.         if ($delay == 999) $delay = mt_rand(60, 180);
  160.         $post_type = esc_attr($options['post_type']);
  161.         if ($post_type != 'pending') $post_type = 'draft';
  162.         $cats = esc_attr($options['cats']);
  163.         $order = esc_attr($options['order']);
  164.         if ($order == 'RAND') {$orderby = 'rand'; $order='';}else{ $orderby = 'ID';}
  165.         if ($order != '' && $order != 'DESC') $order = 'ASC';
  166.            
  167.         $args = array(
  168.         'category' => $cats,
  169.         'post_type' => 'post',
  170.         'post_status' => $post_type,
  171.         'numberposts' => 1,
  172.         'orderby' =>$orderby,
  173.         'order' => $order
  174.         );
  175.  
  176.         $post = get_posts($args);
  177.         if ($post) {
  178.             //$post = $wpdb->get_results("select * FROM $wpdb->posts WHERE post_status='draft' AND post_type='post' ORDER BY ID ASC LIMIT 1");
  179.             $post_id = $post[0]->ID;
  180.  
  181.             $postarr = array('ID' => $post_id, 'post_status' => 'publish', 'post_date' => current_time('mysql'), 'post_date_gmt' => current_time('mysql', 1));
  182.             wp_update_post($postarr);
  183.             wp_schedule_single_event(time() + $delay * 60, 'yg_publish', array());
  184.         } /*else{
  185.             add_action('admin_notices', 'yg_d2p_notice');
  186.         }*/
  187. }
  188. /*
  189. function yg_d2p_notice() {
  190.     echo '<div class="error"><p>';
  191.     echo '<strong>Uyarı:</strong>: Yayımlanacak yazı bulunamadığından, Draft to Publish eklentisi<br />tarafından yazıların yayımlanması durduruldu.';
  192.     echo '</p></div>';
  193. }  
  194. */
  195.  add_action('yg_publish', 'yg_draft_to_publish', 10, 1);
  196.  
  197.  register_activation_hook(__FILE__,'yg_draft2publish_install');
  198.  register_deactivation_hook(__FILE__,'yg_draft2publish_uninstall');
  199.  
  200. // Install plugin
  201. function yg_draft2publish_install() {
  202.   $options = array('activate'=>0, 'delay'=>30, 'post_type'=>'draft', 'cats'=>'', 'order'=>'ASC');
  203.     add_option('yg_d2p_options', $options);
  204. }
  205.  
  206. // Uninstall plugin
  207. function yg_draft2publish_uninstall() {
  208.  wp_clear_scheduled_hook('yg_publish');
  209. }
  210. ?>
Advertisement
Add Comment
Please, Sign In to add comment