| 1 |
<?php |
| 2 |
/** |
| 3 |
* Simple_Urls_Admin file. |
| 4 |
* |
| 5 |
* @package simple-urls |
| 6 |
*/ |
| 7 |
|
| 8 |
use LassoLite\Classes\Helper; |
| 9 |
|
| 10 |
/** |
| 11 |
* Simple_Urls_Admin class. |
| 12 |
*/ |
| 13 |
class Simple_Urls_Admin { |
| 14 |
|
| 15 |
/** |
| 16 |
* Constructor. |
| 17 |
*/ |
| 18 |
public function __construct() { |
| 19 |
add_filter( 'post_updated_messages', array( $this, 'updated_message' ) ); |
| 20 |
add_action( 'admin_menu', array( $this, 'add_meta_box' ) ); |
| 21 |
add_action( 'save_post', array( $this, 'meta_box_save' ), 1, 2 ); |
| 22 |
add_action( 'manage_posts_custom_column', array( $this, 'columns_data' ) ); |
| 23 |
add_filter( 'manage_edit-surl_columns', array( $this, 'columns_filter' ) ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Colum filter. |
| 28 |
* |
| 29 |
* @param array $columns Columns. |
| 30 |
* |
| 31 |
* @return array Filtered columns. |
| 32 |
*/ |
| 33 |
public function columns_filter( $columns ) { |
| 34 |
|
| 35 |
$columns = array( |
| 36 |
'cb' => '<input type="checkbox" />', |
| 37 |
'title' => __( 'Title', 'simple-urls' ), |
| 38 |
'url' => __( 'Redirect to', 'simple-urls' ), |
| 39 |
'permalink' => __( 'Permalink', 'simple-urls' ), |
| 40 |
'clicks' => __( 'Clicks', 'simple-urls' ), |
| 41 |
); |
| 42 |
|
| 43 |
return $columns; |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Columns data. |
| 49 |
* |
| 50 |
* @param array $column Columns. |
| 51 |
*/ |
| 52 |
public function columns_data( $column ) { |
| 53 |
|
| 54 |
global $post; |
| 55 |
|
| 56 |
$url = get_post_meta( $post->ID, '_surl_redirect', true ); |
| 57 |
$count = get_post_meta( $post->ID, '_surl_count', true ); |
| 58 |
|
| 59 |
$allowed_tags = array( |
| 60 |
'a' => array( |
| 61 |
'href' => array(), |
| 62 |
'rel' => array(), |
| 63 |
), |
| 64 |
); |
| 65 |
|
| 66 |
if ( 'url' === $column ) { |
| 67 |
echo wp_kses( make_clickable( esc_url( $url ? $url : '' ) ), $allowed_tags ); |
| 68 |
} elseif ( 'permalink' === $column ) { |
| 69 |
echo wp_kses( make_clickable( get_permalink() ), $allowed_tags ); |
| 70 |
} elseif ( 'clicks' === $column ) { |
| 71 |
echo esc_html( $count ? $count : 0 ); |
| 72 |
} |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Update message. |
| 78 |
* |
| 79 |
* @param array $messages Messages. |
| 80 |
* |
| 81 |
* @return array Messages. |
| 82 |
*/ |
| 83 |
public function updated_message( $messages ) { |
| 84 |
|
| 85 |
$surl_object = get_post_type_object( 'surl' ); |
| 86 |
|
| 87 |
$messages['surl'] = $surl_object->labels->messages; |
| 88 |
|
| 89 |
$permalink = get_permalink(); |
| 90 |
|
| 91 |
if ( $permalink ) { |
| 92 |
foreach ( $messages['surl'] as $id => $message ) { |
| 93 |
$messages['surl'][ $id ] = sprintf( $message, $permalink ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return $messages; |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Add metabox. |
| 103 |
*/ |
| 104 |
public function add_meta_box() { |
| 105 |
add_meta_box( 'surl', __( 'URL Information', 'simple-urls' ), array( $this, 'meta_box' ), 'surl', 'normal', 'high' ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Metabox. |
| 110 |
*/ |
| 111 |
public function meta_box() { |
| 112 |
|
| 113 |
global $post; |
| 114 |
|
| 115 |
printf( '<input type="hidden" name="_surl_nonce" value="%s" />', esc_attr( wp_create_nonce( plugin_basename( __FILE__ ) ) ) ); |
| 116 |
|
| 117 |
printf( '<p><label for="%s">%s</label></p>', '_surl_redirect', esc_html__( 'Redirect URI', 'simple-urls' ) ); |
| 118 |
printf( '<p><input style="%s" type="text" name="%s" id="%s" value="%s" /></p>', 'width: 99%;', '_surl_redirect', '_surl_redirect', esc_attr( get_post_meta( $post->ID, '_surl_redirect', true ) ) ); |
| 119 |
printf( '<p><span class="description">%s</span></p>', esc_html__( 'This is the URL that the Redirect Link you create on this page will redirect to when accessed in a web browser.', 'simple-urls' ) ); |
| 120 |
|
| 121 |
$count = isset( $post->ID ) ? get_post_meta( $post->ID, '_surl_count', true ) : 0; |
| 122 |
/* translators: %d is the counter of clicks. */ |
| 123 |
echo '<p>' . sprintf( esc_html__( 'This URL has been accessed %d times', 'simple-urls' ), esc_attr( $count ) ) . '</p>'; |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Metabox save function. |
| 129 |
* |
| 130 |
* @param string $post_id Post Id. |
| 131 |
* @param WP_Post $post Post. |
| 132 |
*/ |
| 133 |
public function meta_box_save( $post_id, $post ) { |
| 134 |
|
| 135 |
$key = '_surl_redirect'; |
| 136 |
$post_request = Helper::POST(); |
| 137 |
|
| 138 |
// Verify the nonce. |
| 139 |
// phpcs:ignore |
| 140 |
if ( ! isset( $post_request['_surl_nonce'] ) || ! wp_verify_nonce( $post_request['_surl_nonce'], plugin_basename( __FILE__ ) ) ) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
// Don't try to save the data under autosave, ajax, or future post. |
| 145 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 146 |
return; |
| 147 |
}; |
| 148 |
|
| 149 |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 150 |
return; |
| 151 |
}; |
| 152 |
|
| 153 |
if ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
| 154 |
return; |
| 155 |
}; |
| 156 |
|
| 157 |
// Is the user allowed to edit the URL? |
| 158 |
if ( ! current_user_can( 'edit_posts' ) || 'surl' !== $post->post_type ) { |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
// phpcs:ignore |
| 163 |
$value = $post_request[ $key ] ?? ''; |
| 164 |
|
| 165 |
if ( $value ) { |
| 166 |
// Save/update. |
| 167 |
update_post_meta( $post->ID, $key, $value ); |
| 168 |
} else { |
| 169 |
// Delete if blank. |
| 170 |
delete_post_meta( $post->ID, $key ); |
| 171 |
} |
| 172 |
|
| 173 |
} |
| 174 |
} |
| 175 |
|