| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Settings class for custom post types and taxonomies language and translation management |
| 5 |
* |
| 6 |
* @since 1.8 |
| 7 |
*/ |
| 8 |
class PLL_Settings_CPT extends PLL_Settings_Module { |
| 9 |
private $post_types, $disabled_post_types, $taxonomies, $disabled_taxonomies; |
| 10 |
|
| 11 |
/** |
| 12 |
* Constructor |
| 13 |
* |
| 14 |
* @since 1.8 |
| 15 |
* |
| 16 |
* @param object $polylang polylang object |
| 17 |
*/ |
| 18 |
public function __construct( &$polylang ) { |
| 19 |
parent::__construct( |
| 20 |
$polylang, |
| 21 |
array( |
| 22 |
'module' => 'cpt', |
| 23 |
'title' => __( 'Custom post types and Taxonomies', 'polylang' ), |
| 24 |
'description' => __( 'Activate languages and translations management for the custom post types and the taxonomies.', 'polylang' ), |
| 25 |
) |
| 26 |
); |
| 27 |
|
| 28 |
$public_post_types = get_post_types( array( 'public' => true, '_builtin' => false ) ); |
| 29 |
/** This filter is documented in include/model.php */ |
| 30 |
$this->post_types = array_unique( apply_filters( 'pll_get_post_types', $public_post_types, true ) ); |
| 31 |
|
| 32 |
$programmatically_active_post_types = array_unique( apply_filters( 'pll_get_post_types', array(), false ) ); |
| 33 |
/** This filter is documented in include/model.php */ |
| 34 |
$this->disabled_post_types = array_intersect( $programmatically_active_post_types, $this->post_types ); |
| 35 |
|
| 36 |
$public_taxonomies = get_taxonomies( array( 'public' => true, '_builtin' => false ) ); |
| 37 |
$public_taxonomies = array_diff( $public_taxonomies, get_taxonomies( array( '_pll' => true ) ) ); |
| 38 |
/** This filter is documented in include/model.php */ |
| 39 |
$this->taxonomies = array_unique( apply_filters( 'pll_get_taxonomies', $public_taxonomies, true ) ); |
| 40 |
|
| 41 |
$programmatically_active_taxonomies = array_unique( apply_filters( 'pll_get_taxonomies', array(), false ) ); |
| 42 |
/** This filter is documented in include/model.php */ |
| 43 |
$this->disabled_taxonomies = array_intersect( $programmatically_active_taxonomies, $this->taxonomies ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Tells if the module is active |
| 48 |
* |
| 49 |
* @since 1.8 |
| 50 |
* |
| 51 |
* @return bool |
| 52 |
*/ |
| 53 |
public function is_active() { |
| 54 |
return ! empty( $this->post_types ) || ! empty( $this->taxonomies ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Displays the settings form |
| 59 |
* |
| 60 |
* @since 1.8 |
| 61 |
*/ |
| 62 |
protected function form() { |
| 63 |
if ( ! empty( $this->post_types ) ) {?> |
| 64 |
<h4><?php esc_html_e( 'Custom post types', 'polylang' ); ?></h4> |
| 65 |
<ul class="pll-inline-block-list"> |
| 66 |
<?php |
| 67 |
foreach ( $this->post_types as $post_type ) { |
| 68 |
$pt = get_post_type_object( $post_type ); |
| 69 |
if ( ! empty( $pt ) ) { |
| 70 |
$disabled = in_array( $post_type, $this->disabled_post_types ); |
| 71 |
printf( |
| 72 |
'<li><label><input name="post_types[%s]" type="checkbox" value="1" %s %s/> %s</label></li>', |
| 73 |
esc_attr( $post_type ), |
| 74 |
checked( in_array( $post_type, $this->options['post_types'] ) || $disabled, true, false ), |
| 75 |
disabled( $disabled, true, false ), |
| 76 |
esc_html( $pt->labels->name ) |
| 77 |
); |
| 78 |
} |
| 79 |
} |
| 80 |
?> |
| 81 |
</ul> |
| 82 |
<p class="description"><?php esc_html_e( 'Activate languages and translations for custom post types.', 'polylang' ); ?></p> |
| 83 |
<?php |
| 84 |
} |
| 85 |
|
| 86 |
if ( ! empty( $this->taxonomies ) ) { |
| 87 |
?> |
| 88 |
<h4><?php esc_html_e( 'Custom taxonomies', 'polylang' ); ?></h4> |
| 89 |
<ul class="pll-inline-block-list"> |
| 90 |
<?php |
| 91 |
foreach ( $this->taxonomies as $taxonomy ) { |
| 92 |
$tax = get_taxonomy( $taxonomy ); |
| 93 |
if ( ! empty( $tax ) ) { |
| 94 |
$disabled = in_array( $taxonomy, $this->disabled_taxonomies ); |
| 95 |
printf( |
| 96 |
'<li><label><input name="taxonomies[%s]" type="checkbox" value="1" %s %s/> %s</label></li>', |
| 97 |
esc_attr( $taxonomy ), |
| 98 |
checked( in_array( $taxonomy, $this->options['taxonomies'] ) || $disabled, true, false ), |
| 99 |
disabled( $disabled, true, false ), |
| 100 |
esc_html( $tax->labels->name ) |
| 101 |
); |
| 102 |
} |
| 103 |
} |
| 104 |
?> |
| 105 |
</ul> |
| 106 |
<p class="description"><?php esc_html_e( 'Activate languages and translations for custom taxonomies.', 'polylang' ); ?></p> |
| 107 |
<?php |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Sanitizes the settings before saving |
| 113 |
* |
| 114 |
* @since 1.8 |
| 115 |
* |
| 116 |
* @param array $options |
| 117 |
*/ |
| 118 |
protected function update( $options ) { |
| 119 |
$newoptions = array(); |
| 120 |
|
| 121 |
foreach ( array( 'post_types', 'taxonomies' ) as $key ) { |
| 122 |
$newoptions[ $key ] = empty( $options[ $key ] ) ? array() : array_keys( $options[ $key ], 1 ); |
| 123 |
} |
| 124 |
return $newoptions; // Take care to return only validated options |
| 125 |
} |
| 126 |
} |
| 127 |
|