Advertisement
alchymyth

quote source quote-source.php

Jan 28th, 2020
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.71 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Quote Source
  4. Plugin URI: http://raptor.hk/plugin-quote-source/
  5. Description: Quote your article source at the end of your post.
  6. Version: 1.0.6
  7. Author: Raptor/ edits alchymyth
  8. Author URI: http://raptor.hk/
  9. License: License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  10. */
  11.  
  12. define('QUOTE_SOURCE_VERSION', '1.0.6');
  13. define('QUOTE_WORDPRESS_FOLDER',$_SERVER['DOCUMENT_ROOT']);
  14. define('QUOTE_THEME_FOLDER',str_replace("\\",'/',dirname(__FILE__)));
  15. define('QUOTE_THEME_PATH','/' . substr(QUOTE_THEME_FOLDER,stripos(QUOTE_THEME_FOLDER,'wp-content')));
  16.  
  17. add_action('admin_init','quote_source_init');
  18. if(is_admin()) {
  19.     require_once(dirname( __FILE__ ) . '/qs-admin.php');
  20. }
  21.  
  22. function quote_source_init() {
  23.     wp_enqueue_style('quote_source_css', QUOTE_THEME_PATH . '/custom/meta.css');
  24.     foreach (array('post','page') as $type) {
  25.         if( get_option('qs_mode_page') == 'true' ) add_meta_box('quote_source_box', 'Quote up to '.get_option('qs_number_quotes').' sources', 'quote_source_setup', 'page', 'normal', 'high');
  26.         if( get_option('qs_mode_single_post') == 'true' || get_option('qs_mode_category') == 'true' || get_option('qs_mode_summary') == 'true' ) add_meta_box('quote_source_box', 'Quote up to '.get_option('qs_number_quotes').' sources', 'quote_source_setup', 'post', 'normal', 'high');
  27.     }
  28.     add_action('save_post','quote_source_save');
  29.  
  30. }
  31.  
  32. function quote_source_setup() {
  33.     global $post;
  34.     // get post metadata
  35.     $meta = get_post_meta($post->ID,'_quote_source',TRUE);
  36.     // include setup box
  37.     require_once('custom/meta.php');
  38.     // create a custom nonce
  39.     echo '<input type="hidden" name="quote_source_noncename" value="' . wp_create_nonce(__FILE__) . '" />';
  40. }
  41.  
  42. function quote_source_save($post_id) {
  43.     // authentication checks
  44.     // make sure data came from our meta box
  45.     if (!wp_verify_nonce($_POST['quote_source_noncename'],__FILE__)) {
  46.         return $post_id;
  47.     }
  48.  
  49.     // check user permissions
  50.     if ($_POST['post_type'] == 'page') {
  51.         if (!current_user_can('edit_page', $post_id)) {
  52.             return $post_id;
  53.         }
  54.     } else {
  55.         if (!current_user_can('edit_post', $post_id)) {
  56.             return $post_id;
  57.         }
  58.     }
  59.     // authentication passed, save data
  60.     // var types
  61.     // single: _quote_source[var]
  62.     // array: _quote_source[var][]
  63.     // grouped array: _quote_source[var_group][0][var_1], _quote_source[var_group][0][var_2]
  64.     $current_data = get_post_meta($post_id, '_quote_source', TRUE);
  65.     $new_data = $_POST['_quote_source'];
  66.     quote_source_clean($new_data);
  67.     if ($current_data) {
  68.         if (is_null($new_data)) {
  69.             delete_post_meta($post_id,'_quote_source');
  70.         } else {
  71.             update_post_meta($post_id,'_quote_source',$new_data);
  72.         }
  73.     } elseif (!is_null($new_data)) {
  74.         add_post_meta($post_id,'_quote_source',$new_data,TRUE);
  75.     }
  76.     return $post_id;
  77. }
  78.  
  79. function quote_source_clean(&$arr) {
  80.     if (is_array($arr)) {
  81.         foreach ($arr as $i => $v) {
  82.             if (is_array($arr[$i])) {
  83.                 quote_source_clean($arr[$i]);
  84.                 if (!count($arr[$i])) {
  85.                     unset($arr[$i]);
  86.                 }
  87.             } else {
  88.                 if (trim($arr[$i]) == '') {
  89.                     unset($arr[$i]);
  90.                 }
  91.             }
  92.         }
  93.         if (!count($arr)) {
  94.             $arr = NULL;
  95.         }
  96.     }
  97. }
  98.  
  99. function quote_source($content = '') {
  100.     /*
  101.     INPUT: the post contents
  102.     OUTPUT: the post contents with quoted source at specified position & format
  103.     */
  104.     global $post;
  105.     $meta = get_post_meta($post->ID,'_quote_source',TRUE);
  106.     $quotes = array();
  107.    
  108.     $src_cnt = 0;
  109.     for($i = 0; $i < get_option('qs_number_quotes'); $i++) {
  110.         $tmp_url = isset( $meta['quote' . ($i+1)] ) ? $meta['quote'. ($i+1)] : '';
  111.         $tmp_title = isset( $meta['quote' . ($i+1) .'_title'] ) ? $meta['quote' . ($i+1) .'_title'] : '';
  112.         if(!empty($tmp_url) || !empty($tmp_title)) {
  113.             $quotes[$src_cnt] = array();
  114.             $quotes[$src_cnt]['url'] = $tmp_url;
  115.             $quotes[$src_cnt]['title'] = $tmp_title;
  116.             $src_cnt++;
  117.         }
  118.     }
  119.     if($src_cnt == 0) {
  120.         return $content;
  121.     }
  122.    
  123.     if((is_single()     && get_option('qs_mode_single_post', 'true')    == 'true') ||
  124.         (is_category()  && get_option('qs_mode_category', 'false')      == 'true') ||
  125.         (is_page()      && get_option('qs_mode_page', 'false')          == 'true') ||
  126.         ((is_home() || is_archive() || is_paged()) && get_option('qs_mode_summary', 'false') == 'true')) {
  127.  
  128.             $display_mode = (int)get_option('qs_layout', 1);
  129.            
  130.             switch($display_mode) {
  131.                 case 1:
  132.                     $content .= '<hr noshade="noshade" />';
  133.                     $content .= '<p class="quote-source" style="color: #' . get_option('qs_text_color', '000000') . '"><strong>Sources :</strong>' . PHP_EOL;
  134.                     $content .= '</br>';
  135.                     $lines = array();
  136.                     foreach($quotes as $quote) {
  137.                         if(!empty($quote['title'])) {
  138.                             if(!empty($quote['url'])) {
  139.                                 $lines[] = '<a href="' . $quote['url'] . '" title="' . esc_attr( $quote['title'] ) . '" target="_blank">' . $quote['title'] . '</a>' . PHP_EOL;
  140.                             } else {
  141.                                 $lines[] = $quote['title'] . PHP_EOL;
  142.                             }
  143.                         } else {
  144.                             $lines[] = '<a href="' . $quote['url'] . '" target="_blank">' . $quote['url'] . '</a>' . PHP_EOL;
  145.                         }
  146.                     }
  147.                     $content .= implode('</br>', $lines);
  148.                     $content .= '</p>';
  149.                     break;
  150.                 case 2:
  151.                     $content .= '<hr noshade="noshade" />';
  152.                     $content .= '<p class="quote-source" style="color: #' . get_option('qs_text_color', '000000') . '"><strong>Sources :</strong>' . PHP_EOL;
  153.                     $content .= '</br>';
  154.                     $lines = array();
  155.                     foreach($quotes as $quote) {
  156.                         if(!empty($quote['title'])) {
  157.                             if(!empty($quote['url'])) {
  158.                                 $lines[] = $quote['title'] . ': <a href="' . $quote['url'] . '" title="' . esc_attr( $quote['title'] ) . '" target="_blank">' . $quote['url'] . '</a>' . PHP_EOL;
  159.                             } else {
  160.                                 $lines[] = $quote['title'];
  161.                             }
  162.                         } else {
  163.                             $lines[] = '<a href="' . $quote['url'] . '" target="_blank">' . $quote['url'] . '</a>' . PHP_EOL;
  164.                         }
  165.                     }
  166.                     $content .= implode('<br />', $lines);
  167.                     $content .= '</p>';
  168.                     break;
  169.                 case 3:
  170.                     $content .= '<hr noshade="noshade" />';
  171.                     $content .= '<ul class="quote-source" style="color: #' . get_option('qs_text_color', '000000') . '"><strong>Sources :</strong>' . PHP_EOL;
  172.                     $lines = array();
  173.                     foreach($quotes as $quote) {
  174.                         if(!empty($quote['title'])) {
  175.                             if(!empty($quote['url'])) {
  176.                                 $lines[] = '<li><a href="' . $quote['url'] . '" title="' . esc_attr( $quote['title'] ) . '" target="_blank">' . $quote['title'] . '</a></li>' . PHP_EOL;
  177.                             } else {
  178.                                 $lines[] = '<li>' . $quote['title'] . '</li>' . PHP_EOL;
  179.                             }
  180.                         } else {
  181.                             $lines[] = '<li><a href="' . $quote['url'] . '" target="_blank">' . $quote['url'] . '</a></li>' . PHP_EOL;
  182.                         }
  183.                     }
  184.                     $content .= implode('', $lines);
  185.                     $content .= '</ul>';
  186.                     break;
  187.         }
  188.     }
  189.     return $content;
  190. }
  191. add_filter('the_content','quote_source');
  192. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement