66 * @Last Modified by: Codehaveli
77 * @Website: www.codehaveli.com
889- * @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) {
185201add_action ('transition_post_status ' , 'wbitly_update_shorturl ' , 10 , 3 );
186202function 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}
0 commit comments