Advertisement
Guest User

ps-multilingual-edit-post.php *Excerpt Support*

a guest
Oct 8th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.78 KB | None | 0 0
  1. <?php
  2. /**
  3.  * edit multilingual post
  4.  *
  5.  * Main Custom Multilingual for Edit Post include Class
  6.  *
  7.  * Author: Ouhinit(Wangbin)
  8.  *
  9.  * @package ps_multilingual_edit_post
  10.  */
  11. /*
  12.  * 投稿、ページ、カスタム投稿の編集などを多国語に対応する
  13.  * このクラスの作成に関しては、「custom-field-gui-utility」(CSS)(edit_meta_value)と「Custom Field Template」(tinyMCE)を参考させていただきました。
  14.  *  Tomohiro Okuwaki(Web屋かたつむりくん)さんとHiroaki Miyashitaさんに心より感謝しております。
  15.  */
  16. Class ps_multilingual_edit_post {
  17.  
  18.     /*
  19.      * 多国語化カスタムフィールドのprefix
  20.      */
  21.     var $prefix_meta_keys = array( 'post_title_', 'post_content_', 'post_excerpt_' );
  22.    
  23.     /*
  24.      *コンストラクタ.
  25.      */
  26.     function __construct( ) {
  27.         $this->init( );
  28.     }
  29.    
  30.     /*
  31.      * initializing
  32.      */
  33.     function init( ){
  34.         global $ps_multi_languages;
  35.  
  36.         if ( !isset( $ps_multi_languages ) ) return;
  37.  
  38.         //対応する多国語
  39.         $this->_items = $ps_multi_languages->multilingual;
  40.         //国旗画像ディレクトリ
  41.         $this->flags_dir = $ps_multi_languages->flags_dir;
  42.         //デフォルトの言語
  43.         $this->WPLANGKEY = $ps_multi_languages->WPLANGKEY;
  44.         //デフォルトの一覧の多国語表示個数
  45.         $this->max_count = $ps_multi_languages->max_count;
  46.  
  47.         $this->Start( );
  48.  
  49.     }
  50.    
  51.     /*
  52.      *プラグインの機能実行をスタート
  53.      */
  54.     function Start(){
  55.         if ( is_admin( ) ):
  56.            
  57.             //多国語のメタボックスを生成する
  58.             add_action ('add_meta_boxes'                            , array(&$this,'insert_multilingual_custom_field') );
  59.            
  60.             /* edit_post : 投稿記事またはページが更新・編集された際に実行する。これには、コメントが追加・更新された場合(投稿またはページのコメント数が更新される)も含む。 */
  61.             add_action('edit_post'                                  , array(&$this,'edit_meta_value'), 10, 2 );
  62.            
  63.             /* save_post : インポート機能の利用、記事・ページ編集フォームの利用、XMLRPCでの投稿、メールでの投稿のうちいずれかの方法で記事・ページが作成・更新された際に実行する。 */
  64.             add_action( 'save_post'                                 , array( &$this, 'edit_meta_value'), 10, 2 );
  65.            
  66.             /* publish_post : 投稿記事が公開された際、または公開済みの記事の情報が編集された際に実行する。 */
  67.             add_action( 'publish_post'                              , array( &$this, 'edit_meta_value') );
  68.            
  69.             /* transition_post_status : 記事・ページが公開された際、またはステータスが「公開」に変更された場合に実行する。 */
  70.             add_action( 'transition_post_status'                    , array( &$this, 'edit_meta_value') );
  71.            
  72.             //投稿を新規と編集する場合、JSファイルをCSSファイルを読み込みする
  73.             add_action( 'admin_print_styles-post.php'               , array( &$this, 'add_admin_print_styles' ) );
  74.             add_action( 'admin_print_styles-post-new.php'           , array( &$this, 'add_admin_print_styles' ) );
  75.            
  76.             //投稿、ページ、カスタム投稿の一覧多国語表示
  77.             add_filter( 'manage_posts_columns'                          , array(&$this,'add_multilingual_posts_columns' ) );
  78.             add_filter( 'manage_page_posts_columns'                     , array(&$this,'add_multilingual_posts_columns' ));
  79.             add_action( 'manage_posts_custom_column'                    , array(&$this,'add_multilingual_scompt_custom_column'), 10, 2);
  80.             add_action( 'manage_page_posts_custom_column'               , array(&$this,'add_multilingual_scompt_custom_column'), 10, 2);       
  81.            
  82.         endif;
  83.     }
  84.    
  85.    
  86.     /**
  87.     * ファンクション名:add_multilingual_meta_boxes
  88.     * 機能概要:各多国語のメターボックスを追加する
  89.     * 作成:プライム・ストラテジー株式会社 王 濱
  90.     * 変更:
  91.     * @param String $post_type
  92.     * @param Object $post
  93.     * @return
  94.     */ 
  95.     function insert_multilingual_custom_field( $post_type = 'post', $post = NULL ){
  96.         add_meta_box('insert_multilingual_custom_field', "Multilingual Settings", array(&$this,'add_multilingual_meta_boxes'), $post_type, 'normal', 'high');
  97.     }
  98.    
  99.     /**
  100.     * ファンクション名:add_admin_print_styles
  101.     * 機能概要:JSとCSSを読み込み
  102.     * 作成:プライム・ストラテジー株式会社 王 濱
  103.     * 変更:
  104.     * @param
  105.     * @return
  106.     */ 
  107.     function add_admin_print_styles( ){
  108.  
  109.         //wp_enqueue_script( 'prefix-js-tinyMCEID' , plugins_url('js/tinyMCEID.js', __FILE__) );
  110.                
  111.         wp_enqueue_script( 'prefix-js-' . strtolower(__CLASS__) , plugins_url('js/edit-post-prefix-js.js', __FILE__) );
  112.        
  113.         wp_enqueue_style( 'prefix-style-' . strtolower(__CLASS__), plugins_url('css/edit-post-prefix-style.css', __FILE__)  );
  114.  
  115.     }
  116.    
  117.     /**
  118.     * ファンクション名:
  119.     * 機能概要:一覧項目のカスタマイズ
  120.     * 作成:プライム・ストラテジー株式会社 王 濱
  121.     * 変更:
  122.     * @param resource
  123.     * @param int
  124.     * @param string
  125.     * @return
  126.     */
  127.     function add_multilingual_posts_columns( $columns ){
  128.  
  129.         $columns['title'] = $columns['title'] . '&nbsp;<img src="' .  $this->flags_dir . $this->WPLANGKEY . '.png' . '">';
  130.         foreach ( $this->_items as $key => $val ){
  131.             $count = $count + 1 ;
  132.             if ( $count > $this->max_count ){
  133.                 break;
  134.             }
  135.             $columns['post_title_'.$key] = __('Title').'&nbsp;<img src="' . $this->flags_dir .  $key . '.png">';
  136.         }
  137.             return $columns;
  138.     }
  139.    
  140.     /**
  141.     * ファンクション名:add_multilingual_scompt_custom_column
  142.     * 機能概要:一覧項目のカスタマイズ
  143.     * 作成:プライム・ストラテジー株式会社 王 濱
  144.     * 変更:
  145.     * @param resource
  146.     * @param int
  147.     * @param string
  148.     * @return
  149.     */
  150.     function add_multilingual_scompt_custom_column( $column_name, $id ){
  151.         foreach ( $this->_items as $key => $val ){
  152.             if( $column_name == 'post_title_' . $key ) {
  153.                 $multilingual = get_post_meta( $id , 'post_title_' . $key , true );
  154.                 if ( $multilingual ){
  155.                     $edit_link = get_edit_post_link( $id );
  156.                     echo '<a title="'.sprintf(__('Edit &#8220;%s&#8221;'), $multilingual) .'" href="'.$edit_link.'" class="row-title">'.$multilingual.'</a>';
  157.                 }
  158.             }
  159.         }
  160.     }  
  161.    
  162.     /**
  163.     * ファンクション名:add_multilingual_meta_boxes
  164.     * 機能概要:各多国語のメターボックスを追加する
  165.     * 作成:プライム・ストラテジー株式会社 王 濱
  166.     * 変更:
  167.     * @param Object $post
  168.     * @return
  169.     */ 
  170.     function add_multilingual_meta_boxes( $post ){
  171.         //
  172.         $this->add_multilingual_postbox_title( );
  173.        
  174.         foreach ( $this->_items as $key => $val ):
  175.             //hrのHTMLタグを生成する
  176.             $this->add_multilingual_hr($key);
  177.             //TitleのHTMLタグを生成する
  178.             $this->add_multilingual_title($key,$post);
  179.             //ContentのHTMLタグを生成する
  180.             $this->add_multilingual_content($key,$post );
  181.             //ExcerptのHTMLタグを生成する
  182.             $this->add_multilingual_excerpt($key,$post);
  183.         endforeach;
  184.        
  185.     }
  186.  
  187.     /**
  188.     * ファンクション名:add_multilingual_postbox_title
  189.     * 機能概要:各多国語のメターボックスのタイトル
  190.     * 作成:プライム・ストラテジー株式会社 王 濱
  191.     * 変更:
  192.     * @param
  193.     * @return
  194.     */ 
  195.     function add_multilingual_postbox_title( ){
  196.         echo '<h5 class="postbox_hr post" id="postbox_hr_postbox_title" >Sections Setting</h5>';
  197.        
  198.         $flag_icon_path = $this->flags_dir . $this->WPLANGKEY . '.png';
  199.         if ( $this->url_exists( $flag_icon )):
  200.             $flag_icon = $flag_icon_path;  
  201.         endif;
  202.         $hidden = '<input type="hidden" name="default_language" id="default_language" value="'. $flag_icon .'" />';
  203.         $hidden .= '<input type="hidden" name="multilingual-verify-key" id="multilingual-verify-key" value="'. wp_create_nonce('multilingual') .'" />';
  204.        
  205.         echo $hidden;  
  206.  
  207.     }
  208.  
  209.     /**
  210.     * ファンクション名:add_multilingual_hr
  211.     * 機能概要:各多国語の設定セクション表題
  212.     * 作成:プライム・ストラテジー株式会社 王 濱
  213.     * 変更:
  214.     * @param String $lang
  215.     * @return
  216.     */ 
  217.     function add_multilingual_hr( $lang ){
  218.         $lang_name = $this->_items[$lang];
  219.         $flag_icon = $this->flags_dir . $lang . '.png';
  220.         if ( $this->url_exists( $flag_icon )):
  221.             $flag_icon = '&nbsp; <img src="' .  $flag_icon. '">';  
  222.         endif;
  223.         echo '<h5 class="postbox_hr post page" id="postbox_hr2">Setting Information for ' . $lang_name . $flag_icon . '</h5>';
  224.        
  225.     }
  226.    
  227.     /**
  228.     * ファンクション名:add_multilingual_title
  229.     * 機能概要:各多国語のタイトルの表示と設定
  230.     * 作成:プライム・ストラテジー株式会社 王 濱
  231.     * 変更:
  232.     * @param String $lang
  233.     * @param Object $post
  234.     * @return
  235.     */ 
  236.     function add_multilingual_title( $lang , $post = null ){
  237.         $Title = __( 'Title' );
  238.         $post_ml_title = get_post_meta($post->ID,'post_title_' . $lang , true );
  239.         $inside = <<< EOF
  240.             <div id="ml_post_title_{$lang}_box" class="postbox textfield post page">
  241.                 <h4 class="cf_title">{$this->_items[$lang]} {$Title}</h4>
  242.                 <div class="inside">
  243.                     <p class="ml_input">
  244.                         <input type="text" size="138" value="{$post_ml_title}" name="ml_post_title_{$lang}" id="ml_post_title_{$lang}" class="data errPosRight">
  245.                     </p>
  246.                     <p class="ml_sample">Please enter the title.</p>
  247.                 </div>
  248.             </div>
  249. EOF;
  250.  
  251.         echo $inside;
  252.        
  253.     }
  254.    
  255.     /**
  256.     * ファンクション名:add_multilingual_content
  257.     * 機能概要:各多国語の本文内容の表示と設定
  258.     * 作成:プライム・ストラテジー株式会社 王 濱
  259.     * 変更:
  260.     * @param String $lang
  261.     * @param Object $post
  262.     * @return
  263.     */ 
  264.     function add_multilingual_content( $lang, $post = null ){
  265.  
  266.         $defaults = array(    
  267.             'tabindex'      => 1,
  268.             'wpautop'       => true,
  269.             'media_buttons' => true,
  270.             'textarea_name' => 'content',
  271.             'textarea_rows' => get_option('default_post_edit_rows', 10), // rows="..."                
  272.             'tabindex'      => '',        
  273.             'editor_css'    => '', // intended for extra styles for both visual and HTML editors buttons, needs to include the <style> tags, can use "scoped".        
  274.             'editor_class'  => '', // add extra class(es) to the editor textarea        
  275.             'teeny'         => false, // output the minimal editor config used in Press This        
  276.             'dfw'           => false, // replace the default fullscreen with DFW (needs specific DOM elements and css)        
  277.             'tinymce'       => true, // load TinyMCE, can be used to pass settings directly to TinyMCE using an array()        
  278.             'quicktags'     => true, // load Quicktags, can be used to pass settings directly to Quicktags using an array()        
  279.             'textarea_name' => "ml_post_content_{$lang}"
  280.         );  
  281.  
  282.         $atts = wp_parse_args( $args, $defaults );
  283.         $post_ml_content = get_post_meta($post->ID,'post_content_' . $lang , true );
  284.  
  285.         $Content = __('Content');
  286.  
  287.         $inside = <<< EOF
  288.         <h4 class="cf_title" >{$this->_items[$lang]} {$Content}</h4>
  289. EOF;
  290.         echo $inside;
  291.         wp_editor( $post_ml_content, "ml_post_content_{$lang}" , $atts );
  292.         return;
  293.  
  294.         /*$Add_Media = __('Add Media');
  295.         $Upload_Insert = sprintf(__('Upload/Insert %s'),'');
  296.         $Visual = __('Visual');
  297.         $Content = __('Content');
  298.         $post_ml_content = get_post_meta($post->ID,'post_content_' . $lang , true );
  299.         $inside = <<< EOF
  300.         <div class="postbox textarea post page news faq" id="ml_post_content_{$lang}_box">
  301.         <h4 class="cf_title" >{$this->_items[$lang]} {$Content}</h4>
  302.         <div class="inside">
  303.         <script type="text/javascript">
  304.             // <![CDATA[
  305.             jQuery(document).ready(function() {
  306.                 if ( typeof tinyMCE != "undefined" ) {
  307.                     document.getElementById("ml_post_content_{$lang}").value = switchEditors.wpautop(document.getElementById("ml_post_content_{$lang}").value);
  308.                     var ed = new tinyMCE.Editor("ml_post_content_{$lang}", tinyMCEPreInit.mceInit["content"]);
  309.                     ed.render();
  310.                     tinyMCEID.push("ml_post_content_{$lang}");
  311.                 }
  312.             }
  313.             );
  314.             // ]]>
  315.         </script>
  316.        
  317.         <div>
  318.             <span><label for="add_media_ml_post_content_{$lang}">{$Upload_Insert}</label></span>
  319.             <a href="media-upload.php?TB_iframe=true" id="add_media_ml_post_content_{$lang}" title='{$Add_Media}' onclick="focusTextArea('ml_post_content_{$lang}'); jQuery(this).attr('href',jQuery(this).attr('href').replace('\?','?post_id='+jQuery('#post_ID').val())); return thickbox(this);">
  320.                 <img src='images/media-button.png' alt="{$Add_Media}" />
  321.             </a>
  322.        
  323.             <div id="ml_post_content_{$lang}_div_visual" class="wp-editor-tools tmce-active">
  324.                 <a href="#" id="ml_post_content_{$lang}_a_html"  class="hide-if-no-js wp-switch-editor switch-html" onclick="switchMode(jQuery(this).parent().parent().parent().find('textarea').attr('id'),'html'); return false;">HTML
  325.                 </a>
  326.                 <a href="#" id="ml_post_content_{$lang}_a_visual" class="hide-if-no-js wp-switch-editor switch-tmce"  onclick="switchMode(jQuery(this).parent().parent().parent().find('textarea').attr('id'),'visual'); return false;">{$Visual}
  327.                 </a>
  328.             </div>
  329.        
  330.             <script type="text/javascript">
  331.                 // <![CDATA[
  332.                         jQuery(document).ready(function() { new QTags("ml_post_content_{$lang}"); QTags._buttonsInit();  jQuery("#qt_ml_post_content_{$lang}_toolbar").hide(); });
  333.                 // ]]>
  334.             </script>
  335.        
  336.             <div class="wp-editor-container" id="editorcontainer_ml_post_content_{$lang}">
  337.                 <textarea class="data" id="ml_post_content_{$lang}" name="ml_post_content_{$lang}" type="textfield" rows="20" cols="137">{$post_ml_content}</textarea>
  338.             </div>
  339.             </div>
  340.                 <p class="ml_sample">Please enter the content.</p>
  341.             </div>
  342.         </div>
  343. EOF;
  344.         echo $inside;*/
  345.     }
  346.    
  347.     /**
  348.     * ファンクション名:add_multilingual_excerpt
  349.     * 機能概要:各多国語のタイトルの表示と設定
  350.     * 作成:プライム・ストラテジー株式会社 王 濱
  351.     * 変更:
  352.     * @param String $lang
  353.     * @param Object $post
  354.     * @return
  355.     */ 
  356.     function add_multilingual_excerpt( $lang , $post = null ){
  357.         $Excerpt = __( 'Excerpt' );
  358.         $post_ml_excerpt = get_post_meta($post->ID,'post_excerpt_' . $lang , true );
  359.         $inside = <<< EOF
  360.             <div id="ml_post_excerpt_{$lang}_box" class="postbox textfield post page">
  361.                 <h4 class="cf_excerpt">{$this->_items[$lang]} {$Excerpt}</h4>
  362.                 <div class="inside">
  363.                     <p class="ml_input">
  364. <textarea rows="1" cols="40" name="ml_post_excerpt_{$lang}" tabindex="6" id="ml_post_excerpt_{$lang}" style="margin: 0px; width: 782px; height: 48px; ">{$post_ml_excerpt}</textarea>
  365.                     </p>
  366.                     <p class="ml_sample">Please enter the excerpt.</p>
  367.                 </div>
  368.             </div>
  369. EOF;
  370.  
  371.         echo $inside;
  372.        
  373.     }
  374.  
  375.     /**
  376.     * ファンクション名:edit_meta_value
  377.     * 機能概要:多国語のタイトルと内容をカスタムフィールドに登録する
  378.     * 作成:プライム・ストラテジー株式会社 王 濱
  379.     * 変更:
  380.     * @param Int $post_id
  381.     * @param Object $post
  382.     * @return
  383.     */ 
  384.     function edit_meta_value( $post_id ,$post=null){       
  385.    
  386.         if ( $post_id == 0) {
  387.             return $post_id;
  388.         }
  389.                
  390.         global $wpdb;
  391.         if (!current_user_can('edit_post', $post_id)) {
  392.             return $post_id;
  393.         }
  394.  
  395.         $nonce = isset($_REQUEST['multilingual-verify-key']) ? $_REQUEST['multilingual-verify-key']: '';
  396.  
  397.         if (!wp_verify_nonce($nonce, 'multilingual')) {
  398.             return $post_id;
  399.         }
  400.    
  401.         foreach ( $this->_items as $key => $val ):
  402.             foreach ($this->prefix_meta_keys as $prefix_meta_key) :
  403.                 $name = 'ml_' . $prefix_meta_key . $key;
  404.                
  405.                 $meta_key = $prefix_meta_key . $key;
  406.  
  407.                 $meta_value = isset($_REQUEST["{$name}"]) ? stripslashes(trim($_REQUEST["{$name}"])): '';          
  408.  
  409.                 if (isset($meta_value) && !empty($meta_value)) {
  410.                
  411.                     $_meta_value = get_post_meta($post_id, $meta_key, true);
  412.    
  413.                     if ( $_meta_value == $meta_value && $meta_value != "" ){
  414.                         continue;
  415.                     }elseif( $meta_value == '' ){
  416.                         if ( !isset($_REQUEST["$name"]) )
  417.                         delete_post_meta($post_id, $meta_key);
  418.                     }else{
  419.                         update_post_meta($post_id, $meta_key, $meta_value);
  420.                     }
  421.                 } elseif (isset($meta_value) && strval($meta_value) === '0') {
  422.                     add_post_meta($post_id, $meta_key, '0');
  423.                 } else {
  424.                     delete_post_meta($post_id, $meta_key);
  425.                 }
  426.             endforeach;
  427.         endforeach;
  428.        
  429.     }
  430.  
  431.     /**
  432.     * ファンクション名:url_exists
  433.     * 機能概要:URLのありなし確認
  434.     * 作成:プライム・ストラテジー株式会社 王 濱
  435.     * 変更:
  436.     * @param string $url
  437.     * @return Boolean true/false
  438.     */ 
  439.     function url_exists($url) {
  440.         if (!$fp = curl_init($url)) return false;
  441.         return true;
  442.     }
  443.    
  444. }//class end
  445.  
  446. $ps_multilingual_edit_post = new ps_multilingual_edit_post( );
  447.  
  448. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement