-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink-in-bio-wp.php
411 lines (348 loc) · 11.6 KB
/
link-in-bio-wp.php
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<?php
/**
* Template code for a WordPress plugin.
*
* @package linkinbio
*/
/*
-------------------------------------------------------------------------------
Plugin Name: Link In Bio WP
Plugin URI: https://wordpress.org/plugins/link-in-bio-wp/
Description: Add a link in bio page for use on social media pages.
Text Domain: linkinbio
Author: sgarza
Author URI: https://profiles.wordpress.org/sgarza/
Contributors: imforza
License: GPLv3 or later
License URI: https://www.gnu.org/licenses/gpl-3.0.en.html
Version: 1.2.0
------------------------------------------------------------------------------
*/
/* Exit if accessed directly. */
if ( ! defined( 'ABSPATH' ) ) { exit; }
/* Include dependencies */
include_once( 'includes.php' );
/** Instantiate the plugin. */
WP_LinkInBio::get_instance();
/**
* WP_LinkInBio class.
*
* @package linkinbio
* @todo Change class name to be unique to your plugin.
**/
class WP_LinkInBio {
/**
* Plugin Basename.
*
* IE: wordpress-plugin-template/plugin-template.php
*
* @var [String]
*/
public static $plugin_base_name;
/**
* Path to current plugin directory.
*
* @var [String]
*/
public static $plugin_base_dir;
/**
* Path to plugin base file.
*
* @var [String]
*/
public static $plugin_file;
/**
* Plugin Constructor.
*/
private function __construct() {
/* Define Constants */
static::$plugin_base_name = plugin_basename( __FILE__ );
static::$plugin_base_dir = plugin_dir_path( __FILE__ );
static::$plugin_file = __FILE__;
$this->init();
}
public static function get_plugin_base_name(){
if ( ! isset( static::$plugin_base_name ) ) {
static::$plugin_base_name = plugin_basename( __FILE__ );
}
return static::$plugin_base_name;
}
public static function get_plugin_base_dir(){
if ( ! isset( static::$plugin_base_dir ) ) {
static::$plugin_base_dir = plugin_dir_path( __FILE__ );
}
return static::$plugin_base_dir;
}
public static function get_plugin_file(){
if ( ! isset( static::$plugin_file ) ) {
static::$plugin_file = __FILE__;
}
return static::$plugin_file;
}
/**
* Singleton instantiator.
*
* @return WP_LinkInBio Single instance of plugin class.
*/
public static function get_instance() {
static $instance;
if ( ! isset( $instance ) ) {
$instance = new WP_LinkInBio();
}
return $instance;
}
/**
* Initialize Plugin.
*/
private function init() {
/* Language Support */
load_plugin_textdomain( 'linkinbio', false, dirname( static::$plugin_base_name ) . '/languages' );
/* Plugin Activation/De-Activation. */
register_activation_hook( static::$plugin_file, array( $this, 'activate' ) );
register_deactivation_hook( static::$plugin_file, array( $this, 'deactivate' ) );
add_image_size( 'image_link', 250, 250, true );
/** Enqueue css and js files */
add_action( 'wp_enqueue_scripts', array( $this, 'frontend_scripts' ) );
/* Add link to settings in plugins admin page */
add_filter( 'plugin_action_links_' . static::$plugin_base_name , array( $this, 'plugin_links' ) );
add_action( 'init', array( $this, 'create_post_type' ) );
add_action( 'init', array( $this, 'register_post_meta' ) );
add_action( 'init', array( $this, 'add_rewrites' ) );
add_filter( 'template_include', array($this, 'include_template') );
add_filter( 'pre_get_posts', array( $this, 'posts_per_page' ) );
// No need to define metaboxes/save post function unless it's accessible.
if ( is_admin() ) {
add_filter( 'manage_edit-link-in-bio_columns', array( $this, 'columns_filter' ) );
add_action( 'manage_link-in-bio_posts_custom_column', array( $this, 'columns_data' ) );
add_action( 'add_meta_boxes', array( $this, 'register_meta_boxes' ) );
add_action( 'save_post_link-in-bio', array( $this, 'metabox_save' ), 1, 2 );
}
add_action('update_option_linkinbio_landing_page_custom_slug', array( $this, 'update_custom_slug'));
}
/**
* Register the post type.
*
* @return void
*/
public function create_post_type() {
$args = array(
'labels' => array(
'name' => __( 'Links', 'linkinbio' ),
'singular_name' => __( 'Link', 'linkinbio' ),
'add_new' => __( 'Add New', 'linkinbio' ),
'add_new_item' => __( 'Add New Link', 'linkinbio' ),
'edit' => __( 'Edit', 'linkinbio' ),
'edit_item' => __( 'Edit Link', 'linkinbio' ),
'new_item' => __( 'New Link', 'linkinbio' ),
'view' => __( 'View Link', 'linkinbio' ),
'view_item' => __( 'View Link', 'linkinbio' ),
'search_items' => __( 'Search Link', 'linkinbio' ),
'not_found' => __( 'No Link found', 'linkinbio' ),
'not_found_in_trash' => __( 'No Link found in Trash', 'linkinbio' ),
'filter_items_list' => __( 'Filter Link', 'linkinbio' ),
'items_list_navigation' => __( 'Link navigation', 'linkinbio' ),
'items_list' => __( 'Link list', 'linkinbio' ),
),
'supports' => array( 'title', 'thumbnail' ), // Removes content field. To remove title, set entire thing to be false rather than an array.
'label' => __( 'Links', 'linkinbio' ),
'description' => __( 'A Link', 'linkinbio' ),
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'publicly_queryable' => true,
'public' => true,
'query_var' => true,
'show_in_rest' => true,
'rest_base' => 'links',
'rest_controller_class' => 'WP_REST_Posts_Controller',
'menu_position' => 5,
'menu_icon' => 'dashicons-admin-links',
'has_archive' => true,
'rewrite' => array(
'slug' => 'links',
'feeds' => false
),
);
register_post_type( 'link-in-bio', $args );
}
public function register_post_meta(){
register_post_meta( 'link-in-bio', '_linkinbio_redirect_link', array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
}
) );
}
public function add_rewrites(){
$slug = trim( get_option('linkinbio_landing_page_custom_slug') );
if( ! empty( $slug ) ){
add_rewrite_rule( sanitize_title_with_dashes( $slug ) . '?', 'index.php?post_type=link-in-bio', 'top' );
if( true === (bool)get_option( 'linkinbio_flush_rewrites' )){
flush_rewrite_rules();
}
}
}
public function update_custom_slug(){
update_option('linkinbio_flush_rewrites', true );
}
/**
* Register meta boxes.
*
* @return void
*/
public function register_meta_boxes() {
add_meta_box( 'link-in-bio_general', __( 'Information', 'linkinbio' ), array( $this, 'general_metabox' ), 'link-in-bio', 'advanced', 'high' );
}
/**
* The general metabox.
*
* @return void
*/
public function general_metabox() {
global $post;
wp_nonce_field( 'linkinbio_metabox_save', 'linkinbio_metabox_nonce' );
echo '<p><label>Redirect Link:</label><br /><input type="text" name="linkinbio_redirect_link" value="' . esc_attr( get_post_meta( $post->ID, "_linkinbio_redirect_link", true ) ) .'"/></p>';
}
/**
* Save metabox data.
*
* Note: fasi data cannot be modified through metaboxes.
*
* @param mixed $post_id The ID of the post.
* @param WP_Post $post The WP Post.
* @return void
*/
public function metabox_save( $post_id, $post ) {
// Check nonce.
if ( ! isset( $_POST['linkinbio_metabox_nonce'] ) || ! wp_verify_nonce( $_POST['linkinbio_metabox_nonce'], 'linkinbio_metabox_save' ) ) {
return $post_id;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}
if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
return;
}
// Security.
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
// Validate and sanitize redirect url.
$redirect_link = wp_http_validate_url( esc_url_raw( trim( $_POST['linkinbio_redirect_link'] ) ) );
$redirect_link = (false !== $redirect_link ) ? $redirect_link : '';
// Perform save.
update_post_meta( $post_id, "_linkinbio_redirect_link", $redirect_link );
}
// Add a filter to 'template_include' hook
function include_template( $template ) {
// If the current url is an archive of any kind
if( 'link-in-bio' === get_post_type() ){
if( is_archive() ) {
// Set this to the template file inside your plugin folder
$template = static::$plugin_base_dir .'/templates/archive-link-in-bio.php';
}
if( is_single() ){
$template = static::$plugin_base_dir .'/templates/single-link-in-bio.php';
}
}
// Always return, even if we didn't change anything
return $template;
}
/**
* Enqueue Scripts and styles for Frontend.
*/
public function frontend_scripts() {
global $wp;
// Only load Style and Scripts where needed.
if( 'link-in-bio' === get_post_type() ){
// Do not enqueue CSS if disable CSS option is selected in customizer.
if( 1 !== (int) get_option( 'linkinbio_landing_page_disable_css' ) ){
wp_register_style( 'linkinbio-css', plugins_url( 'assets/css/link-in-bio.css', static::$plugin_file ) );
wp_enqueue_style( 'linkinbio-css' );
}
wp_enqueue_script( 'linkinbio-infinite-scroll',plugins_url( 'assets/js/infinite-scroll.min.js', static::$plugin_file ), null, null, true );
wp_localize_script( 'linkinbio-infinite-scroll', 'linkinbio_scroll', array( "url" => site_url('/wp-json/wp/v2/links') ) );
}
}
/**
* Method that executes on plugin activation.
*/
public function activate() {
$this->create_post_type();
flush_rewrite_rules();
}
/**
* Method that executes on plugin de-activation.
*/
public function deactivate() {
unregister_post_type('link-in-bio');
flush_rewrite_rules();
}
/**
* Add Tools link on plugin page.
*
* @param [Array] $links : Array of links on plugin page.
* @return [Array] : Array of links on plugin page.
*/
public function plugin_links( $links ) {
$archive_link = '<a href="' . site_url('/links') . '"> View Landing Page</a>';
array_unshift( $links, $archive_link );
$settings_link = '<a href="customize.php?autofocus%5Bpanel%5D=linkinbio">Settings</a>';
array_unshift( $links, $settings_link );
return $links;
}
function posts_per_page( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_post_type_archive( 'link-in-bio' ) ) {
$query->set( 'posts_per_page', 6 );
}
}
/**
* Filter column headers.
*
* @param array $columns The original columns.
* @return array The modified columns.
*/
public function columns_filter( $columns ) {
$columns = array(
'cb' => $columns['cb'],
'image' => __('Image', 'linkinbio'),
'redirect' => __('Redirect Link', 'linkinbio'),
'title' => $columns['title'],
'date' => $columns['date'],
);
return $columns;
}
/**
* Add data to the columns.
*
* @param string $column The identifier for the column.
* @return void
*/
public function columns_data( $column ) {
global $post;
if ( 'image' === $column ) {
echo get_the_post_thumbnail( $post->ID, array(80, 80) );
}
if ( 'redirect' === $column ) {
$ksesargs = array(
'span'=> array('class'=>array()),
'a' => array(
'href' => array(),
'target' => array(),
),
);
$redirect = get_post_meta( $post->ID, "_linkinbio_redirect_link", true );
echo esc_url( $redirect ) . wp_kses('<a href="'.$redirect .'" target="_blank"> <span class="dashicons dashicons-admin-links"></span></a>', $ksesargs );
}
}
}