Advertisement
alchymyth

quote source file quote-source.php

Jan 26th, 2020
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.01 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
  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.         add_meta_box('quote_source_box', 'Quote up to 3 sources', 'quote_source_setup', $type, 'normal', 'high');
  26.     }
  27.     add_action('save_post','quote_source_save');
  28.  
  29. }
  30.  
  31. function quote_source_setup() {
  32.     global $post;
  33.     // get post metadata
  34.     $meta = get_post_meta($post->ID,'_quote_source',TRUE);
  35.     // include setup box
  36.     require_once('custom/meta.php');
  37.     // create a custom nonce
  38.     echo '<input type="hidden" name="quote_source_noncename" value="' . wp_create_nonce(__FILE__) . '" />';
  39. }
  40.  
  41. function quote_source_save($post_id) {
  42.     // authentication checks
  43.     // make sure data came from our meta box
  44.     if (!wp_verify_nonce($_POST['quote_source_noncename'],__FILE__)) {
  45.         return $post_id;
  46.     }
  47.  
  48.     // check user permissions
  49.     if ($_POST['post_type'] == 'page') {
  50.         if (!current_user_can('edit_page', $post_id)) {
  51.             return $post_id;
  52.         }
  53.     } else {
  54.         if (!current_user_can('edit_post', $post_id)) {
  55.             return $post_id;
  56.         }
  57.     }
  58.     // authentication passed, save data
  59.     // var types
  60.     // single: _quote_source[var]
  61.     // array: _quote_source[var][]
  62.     // grouped array: _quote_source[var_group][0][var_1], _quote_source[var_group][0][var_2]
  63.     $current_data = get_post_meta($post_id, '_quote_source', TRUE);
  64.     $new_data = $_POST['_quote_source'];
  65.     quote_source_clean($new_data);
  66.     if ($current_data) {
  67.         if (is_null($new_data)) {
  68.             delete_post_meta($post_id,'_quote_source');
  69.         } else {
  70.             update_post_meta($post_id,'_quote_source',$new_data);
  71.         }
  72.     } elseif (!is_null($new_data)) {
  73.         add_post_meta($post_id,'_quote_source',$new_data,TRUE);
  74.     }
  75.     return $post_id;
  76. }
  77.  
  78. function quote_source_clean(&$arr) {
  79.     if (is_array($arr)) {
  80.         foreach ($arr as $i => $v) {
  81.             if (is_array($arr[$i])) {
  82.                 quote_source_clean($arr[$i]);
  83.                 if (!count($arr[$i])) {
  84.                     unset($arr[$i]);
  85.                 }
  86.             } else {
  87.                 if (trim($arr[$i]) == '') {
  88.                     unset($arr[$i]);
  89.                 }
  90.             }
  91.         }
  92.         if (!count($arr)) {
  93.             $arr = NULL;
  94.         }
  95.     }
  96. }
  97.  
  98. function quote_source($content = '') {
  99.     /*
  100.     INPUT: the post contents
  101.     OUTPUT: the post contents with quoted source at specified position & format
  102.     */
  103.     global $post;
  104.     $meta = get_post_meta($post->ID,'_quote_source',TRUE);
  105.     $quotes = array();
  106.    
  107.     $src_cnt = 0;
  108.     for($i = 0; $i < 3; $i++) {
  109.         $tmp_url = isset( $meta['quote' . ($i+1)] ) ? $meta['quote'. ($i+1)] : '';
  110.         $tmp_title = isset( $meta['quote' . ($i+1) .'_title'] ) ? $meta['quote' . ($i+1) .'_title'] : '';
  111.         if(!empty($tmp_url) || !empty($tmp_title)) {
  112.             $quotes[$src_cnt] = array();
  113.             $quotes[$src_cnt]['url'] = $tmp_url;
  114.             $quotes[$src_cnt]['title'] = $tmp_title;
  115.             $src_cnt++;
  116.         }
  117.     }
  118.     if($src_cnt == 0) {
  119.         return $content;
  120.     }
  121.    
  122.     if((is_single()     && get_option('qs_mode_single_post', 'true')    == 'true') ||
  123.         (is_category()  && get_option('qs_mode_category', 'false')      == 'true') ||
  124.         (is_page()      && get_option('qs_mode_page', 'false')          == 'true') ||
  125.         ((is_home() || is_archive() || is_paged()) && get_option('qs_mode_summary', 'false') == 'true')) {
  126.         // count sources
  127.         /*$src_cnt = 0;
  128.         $src_selected = 0;
  129.         if(!empty($s1_title) || !empty($s1)) {
  130.             $src_selected = 1;
  131.             $src_cnt++;
  132.         }
  133.         if(!empty($s2_title) || !empty($s2)) {
  134.             $src_selected = 2;
  135.             $src_cnt++;
  136.         }
  137.         if(!empty($s3_title) || !empty($s3)) {
  138.             $src_selected = 3;
  139.             $src_cnt++;
  140.         }*/
  141.         if($src_cnt == 1) {
  142.             // have only 1 quote
  143.             $content .= '<hr noshade="noshade" />';
  144.             $selected_link = $quotes[0]['url'];
  145.             $selected_title = $quotes[0]['title'];
  146.             $content .= '<p style="color: #' . get_option('qs_text_color', '000000') . '"><strong>Source</strong> :</br> ';
  147.             if(!empty($selected_title)) {
  148.                 if(!empty($selected_link)) {
  149.                     $content .= '<a href="' . $selected_link . '" title="' . esc_attr( $selected_title ) . '" target="_blank">' . $selected_title . '</a>';
  150.                 } else {
  151.                     $content .= $selected_title;
  152.                 }
  153.                 $content .= PHP_EOL;
  154.             } elseif(!empty($selected_link)) {
  155.                 $content .= '<a href="' . $selected_link . '" target="_blank">' . $selected_link . '</a>' . PHP_EOL;
  156.             }
  157.             $content .= '</p>';
  158.            
  159.         } else {
  160.             $display_mode = (int)get_option('qs_layout', 1);
  161.             $content .= '<hr noshade="noshade" />';
  162.             $content .= '<p style="color: #' . get_option('qs_text_color', '000000') . '"><strong>Sources :</strong>';
  163.             switch($display_mode) {
  164.                 case 1:
  165.                     $content .= '</br>';
  166.                     $lines = array();
  167.                     foreach($quotes as $quote) {
  168.                         if(!empty($quote['title'])) {
  169.                             if(!empty($quote['url'])) {
  170.                                 $lines[] = '<a href="' . $quote['url'] . '" title="' . esc_attr( $quote['title'] ) . '" target="_blank">' . $quote['title'] . '</a>' . PHP_EOL;
  171.                             } else {
  172.                                 $lines[] = $quote['title'] . PHP_EOL;
  173.                             }
  174.                         } else {
  175.                             $lines[] = '<a href="' . $quote['url'] . '" target="_blank">' . $quote['url'] . '</a>' . PHP_EOL;
  176.                         }
  177.                     }
  178.                     $content .= implode('</br>', $lines);
  179.                     $content .= '</p>';
  180.                     break;
  181.                 case 2:
  182.                     $content .= '</br>';
  183.                     $lines = array();
  184.                     foreach($quotes as $quote) {
  185.                         if(!empty($quote['title'])) {
  186.                             if(!empty($quote['url'])) {
  187.                                 $lines[] = $quote['title'] . ': <a href="' . $quote['url'] . '" title="' . esc_attr( $quote['title'] ) . '" target="_blank">' . $quote['url'] . '</a>' . PHP_EOL;
  188.                             } else {
  189.                                 $lines[] = $quote['title'];
  190.                             }
  191.                         } else {
  192.                             $lines[] = '<a href="' . $quote['url'] . '" target="_blank">' . $quote['url'] . '</a>' . PHP_EOL;
  193.                         }
  194.                     }
  195.                     $content .= implode('<br />', $lines);
  196.                     $content .= '</p>';
  197.                     break;
  198.                 case 3:
  199.                     $content .= '<ul>';
  200.                     $lines = array();
  201.                     foreach($quotes as $quote) {
  202.                         if(!empty($quote['title'])) {
  203.                             if(!empty($quote['url'])) {
  204.                                 $lines[] = '<li><a href="' . $quote['url'] . '" title="' . esc_attr( $quote['title'] ) . '" target="_blank">' . $quote['title'] . '</a></li>' . PHP_EOL;
  205.                             } else {
  206.                                 $lines[] = '<li>' . $quote['title'] . '</li>' . PHP_EOL;
  207.                             }
  208.                         } else {
  209.                             $lines[] = '<li><a href="' . $quote['url'] . '" target="_blank">' . $quote['url'] . '</a></li>' . PHP_EOL;
  210.                         }
  211.                     }
  212.                     $content .= implode('', $lines);
  213.                     $content .= '</ul></p>';
  214.                     break;
  215.             }
  216.         }
  217.     }
  218.     return $content;
  219. }
  220. add_filter('the_content','quote_source');
  221. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement