Last active
August 29, 2015 14:12
-
-
Save eri-trabiccolo/4837d112e8a549792420 to your computer and use it in GitHub Desktop.
Install the Customizr-Polylang bridge code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==================================================================================================================== | |
// Customizr-Polylang bridge: Makes the Customizr theme or the Featured Pages Unlimited | |
// plugin compatible with Polylang; Runs only if Polylang is active; | |
// ==================================================================================================================== | |
// If Polylang is active, hook function on the admin pages | |
if ( function_exists( 'pll_register_string' ) ) add_action( 'admin_init', 'pll_tc_strings_setup' ); | |
function pll_tc_strings_setup() { | |
// Find out what code we're running for our featured pages. | |
$running_fpu = ( class_exists( 'TC_fpu' ) ); | |
$running_customizr = ( ( wp_get_theme()->Template ) == 'customizr' ); | |
$running_customizr_pro = ( ( wp_get_theme()->Template ) == 'customizr-pro' ); | |
$pll_tc_options = array(); | |
$pll_tc_areas = array(); | |
$pll_tc_archive_titles = array( | |
'tag' => 'Tag pages title', | |
'cat' => 'Category pages titles', | |
'author' => 'Author pages titles', | |
'search' => 'Search results page titles' | |
); | |
$button_text_option_prefix = 'tc_featured_page_'; | |
if ( $running_customizr || $running_customizr_pro ) { // Running Customizr(-pro) alone | |
// Get Customizr(-pro) options | |
$pll_tc_options = TC_utils::$inst->tc_get_theme_options(); | |
if ( $running_customizr ){ // get array of featured pages / set Polylang's translation group | |
$pll_tc_areas = TC_init::$instance->fp_ids; | |
$fp_polylang_group = $polylang_group = 'Customizr'; | |
} | |
} | |
if ( $running_fpu && class_exists('TC_utils_fpu') ) { // Running Featured Pages Unlimited plugin (included in Customizr-pro) : Setup FPU-defined featured page strings | |
// Get FPU options / get array of featured pages / set Polylang's translation group | |
$option_name = $running_customizr_pro ? 'tc_pro_fpu' : 'tc_unlimited_featured_pages'; | |
$_pll_tc_options = array_merge(TC_utils_fpu::$instance -> default_options, get_option( $option_name ) ); | |
$pll_tc_options = array_merge($pll_tc_options, $_pll_tc_options); | |
$_pll_tc_areas = TC_utils_fpu::$instance->tc_get_custom_fp_nb(); | |
$pll_tc_areas = array_unique( array_merge($pll_tc_areas, $_pll_tc_areas) ); | |
$fp_polylang_group = 'Featured Pages Unlimited'; | |
$button_text_option_prefix = 'tc_fp_'; | |
} | |
if ( $running_customizr_pro ) | |
$fp_polylang_group = $polylang_group = 'Customizr-Pro'; | |
// Add featured pages button text to Polylang's string translation panel | |
if ( isset( $pll_tc_options[ $button_text_option_prefix . 'button_text'] ) ) | |
pll_register_string( 'Featured pages button text', esc_attr($pll_tc_options[ $button_text_option_prefix . 'button_text']), $fp_polylang_group ); | |
// Add featured pages excerpt text to Polylang's string translation panel | |
foreach ( $pll_tc_areas as $area ) | |
if ( isset( $pll_tc_options['tc_featured_text_'.$area] ) ) | |
pll_register_string( 'Featured text '.$area.'(200 char. max)', esc_attr($pll_tc_options['tc_featured_text_'.$area]), $fp_polylang_group ); | |
// Add front page slider name to Polylang's string translation panel | |
if ( isset( $pll_tc_options['tc_front_slider'] ) ) | |
pll_register_string( 'Front page slider name', esc_attr($pll_tc_options['tc_front_slider']), $polylang_group ); | |
// Add archive title strings to Polylang's string translation panel | |
foreach ( $pll_tc_archive_titles as $opt => $name ) | |
if ( isset( $pll_tc_options["tc_{$opt}_title"] ) ) | |
pll_register_string( $name, esc_attr($pll_tc_options["tc_${opt}_title"]), $polylang_group ); | |
} | |
// front add_filters hooked to 'after_setup_theme' since, when the theme is Customizr-pro, FPU is fired with it | |
add_action('after_setup_theme', 'perform_pll'); | |
function perform_pll(){ | |
// If Polylang is active, translate/swap featured page buttons/text/link and slider | |
if ( function_exists( 'pll_get_post' ) && function_exists( 'pll__' ) ) { | |
$pll_tc_archive_titles = array( 'tag_archive', 'category_archive', 'author_archive', 'search_results'); | |
// Set hook prefix if running FPU. If not, default to Customizr. (Hooks won't be called anyway if neither is running) | |
$pll_tc_hook_prefix = ( class_exists( 'TC_fpu' ) ) ? 'fpc_' : 'tc_fp_'; | |
// Substitute any page id with the equivalent page in current language (if found) | |
add_filter( $pll_tc_hook_prefix.'id', 'pll_tc_page_id' ); | |
function pll_tc_page_id( $fp_page_id ) { | |
return is_int( pll_get_post( $fp_page_id ) ) ? pll_get_post( $fp_page_id ) : $fp_page_id; | |
} | |
// Substitute the featured page button text with the current language button text | |
add_filter( $pll_tc_hook_prefix.'button_text', 'pll__' ); | |
// Substitute the featured page text with the translated featured page text | |
add_filter( $pll_tc_hook_prefix.'text', 'pll__' ); | |
// Substitute any registered slider name | |
add_filter( 'tc_slider_name_id', 'pll__' ); | |
foreach ( $pll_tc_archive_titles as $title ) | |
add_filter("tc_{$title}_title", 'pll__' , 20); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment