web/wp-content/plugins/xili-language/xili-xl-bbp-addon.php
changeset 194 32102edaa81b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
       
     1 <?php
       
     2 /*
       
     3 Plugin Name: xili-xl-bbp-addon
       
     4 Plugin URI: http://dev.xiligroup.com
       
     5 Description: add multilingual functions and features to bbPress (Localized forums). Delivered in xili-language package. If option activated, bbPress default theme will use bbpress.css in your theme directory.
       
     6 Version: 2.8.1
       
     7 Author: MS
       
     8 Author URI: http://dev.xiligroup.com
       
     9 */
       
    10 /*
       
    11 
       
    12 Changelog:
       
    13 2.8.1: - 20120915 - Initial release as class
       
    14 
       
    15 */
       
    16 
       
    17 define('XILIXLBBPADDON_VER','2.8.1');
       
    18 
       
    19 class xili_xl_bbp_addon {
       
    20 	
       
    21 	var $plugin_name = 'xili-xl-bbp-addon'; // filename and folder
       
    22 	var $plugin_folder = 'xili-language';
       
    23 	var $display_plugin_name = '©xili xl-bbPress add-on'; // menu and top
       
    24 	var $settings_name = 'xili-xl-bbp-addon_settings'; // The settings string name for this plugin in options table
       
    25 	var $xili_settings = array(); 
       
    26 	var $xili_settings_ver = '1.0';
       
    27 	var $plugin_local = "'xili_xl_bbp_addon'"; // text domain
       
    28 	var $settings_list = "xili_xl_bbp_addon_list"; // used by settings sections and fields in settings page
       
    29 	var $url = '';
       
    30 	var $urlpath = ''; // The path to this plugin - see construct
       
    31 	
       
    32 	var $debug ; //WP_DEBUG
       
    33 
       
    34 	//Class Functions
       
    35 	/**
       
    36 	 * PHP 4 Compatible Constructor
       
    37 	 */
       
    38 	function xili_xl_bbp_addon(){ $this->__construct(); }
       
    39 
       
    40 	/**
       
    41 	 * PHP 5 Constructor
       
    42 	 */		
       
    43 	function __construct(){
       
    44 		
       
    45 		$this->debug = ( defined ('WP_DEBUG') ) ? WP_DEBUG : false ;
       
    46 		
       
    47 		load_plugin_textdomain( 'xili_xl_bbp_addon', false, $this->plugin_folder.'/languages' );
       
    48 
       
    49 		register_activation_hook( __FILE__, array(&$this,'get_xili_settings') ); // first activation
       
    50 		
       
    51 		$this->url = plugins_url(basename(__FILE__), __FILE__);
       
    52 		$this->urlpath = plugins_url('', __FILE__);	
       
    53 		
       
    54 		//Initialize the options
       
    55 		$this->get_xili_settings();
       
    56 		//Admin menu
       
    57 		
       
    58 		add_action( 'admin_menu', array(&$this, 'admin_menu_link') );
       
    59 		add_action( 'admin_init', array(&$this, 'admin_init') );
       
    60 		
       
    61 		if ( is_admin() ) {
       
    62 			add_filter ( 'xiliml_manage_column_name', array(&$this,'xiliml_manage_column_name'), 10, 3);
       
    63 			add_filter ( 'xiliml_language_translated_in_column', array(&$this,'xiliml_language_translated_in_column'), 10, 3);
       
    64 		}
       
    65 		//Actions both side
       
    66 		
       
    67 		add_action( 'init', array(&$this,'plugin_init') );
       
    68 		add_action( 'bbp_enqueue_scripts', array(&$this,'bbp_custom_css_enqueue') );
       
    69 		
       
    70 		
       
    71 		// front-end side
       
    72 		
       
    73 		add_action( 'xiliml_add_frontend_mofiles', array(&$this,'xiliml_add_frontend_mofiles'), 10 ,2);
       
    74 		if ( ! is_admin() ) { 
       
    75 			add_action( 'save_post', array(&$this,'bbp_save_topic_or_reply'), 10, 2 );
       
    76 			add_action( 'parse_query', array(&$this,'bbpress_parse_query') ); // fixe issues in bbp 2.1
       
    77 		}
       
    78 	}
       
    79 
       
    80 	function plugin_init() {
       
    81 		$this->get_xili_settings();
       
    82 	}
       
    83 
       
    84 	
       
    85 	/**
       
    86 	 * Retrieves the plugin options from the database.
       
    87 	 * @return array
       
    88 	 */
       
    89 	function get_xili_settings() {
       
    90 		if (!$xili_settings = get_option( $this->settings_name )) {
       
    91 			$xili_settings = array(
       
    92 			 	'css-theme-folder' => false,
       
    93 				'version'=> $this->xili_settings_ver  // see on top class
       
    94 			);
       
    95 			update_option( $this->settings_name, $xili_settings);
       
    96 		}
       
    97 		$this->xili_settings = $xili_settings;
       
    98 	}
       
    99 	
       
   100 	/** change default style - inspired from Jared Atchison **/
       
   101 	function bbp_custom_css_enqueue(){
       
   102 		if ( isset( $this->xili_settings['css-theme-folder'] ) && $this->xili_settings['css-theme-folder']  ){
       
   103 			// Unregister default bbPress CSS
       
   104 			wp_deregister_style( 'bbp-default-bbpress' );
       
   105 	
       
   106 			// Register new CSS file in our active theme directory
       
   107 			wp_enqueue_style( 'bbp-default-bbpress', get_stylesheet_directory_uri() . '/bbpress.css' );
       
   108 		}
       
   109 	}
       
   110 
       
   111 	
       
   112 	/** change bbp mo file **/
       
   113 	function xiliml_add_frontend_mofiles ( $theme_domain, $cur_iso_lang ) { // only called in front-end
       
   114 	
       
   115 		unload_textdomain( 'bbpress' );
       
   116 	
       
   117 		//error_log ( $theme_domain . '--------------' . $cur_iso_lang );
       
   118 		load_textdomain( 'bbpress', WP_LANG_DIR . '/bbpress/bbpress-'.$cur_iso_lang.'.mo' );
       
   119 	}
       
   120 	
       
   121 	
       
   122 
       
   123 	function bbp_save_topic_or_reply ( $post_ID, $post ) {
       
   124 		global $xili_language;
       
   125 	//test if topic or reply
       
   126 		if ( in_array ( $post->post_type, array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ) ) ) {
       
   127 	// get language of parent forum
       
   128 			$parent_lang =  $xili_language->get_post_language ( $post->post_parent ) ;
       
   129 	// set taxonomy to language
       
   130 			if ( $parent_lang != '') {
       
   131 				wp_set_object_terms( $post_ID, $parent_lang, TAXONAME );
       
   132 			}
       
   133 		}
       
   134 	}
       
   135 	
       
   136 	
       
   137 	/** 
       
   138 		* fixe issue in bbPress 2.1
       
   139 	 */
       
   140 	function bbpress_parse_query ( $wp_query ) {
       
   141 		$bbp = bbpress() ;
       
   142 		if ( isset ( $wp_query->query_vars['post_type' ] ) && version_compare( $bbp->version, '2.2', '<') ) { 
       
   143 	// announced to be fixed in bbp 2.2 - tracs 1947 - 4216
       
   144 			if ( is_array ( $wp_query->query_vars['post_type' ] ) ) {
       
   145 				if ( $wp_query->query_vars['post_type' ] = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ) ) {
       
   146 					//error_log (' QUERY '.serialize ( $wp_query->query_vars['post_type' ] ));
       
   147 					$wp_query->is_home = false ;
       
   148 				}
       
   149 			}
       
   150 		}
       
   151 	}
       
   152 	
       
   153 		
       
   154 	function xiliml_manage_column_name ( $ends, $cols, $post_type ) {
       
   155 		if ( in_array ( $post_type, array( bbp_get_forum_post_type(), bbp_get_topic_post_type(), bbp_get_reply_post_type() ) ) ) {
       
   156 		//error_log ( '----------->'.$post_type);
       
   157 		$ends = array( 'author', 'comments', 'date', 'rel', 'visible');
       
   158 		}
       
   159 		return $ends;
       
   160 	}
       
   161 	
       
   162 	function xiliml_language_translated_in_column ( $output, $result, $post_type ) {
       
   163 		
       
   164 		if ( in_array ( $post_type, array( bbp_get_forum_post_type(), bbp_get_topic_post_type(), bbp_get_reply_post_type() ) ) ) {
       
   165 			$output = '';		
       
   166 			if ( $result == '' ) {
       
   167 				$output .= '.' ;
       
   168 			} else {
       
   169 				$output .= __('linked in:', 'xili_xl_bbp_addon') ;
       
   170 				$output .= '&nbsp;<span class="translated-in">' . $result .'</span>'; 	
       
   171 			}
       
   172 		}
       
   173 		
       
   174 		return $output;
       
   175 	}
       
   176 	
       
   177 	/**
       
   178 	 * Adds the options subpanel
       
   179 	 */
       
   180 	function admin_menu_link() {
       
   181 		add_options_page( $this->plugin_name, __($this->display_plugin_name, 'xili_xl_bbp_addon'), 'manage_options', __FILE__, array(&$this,'admin_options_page'));
       
   182 		add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array(&$this, 'filter_plugin_actions'), 10, 2 );
       
   183 	}
       
   184 	
       
   185 	/**
       
   186 	 * Admin init - section - fields
       
   187 	 */
       
   188 	function admin_init() {
       
   189 		register_setting ($this->settings_name, $this->settings_name, array(&$this,'xili_settings_validate_options') );
       
   190 		add_settings_section ( $this->settings_list.'_section_1', __('List of options ', 'xili_xl_bbp_addon') , array(&$this,'xili_settings_section_1_draw') , $this->settings_list );
       
   191 		
       
   192 		add_settings_field ( $this->settings_list.'_css-theme-folder', __('bbPress default style file in theme', 'xili_xl_bbp_addon'), array(&$this,'xili_settings_field_1_draw'), $this->settings_list, $this->settings_list.'_section_1');
       
   193 		
       
   194 	}
       
   195 	
       
   196 	function xili_settings_section_1_draw () {
       
   197 		echo '<p>'. __('This plugin shipped with xili-language package is an addon to activate multilingual features to bbPress with xili-language. Some other options are possible.', 'xili_xl_bbp_addon')  . '</p>';
       
   198 		
       
   199 	}
       
   200 	
       
   201 	function xili_settings_field_1_draw () {
       
   202 		// not checked - not saved in settings
       
   203 		$checked = ( isset ( $this->xili_settings['css-theme-folder'] ) && $this->xili_settings['css-theme-folder'] ) ? "checked='checked'" : "";
       
   204 		
       
   205 		echo "<input value = 'true' id='{$this->settings_name}[css-theme-folder]' name='{$this->settings_name}[css-theme-folder]' type='checkbox' {$checked}  />"; 
       
   206 		
       
   207 	}
       
   208 	
       
   209 	function xili_settings_validate_options ( $input ) {
       
   210 	
       
   211 		$valid = $input;
       
   212 		$valid['version'] = $this->xili_settings_ver ; // because not in input !	
       
   213 		return $valid;	
       
   214 	}
       
   215 
       
   216 	/**
       
   217 	 * Adds the Settings link to the plugin activate/deactivate page
       
   218 	 */
       
   219 	function filter_plugin_actions($links, $file) {
       
   220 	   $settings_link = '<a href="options-general.php?page=' . $this->plugin_folder .'/'. basename(__FILE__) . '">' . __('Settings') . '</a>';
       
   221 	   array_unshift( $links, $settings_link ); // before other links
       
   222 
       
   223 	   return $links;
       
   224 	}
       
   225 
       
   226 	/**
       
   227 	 * Adds settings/options page
       
   228 	 */
       
   229 	function admin_options_page() { ?>
       
   230 		<div class="wrap">
       
   231     		<?php screen_icon(); ?>
       
   232     		<h2><?php printf(__( '%s settings', 'xili_xl_bbp_addon'), $this->display_plugin_name ); ?></h2>
       
   233     		<form action="options.php" method="post" >
       
   234     		 	<?php 
       
   235 				do_settings_sections( $this->settings_list );
       
   236     			settings_fields( $this->settings_name ); // hidden fields and referrer and nonce
       
   237     			?>
       
   238     			<?php submit_button( __('Save Changes'), 'secondary' ); // 'primary' = by default ?>
       
   239     		</form>
       
   240     		<h4><a href="http://dev.xiligroup.com/<?php echo $this->plugin_name; ?>" title="Plugin page and docs" target="_blank" style="text-decoration:none" ><img style="vertical-align:middle" src="<?php echo plugins_url( 'images/'.$this->plugin_name.'-logo-32.jpg', __FILE__ ) ;  ?>" alt="<?php echo $this->display_plugin_name; ?> logo"/>&nbsp;<?php echo $this->display_plugin_name; ?> </a> - © <a href="http://dev.xiligroup.com" target="_blank" title="<?php _e('Author'); ?>" >xiligroup.com</a>™ - msc 2012 - v. <?php echo XILIXLBBPADDON_VER; ?></h4>
       
   241     	</div>
       
   242 	
       
   243 		 			
       
   244 		<?php
       
   245 	}
       
   246 	
       
   247 } //End Class
       
   248 
       
   249 // bbPress admin Language (user locale)
       
   250 function xili_xl_bbp_lang_init ( ) { 
       
   251 	if ( is_admin() )
       
   252 		add_filter( 'bbpress_locale', 'xili_bbp_admin_side_locale', 10 );
       
   253 }
       
   254 
       
   255 function xili_bbp_admin_side_locale ( $locale = 'en_US') {
       
   256 	
       
   257 	$locale = get_user_option( 'user_locale' ); 
       
   258 
       
   259 		if ( empty( $locale ) ) {
       
   260 			$locale = ( defined( 'WPLANG' ) ) ? WPLANG : 'en_US';
       
   261 
       
   262 			if ( is_multisite() ) {
       
   263 				if ( defined( 'WP_INSTALLING' ) || ( false === $ms_locale = get_option( 'WPLANG' ) ) )
       
   264 					$ms_locale = get_site_option( 'WPLANG' );
       
   265 
       
   266 			if ( $ms_locale !== false )
       
   267 					$locale = $ms_locale;
       
   268 			}
       
   269 		}
       
   270 		return $locale;
       
   271 }
       
   272 add_action( 'plugins_loaded', 'xili_xl_bbp_lang_init', 9 ); // 9 = to be registered before bbPress instantiate
       
   273 
       
   274 // INIT and ERROR
       
   275 
       
   276 function xili_xl_bbp_addon_init () {  
       
   277 	if ( function_exists ('bbpress') ) 
       
   278 		$bbp = bbpress() ;
       
   279 	if ( class_exists ('xili_language') && version_compare( XILILANGUAGE_VER, '2.8.0', '>')  && class_exists ('bbpress') && version_compare( $bbp->version, '2.1.2', '>=')  ) {
       
   280 		global $xili_xl_bbp_addon;
       
   281 		$xili_xl_bbp_addon['main'] = new xili_xl_bbp_addon();
       
   282 	} else {
       
   283 		add_action( 'admin_notices', 'xili_xl_bbp_addon_need_xl' );
       
   284 		return;
       
   285 	}
       
   286 	/* - not used yet
       
   287 	if ( is_admin() ) {
       
   288 		$plugin_path = dirname(__FILE__) ; //error_log( $plugin_path );
       
   289 		require ( $plugin_path . '/includes/class-admin.php' );
       
   290 	    $xili_xl_extended['admin'] = new xili_xl_template_admin();
       
   291 	}
       
   292 	*/
       
   293 }
       
   294 
       
   295 add_action( 'plugins_loaded', 'xili_xl_bbp_addon_init', 17); // after xili-tidy-tags
       
   296 
       
   297 function xili_xl_bbp_addon_need_xl() {
       
   298 		global $wp_version;
       
   299 		load_plugin_textdomain( 'xili_language_errors', false, 'xili-language/languages' );
       
   300 		echo '<div id="message" class="error fade"><p>';
       
   301 		echo '<strong>'.__( 'Installation of both xili-language AND bbPress is not completed.', 'xili_language_errors' ) . '</strong>';
       
   302 		echo '<br />';
       
   303 		echo '</p></div>';
       
   304 } 
       
   305 
       
   306 
       
   307 ?>