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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
<?php
class Papi_Property extends Papi_Core_Property {
public function get_value() {
$value = $this->get_option( 'value' );
if ( papi_is_empty( $value ) ) {
$type = papi_get_meta_type();
$slug = $this->get_slug( true );
$value = papi_get_field( $slug, null, $type );
$post_status = get_post_status( $this->get_post_id() );
if ( papi_is_empty( $value ) && ( $post_status === false || $post_status === 'auto-draft' ) ) {
$value = $this->get_option( 'default' );
}
}
if ( papi_is_empty( $value ) ) {
return $this->default_value;
}
if ( $this->convert_type === 'string' ) {
$value = papi_convert_to_string( $value );
}
return papi_santize_data( $value );
}
public function html() {
}
public function is_option_page() {
return $this->get_store() instanceof Papi_Option_Store || papi_get_meta_type() === 'option';
}
public function render() {
if ( ! papi_current_user_is_allowed( $this->get_option( 'capabilities' ) ) ) {
return;
}
if ( $this->disabled() ) {
return;
}
if ( $this->get_option( 'lang' ) === strtolower( papi_get_qs( 'lang' ) ) ) {
$this->display = true;
} else {
$this->display = $this->get_option( 'lang' ) === false && papi_is_empty( papi_get_qs( 'lang' ) );
}
if ( $this->display() ) {
$this->display = $this->render_is_allowed_by_rules();
}
$this->render_row_html();
$this->render_hidden_html();
$this->render_rules_json();
}
protected function render_description_html() {
if ( papi_is_empty( $this->get_option( 'description' ) ) ) {
return;
}
papi_render_html_tag( 'p', [
papi_nl2br( $this->get_option( 'description' ) )
] );
}
protected function render_hidden_html() {
$slug = $this->get_option( 'slug' );
if ( substr( $slug, - 1 ) === ']' ) {
$slug = substr( $slug, 0, - 1 );
$slug = papi_get_property_type_key( $slug );
$slug .= ']';
} else {
$slug = papi_get_property_type_key( $slug );
}
$slug = papify( $slug );
$options = $this->get_options();
$property_json = base64_encode( papi_maybe_json_encode( $options ) );
papi_render_html_tag( 'input', [
'data-property' => strtolower( $this->get_option( 'type' ) ),
'name' => $slug,
'type' => 'hidden',
'value' => $property_json
] );
}
protected function render_label_html() {
$title = $this->get_option( 'title' );
papi_render_html_tag( 'label', [
'for' => $this->html_id(),
'title' => trim( $title . ' ' . papi_require_text( $this->get_options() ) ),
$title,
papi_required_html( $this )
] );
}
protected function render_property_html() {
papi_render_html_tag( 'div', [
'class' => 'papi-before-html ' . $this->get_option( 'before_class' ),
'data-property' => $this->get_option( 'type' ),
papi_maybe_get_callable_value( $this->get_option( 'before_html' ) )
] );
$this->html();
papi_render_html_tag( 'div', [
'class' => 'papi-after-html ' . $this->get_option( 'after_class' ),
'data-property' => $this->get_option( 'type' ),
papi_maybe_get_callable_value( $this->get_option( 'after_html' ) )
] );
}
protected function render_row_html() {
$display_class = $this->display() ? '' : ' papi-hide';
$rules_class = papi_is_empty( $this->get_rules() ) ? '' : ' papi-rules-exists';
$css_class = trim( $display_class . $rules_class );
$css_class .= sprintf( ' papi-layout-%s', $this->get_option( 'layout' ) );
if ( $this->get_option( 'raw' ) ) {
echo sprintf( '<div class="%s">', $css_class );
$this->render_property_html();
echo '</div>';
} else {
?>
<tr class="<?php echo $css_class; ?>">
<?php if ( $this->get_option( 'sidebar' ) ): ?>
<td class="papi-table-sidebar">
<?php
$this->render_label_html();
$this->render_description_html();
?>
</td>
<?php endif; ?>
<td <?php echo $this->get_option( 'sidebar' ) ? '' : 'colspan="2"'; ?>>
<?php
if ( $this->get_option( 'layout' ) === 'vertical' ) {
$this->render_label_html();
$this->render_description_html();
}
$this->render_property_html();
?>
</td>
</tr>
<?php
}
}
protected function render_rules_json() {
$rules = $this->get_rules();
if ( empty( $rules ) ) {
return;
}
$rules = $this->conditional->prepare_rules( $rules, $this );
papi_render_html_tag( 'script', [
'data-papi-rule-source-slug' => $this->html_name(),
'data-papi-rules' => 'true',
'type' => 'application/json',
json_encode( $rules )
] );
}
}