class-wpml-tm-post-edit-custom-field-settings-menu.php 2.98 KB
Newer Older
Den Isahac's avatar
Den Isahac committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
<?php

class WPML_TM_Post_Edit_Custom_Field_Settings_Menu extends WPML_SP_User {

	/** @var  WPML_Custom_Field_Setting_Factory $setting_factory */
	private $setting_factory;

	/** @var WP_Post $post */
	private $post;

	private $rendered = false;

	/**
	 * WPML_TM_Post_Edit_Custom_Field_Settings_Menu constructor.
	 *
	 * @param SitePress                         $sitepress
	 * @param WPML_Custom_Field_Setting_Factory $settings_factory
	 * @param WP_Post                           $post
	 */
	public function __construct( &$sitepress, &$settings_factory, $post ) {
		parent::__construct( $sitepress );
		$this->setting_factory = &$settings_factory;
		$this->post            = $post;
	}

	/**
	 * @return string
	 */
	public function render() {
		$custom_keys = (array) $this->sitepress->get_wp_api()->get_post_custom_keys( $this->post->ID );
		$custom_keys = $this->setting_factory->filter_custom_field_keys( $custom_keys );
		ob_start();
		if ( 0 !== count( $custom_keys ) ) {
			?>
			<table class="widefat">
				<thead>
				<tr>
					<th colspan="2"><?php _e( 'Custom fields', 'sitepress' ); ?></th>
				</tr>
				</thead>
				<tbody>
				<?php
				foreach ( $custom_keys as $cfield ) {
					$field_setting = $this->setting_factory->post_meta_setting( $cfield );
					if ( $field_setting->excluded() ) {
						continue;
					}
					$this->rendered = true;
					$radio_disabled = $field_setting->is_read_only() ? 'disabled="disabled"' : '';
					$status         = $field_setting->status();
					$checked0       = $status == WPML_IGNORE_CUSTOM_FIELD ? ' checked="checked"' : '';
					$checked1       = $status == WPML_COPY_CUSTOM_FIELD ? ' checked="checked"' : '';
					$checked2       = $status == WPML_TRANSLATE_CUSTOM_FIELD ? ' checked="checked"' : '';
					?>
					<tr>
						<td id="icl_mcs_cf_<?php echo base64_encode( $cfield ); ?>"><?php echo $cfield; ?></td>
						<td align="right">
							<label><input class="icl_mcs_cfs"
							              name="icl_mcs_cf_<?php echo base64_encode( $cfield ); ?>"
							              type="radio"
							              value="0" <?php echo $radio_disabled . $checked0 ?> />&nbsp;<?php _e( "Don't translate", 'sitepress' ) ?>
							</label>
							<label><input class="icl_mcs_cfs"
							              name="icl_mcs_cf_<?php echo base64_encode( $cfield ); ?>"
							              type="radio"
							              value="1" <?php echo $radio_disabled . $checked1 ?> />&nbsp;<?php _e( "Copy", 'sitepress' ) ?>
							</label>
							<label><input class="icl_mcs_cfs"
							              name="icl_mcs_cf_<?php echo base64_encode( $cfield ); ?>"
							              type="radio"
							              value="2" <?php echo $radio_disabled . $checked2 ?> />&nbsp;<?php _e( "Translate", 'sitepress' ) ?>
							</label>
						</td>
					</tr>
					<?php
				}
				?>
				</tbody>
			</table>
			<br/>
			<?php
		}

		return ob_get_clean();
	}

	/**
	 * @return bool true if there were actual custom fields to display options for
	 */
	public function is_rendered() {

		return $this->rendered;
	}
}