PluginProbe
Lasso Lite – Affiliate Link Manager & Product Displays / 111
Lasso Lite – Affiliate Link Manager & Product Displays v111
158 157 155 156 154 153 152 151 150 149 148 trunk 0.9.9 104 105 106 107 108 109 110 111 112 113 114 115 All 57 releases
simple-urls / includes / class-simple-urls-admin.php

class-simple-urls-admin.php in Lasso Lite – Affiliate Link Manager & Product Displays 111, at includes/class-simple-urls-admin.php

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