Skip to content

Commit bfb837e

Browse files
committed
custom post support added
1 parent cfcbeef commit bfb837e

File tree

3 files changed

+168
-100
lines changed

3 files changed

+168
-100
lines changed

codehaveli-bitly-url-shortener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Plugin Name: Codehaveli Bitly URL Shortener
55
Plugin URI: https://github.com/codehaveli/
66
Description: This Plugin is used for shorten the newly published post url, Plugin use the api functionality of https://bitly.com/ to achive this URL shorten process.
7-
Version: 1.1.1
7+
Version: 1.1.2
88
Author: Codehaveli
99
Author URI: https://www.codehaveli.com/
1010
License: GPLv2 or later
@@ -16,7 +16,7 @@
1616

1717
define( 'WBITLY_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
1818
define( 'WBITLY_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
19-
define( 'WBITLY_PLUGIN_VERSION', '1.1.1' );
19+
define( 'WBITLY_PLUGIN_VERSION', '1.1.2' );
2020
define( 'WBITLY_API_URL', 'https://api-ssl.bitly.com' );
2121
define( 'WBITLY_BASENAME', plugin_basename( __FILE__ ) );
2222
define( 'WBITLY_SETTINGS_URL', admin_url( 'tools.php?page=wbitly' ) );

inc/wbitly-integration.php

Lines changed: 97 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @Last Modified by: Codehaveli
77
* @Website: www.codehaveli.com
88
9-
* @Last Modified time: 2020-08-27 13:44:51
9+
* @Last Modified time: 2020-09-27 19:25:49
1010
*/
1111

1212

@@ -91,90 +91,106 @@ function wbitly_shorten_url ($permalink) {
9191
}
9292

9393

94-
9594
/**
96-
* Add Short URL Column in Post List
95+
* Add Colum for custom post list
96+
*
9797
*/
9898

99-
add_filter('manage_post_posts_columns', function($columns) {
100-
return array_merge($columns, ['wbitly_url' => __('Short URL', 'wbitly')]);
101-
});
99+
add_action( 'admin_init', function(){
102100

103101

104-
/**
105-
* Display the value of bitly URL
106-
* If Access token not added or Guid not added column will show settings link
107-
* If Post Short URL is not generated "Not Generated yet" message will show
108-
*/
109-
110-
add_action('manage_post_posts_custom_column', function($column_key, $post_id) {
111-
if ($column_key == 'wbitly_url') {
102+
$wbitly_settings = new WbitlyURLSettings();
103+
$active_post_types = $wbitly_settings->get_wbitly_active_post_status();
112104

113-
if ( ! class_exists( 'WbitlyURLSettings' ) ) {
114-
return;
115-
}
116105

106+
foreach ($active_post_types as $active_post) {
107+
108+
/**
109+
* Add Short URL Column in Post List
110+
*/
117111

118-
if( 'publish' != get_post_status($post_id)){
119-
return;
120-
}
112+
$wbitly_column_key = 'manage_'.$active_post.'_posts_columns';
113+
$wbitly_column_value_key = 'manage_'.$active_post.'_posts_custom_column';
121114

115+
add_filter($wbitly_column_key, function($columns) {
116+
return array_merge($columns, ['wbitly_url' => __('Short URL', 'wbitly')]);
117+
});
122118

123119

120+
/**
121+
* Display the value of bitly URL
122+
* If Access token not added or Guid not added column will show settings link
123+
* If Post Short URL is not generated "Not Generated yet" message will show
124+
*/
125+
126+
add_action($wbitly_column_value_key, function($column_key, $post_id) {
127+
if ($column_key == 'wbitly_url') {
124128

125-
$wbitly_settings = new WbitlyURLSettings();
126129

127-
$access_token = $wbitly_settings->get_wbitly_access_token();
128-
$guid = $wbitly_settings->get_wbitly_guid();
130+
if( 'publish' != get_post_status($post_id)){
131+
return;
132+
}
129133

130-
if(!$access_token || !$guid){
134+
$wbitly_settings = new WbitlyURLSettings();
135+
$access_token = $wbitly_settings->get_wbitly_access_token();
136+
$guid = $wbitly_settings->get_wbitly_guid();
131137

132-
$plugin_url = admin_url( 'tools.php?page=wbitly');
133-
echo ' <a class="wbitly_settings" href="'.$plugin_url .'">
134-
Setup Bitly URL
135-
</a>';
136-
}else{
138+
if(!$access_token || !$guid){
139+
140+
$plugin_url = admin_url( 'tools.php?page=wbitly');
141+
echo ' <a class="wbitly_settings" href="'.$plugin_url .'">
142+
Setup Bitly URL
143+
</a>';
144+
}else{
145+
146+
echo '<div class="wbitly_column_container">';
147+
148+
$bitly_url = get_wbitly_short_url($post_id);
149+
if ($bitly_url) {
150+
?>
151+
<div class="wbitly_tooltip wbitly copy_bitly">
152+
<p><span class="copy_bitly_link"><?php echo $bitly_url; ?></span> <span class="wbitly_tooltiptext">Click to Copy</span></p>
153+
</div>
154+
<?php
155+
156+
$wbitly_socal_share_status = $wbitly_settings->get_wbitly_socal_share_status();
137157

138-
echo '<div class="wbitly_column_container">';
158+
if( $wbitly_socal_share_status){
159+
wbitly_get_template('share.php');
160+
}
139161

140-
$bitly_url = get_wbitly_short_url($post_id);
141-
if ($bitly_url) {
142-
?>
143-
<div class="wbitly_tooltip wbitly copy_bitly">
144-
<p><span class="copy_bitly_link"><?php echo $bitly_url; ?></span> <span class="wbitly_tooltiptext">Click to Copy</span></p>
145-
</div>
146-
<?php
162+
?>
163+
<?php
164+
165+
} else {
166+
?>
167+
<div class="wbitly_tooltip">
168+
<p><?php echo $bitly_url; ?></p>
169+
<button class="wbitly generate_bitly" data-post_id="<?php echo $post_id;?>">
170+
<span class="wbitly_tooltiptext">Click to Generate</span>
171+
Generate URL
172+
</button>
173+
</div>
147174

148-
$wbitly_socal_share_status = $wbitly_settings->get_wbitly_socal_share_status();
149175

150-
if( $wbitly_socal_share_status){
151-
wbitly_get_template('share.php');
176+
<?php
177+
}
178+
179+
echo "</div>";
180+
152181
}
153182

154-
?>
155-
<?php
156-
157-
} else {
158-
?>
159-
<div class="wbitly_tooltip">
160-
<p><?php echo $bitly_url; ?></p>
161-
<button class="wbitly generate_bitly" data-post_id="<?php echo $post_id;?>">
162-
<span class="wbitly_tooltiptext">Click to Generate</span>
163-
Generate URL
164-
</button>
165-
</div>
166183

184+
}
185+
}, 10, 2);
186+
187+
}
167188

168-
<?php
169-
}
170189

171-
echo "</div>";
172190

173-
}
191+
});
174192

175193

176-
}
177-
}, 10, 2);
178194

179195

180196
/**
@@ -185,20 +201,30 @@ function wbitly_shorten_url ($permalink) {
185201
add_action('transition_post_status', 'wbitly_update_shorturl' , 10 , 3 );
186202
function wbitly_update_shorturl($new_status, $old_status, $post) {
187203

188-
if('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'post') {
204+
205+
206+
207+
if('publish' === $new_status && 'publish' !== $old_status) {
189208

190-
$post_id = $post->ID;
191-
$shorten_url = get_wbitly_short_url($post_id);
192-
193-
if( empty( $shorten_url ) && ! wp_is_post_revision( $post_id ) ) {
194-
$permalink = get_permalink($post_id);
195-
$shorten_url = wbitly_generate_shorten_url($permalink);
196-
197-
if($shorten_url){
198-
save_wbitly_short_url($shorten_url , $post_id);
199-
}
200-
201-
}
209+
$wbitly_settings = new WbitlyURLSettings();
210+
$active_post_types = $wbitly_settings->get_wbitly_active_post_status();
211+
212+
if(in_array($post->post_type, $active_post_types)){
213+
214+
$post_id = $post->ID;
215+
$shorten_url = get_wbitly_short_url($post_id);
216+
217+
if( empty( $shorten_url ) && ! wp_is_post_revision( $post_id ) ) {
218+
219+
$permalink = get_permalink($post_id);
220+
$shorten_url = wbitly_generate_shorten_url($permalink);
221+
222+
if($shorten_url){
223+
save_wbitly_short_url($shorten_url , $post_id);
224+
}
225+
226+
}
227+
}
202228

203229
}
204230
}

inc/wbitly-settings.php

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @Last Modified by: Codehaveli
77
* @Website: www.codehaveli.com
88
9-
* @Last Modified time: 2020-08-27 13:51:35
9+
* @Last Modified time: 2020-09-27 19:23:07
1010
*/
1111

1212

@@ -19,35 +19,10 @@ public function __construct() {
1919
add_action( 'admin_menu', array( $this, 'wbitly_url_add_plugin_page' ) );
2020
add_action( 'admin_init', array( $this, 'wbitly_url_page_init' ) );
2121
add_action( 'init', array( $this, 'wbitly_redirect_to_get_guid'));
22-
add_action( 'plugin_action_links_'.WBITLY_BASENAME, array( $this, 'wbitly_add_settings_url') );
2322
add_action( 'admin_notices', array($this , 'show_success_when_getting_guid') );
2423
add_action( 'admin_notices', array($this, 'show_error_when_getting_guid') );
2524
}
2625

27-
28-
/**
29-
* Add Settings URL
30-
*
31-
* @param <type> $links The links
32-
*
33-
* @return <type> ( description_of_the_return_value )
34-
*/
35-
36-
function wbitly_add_settings_url( $links ) {
37-
38-
$links = array_merge( array(
39-
'<a href="' . esc_url( admin_url( 'tools.php?page=wbitly' ) ) . '">' . __( 'Settings', 'wbitly' ) . '</a>'
40-
), $links );
41-
42-
return $links;
43-
44-
}
45-
46-
47-
48-
49-
50-
5126
/**
5227
* Update GUID
5328
*/
@@ -290,6 +265,14 @@ public function wbitly_url_page_init() {
290265
'wbitly_url_setting_section' // section
291266
);
292267

268+
add_settings_field(
269+
'wbitly_custom_post', // id
270+
'Post Types', // title
271+
array( $this, 'add_wbitly_custom_posttype_settings' ), // callback
272+
'wbitly-url-admin', // page
273+
'wbitly_url_setting_section' // section
274+
);
275+
293276

294277

295278
}
@@ -321,6 +304,12 @@ public function wbitly_url_sanitize($input) {
321304
$sanitary_values['wbitly_socal_share'] = sanitize_text_field( $input['wbitly_socal_share'] );
322305
}
323306

307+
308+
if ( isset( $input['wbitly_custom_post'] ) ) {
309+
$sanitary_values['wbitly_custom_post'] = $input['wbitly_custom_post'];
310+
}
311+
312+
324313
return $sanitary_values;
325314
}
326315

@@ -368,6 +357,28 @@ public function add_wbitly_social_share_button() {
368357
}
369358

370359

360+
361+
362+
public function add_wbitly_custom_posttype_settings(){
363+
364+
$post_types = get_post_types(array('public' => true));
365+
$current_post_types = [];
366+
367+
$output = '<fieldset><legend class="screen-reader-text"><span>Post Types</span></legend>';
368+
369+
370+
if(isset($this->bitly_url_options['wbitly_custom_post'])){
371+
$current_post_types = $this->bitly_url_options['wbitly_custom_post'];
372+
}
373+
374+
foreach ($post_types as $label) {
375+
$output .= '<label for "' . $label . '>' . '<input type="checkbox" name="wbitly_url_option_name[wbitly_custom_post][]" value="' . $label . '" ' . checked(in_array($label, $current_post_types), true, false) . '>' . $label . '</label><br>';
376+
}
377+
378+
echo $output;
379+
}
380+
381+
371382
/**
372383
* Return currently saved bitly access token
373384
*
@@ -409,20 +420,51 @@ public function get_wbitly_domain(){
409420
return $domain ? trim($domain) : "bit.ly";
410421
}
411422

412-
423+
/**
424+
* Gets the wbitly socal share status.
425+
*
426+
* @return bool The wbitly socal share status.
427+
*/
413428
public function get_wbitly_socal_share_status(){
414429

415430
$bitly_url_options_from_db = get_option( 'wbitly_url_option_name' );
416431
$wbitly_socal_share = isset($bitly_url_options_from_db['wbitly_socal_share']) ? $bitly_url_options_from_db['wbitly_socal_share'] : '';
417432
return $wbitly_socal_share === "enable" ? true : false;
418433
}
419434

435+
436+
public function get_wbitly_active_post_status(){
437+
438+
439+
$bitly_url_options_from_db = get_option( 'wbitly_url_option_name' );
440+
$active_post_types = isset($bitly_url_options_from_db['wbitly_custom_post']) ? $bitly_url_options_from_db['wbitly_custom_post'] : [];
441+
return $active_post_types;
442+
443+
}
444+
445+
446+
447+
448+
420449
}
421450

422451

423452

424453
$wbitly_settings = new WbitlyURLSettings();
425454

455+
add_action( 'plugin_action_links_'.WBITLY_BASENAME, 'wbitly_add_settings_url' );
456+
457+
function wbitly_add_settings_url( $links ) {
458+
459+
$links = array_merge( array(
460+
'<a href="' . esc_url( admin_url( 'tools.php?page=wbitly' ) ) . '">' . __( 'Settings', 'wbitly' ) . '</a>'
461+
), $links );
462+
463+
return $links;
464+
465+
}
466+
467+
426468

427469

428470

0 commit comments

Comments
 (0)