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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
<?php
class Papi_Property_Link extends Papi_Property {
public $convert_type = 'object';
public $default_value = [];
protected $link_fields = [
'post_id',
'url',
'title',
'target'
];
public function delete_value( $slug, $post_id, $type ) {
$values = $this->load_value( null, $slug, $post_id );
$values = is_object( $values ) ? (array) $values : $values;
$result = true;
foreach ( array_keys( $values ) as $key ) {
$out = papi_delete_property_meta_value( $post_id, $slug . '_' . $key );
$result = $out ? $result : $out;
}
if ( $result ) {
$result = papi_delete_property_meta_value( $post_id, $slug );
}
return $result;
}
public function format_value( $value, $slug, $post_id ) {
return (object) $this->prepare_link_array( $value, $slug );
}
public function get_default_settings() {
return [];
}
public function load_value( $value, $slug, $post_id ) {
if ( is_array( $value ) || is_object( $value ) ) {
$values = $value;
} else {
$values = $this->link_fields;
foreach ( $values as $index => $key ) {
$values[$key] = papi_get_property_meta_value(
$post_id,
sprintf( '%s_%s', $slug, $key ),
$this->get_meta_type()
);
unset( $values[$index] );
}
}
return (object) $this->prepare_link_array( $values, $slug );
}
public function html() {
$value = $this->get_value();
$value = is_array( $value ) || is_object( $value ) ? $value : [];
$value = (object) $value;
$exists = ! empty( $value->url );
?>
<div class="papi-property-link" data-replace-slug="true" data-slug="<?php echo $this->html_name(); ?>">
<input type="hidden" name="<?php echo $this->html_name(); ?>" value="<?php echo $exists ? 1 : ''; ?>">
<?php if ( $exists ): ?>
<table class="papi-table link-table">
<tbody>
<tr>
<td><?php _e( 'URL', 'papi' ); ?></td>
<td>
<a href="<?php echo $value->url; ?>" target="_blank"><?php echo $value->url; ?></a>
<input type="hidden" value="<?php echo $value->title . ' - ' . $value->url; ?>" data-papi-rule="<?php echo $this->html_name(); ?>">
<input class="wp-link-url" type="hidden" value="<?php echo $value->url; ?>" name="<?php echo $this->html_name(); ?>[url]">
</td>
</tr>
<tr>
<td><?php _e( 'Title', 'papi' ); ?></td>
<td>
<?php echo $value->title; ?>
<input class="wp-link-text" type="hidden" value="<?php echo $value->title; ?>" name="<?php echo $this->html_name(); ?>[title]">
</td>
</tr>
<tr>
<td><?php _e( 'Target', 'papi' ); ?></td>
<td>
<?php echo $value->target === '_blank' ? __( 'New window', 'papi' ) : __( 'Same window', 'papi' ); ?>
<input class="wp-link-target" type="hidden" value="<?php echo $value->target; ?>" name="<?php echo $this->html_name(); ?>[target]">
</td>
</tr>
</tbody>
</table>
<?php endif; ?>
<p class="papi-file-select">
<span class="<?php echo empty( $value->url ) ? '' : 'papi-hide'; ?>">
<?php _e( 'No link selected', 'papi' ); ?>
<button class="button" data-link-action="add"><?php _e( 'Add link', 'papi' ); ?></button>
</span>
<span class="<?php echo empty( $value->url ) ? 'papi-hide' : ''; ?>">
<button class="button" data-link-action="edit"><?php _e( 'Edit link', 'papi' ); ?></button>
<button class="button" data-link-action="remove"><?php _e( 'Remove link', 'papi' ); ?></button>
</span>
</p>
</div>
<?php
}
public function import_value( $value, $slug, $post_id ) {
if ( is_array( $value ) || is_object( $value ) ) {
return $this->update_value( (array) $value, $slug, $post_id );
}
}
protected function prepare_link_array( $link, $slug ) {
$array = is_array( $link );
$values = (array) $link;
foreach ( $values as $key => $val ) {
unset( $values[$key] );
$key = preg_replace( '/^' . $slug . '\_/', '', $key );
$values[$key] = $val;
}
$link = (object) $values;
if ( ! isset( $link->url ) || empty( $link->url ) ) {
return $array ? (array) $link : $link;
}
if ( ! isset( $link->post_id ) || empty( $link->post_id ) ) {
$link->post_id = url_to_postid( $link->url );
}
if ( $link->post_id > 0 ) {
$link->url = get_permalink( $link->post_id );
}
if ( empty( $link->target ) ) {
$link->target = '_self';
}
if ( isset( $link->$slug ) && is_numeric( $link->$slug ) ) {
unset( $link->$slug );
}
return $array ? (array) $link : $link;
}
public function render_link_template() {
?>
<script type="text/template" id="tmpl-papi-property-link">
<table class="papi-table link-table">
<tbody>
<tr>
<td>
<?php _e( 'URL', 'papi' ); ?>
</td>
<td>
<%= link %>
<input type="hidden" value="<%= title %> - <%= href %>" data-papi-rule="<%= slug %>">
<input class="wp-link-url" type="hidden" value="<%= href %>" name="<%= slug %>[url]">
</td>
</tr>
<tr>
<td>
<?php _e( 'Title', 'papi' ); ?>
</td>
<td>
<%= title %>
<input class="wp-link-text" type="hidden" value="<%= title %>" name="<%= slug %>[title]">
</td>
</tr>
<tr>
<td>
<?php _e( 'Target', 'papi' ); ?>
</td>
<td>
<input class="wp-link-target" type="hidden" value="<%= target %>" name="<%= slug %>[target]">
<%= target === '_blank' ? '<?php _e( 'New window', 'papi' ) ?>' : '<?php _e( 'Same window', 'papi' ); ?>' %>
</td>
</tr>
</tbody>
</table>
</script>
<?php
}
public function update_value( $values, $slug, $post_id ) {
if ( ! isset( $values['url'] ) ) {
$values = $this->link_fields;
foreach ( $values as $index => $key ) {
$values[sprintf( '%s_%s', $slug, $key )] = '';
unset( $values[$index] );
}
$values[$slug] = '';
return $values;
}
$values = $this->prepare_link_array( $values, $slug );
foreach ( $values as $key => $val ) {
$values[sprintf( '%s_%s', $slug, $key )] = $val;
unset( $values[$key] );
}
$values[$slug] = 1;
return $values;
}
protected function setup_actions() {
add_action( 'admin_head', [$this, 'render_link_template'] );
}
}