PluginProbe
Link Library / 7.9.4
Link Library v7.9.4
7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 6.0-Beta11 6.0-Beta12 6.0-Beta2 6.0-Beta3 6.0-Beta4 6.0-Beta5 6.0-Beta6 6.0-Beta7 6.0-Beta8 6.0-Beta9 6.6.8 7.1.4 7.1.5 7.1.6 7.1.7 7.1.8 7.1.9 7.2.0 All 135 releases
link-library / link-library.php

link-library.php in Link Library 7.9.4, at link-library.php

2,605 lines 92.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Link Library
4 Plugin URI: https://ylefebvre.github.io/wordpress-plugins/link-library/
5 Description: Display links on pages with a variety of options
6 Version: 7.9.4
7 Author: Yannick Lefebvre
8 Author URI: http://ylefebvre.github.io/
9 Text Domain: link-library
10
11 A plugin for the blogging MySQL/PHP-based WordPress.
12 Copyright 2026 Yannick Lefebvre
13
14 Translations:
15 French Translation courtesy of Michel G. et Luc Capronnier
16 Danish Translation courtesy of GeorgWP (http://wordpress.blogos.dk)
17 Italian Translation courtesy of Gianni Diurno
18 Serbian Translation courtesy of Ogi Djuraskovic (firstsiteguide.com)
19
20 This program is free software; you can redistribute it and/or
21 modify it under the terms of the GNUs General Public License
22 as published by the Free Software Foundation; either version 2
23 of the License, or (at your option) any later version.
24
25 This program is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License for more details.
29
30 You should have received a copy of the GNU General Public License
31 along with this program; if not, write to the Free Software
32 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33
34 You can also view a copy of the HTML version of the GNU General Public
35 License at http://www.gnu.org/copyleft/gpl.html
36
37 I, Yannick Lefebvre, can be contacted via e-mail at ylefebvre@gmail.com
38 */
39
40 // Exit if login page
41 if ( !function_exists( 'is_login' ) || ( function_exists( 'is_login' ) && !is_login() ) ) {
42 update_option( 'link_manager_enabled', 0 );
43
44 require_once(ABSPATH . '/wp-admin/includes/bookmark.php');
45 require_once plugin_dir_path( __FILE__ ) . 'link-library-defaults.php';
46 require_once plugin_dir_path( __FILE__ ) . 'rssfeed.php';
47 require_once plugin_dir_path( __FILE__ ) . '/upvote-downvote/thumbs-rating.php';
48
49 global $my_link_library_plugin;
50 global $my_link_library_plugin_admin;
51
52 function link_library_tweak_plugins_http_filter( $response, $r, $url ) {
53 if ( stristr( $url, 'api.wordpress.org/plugins/update-check/1.1' ) ) {
54 $wpapi_response = json_decode( $response['body'] );
55 $wpapi_response->plugins = link_library_modify_http_response( $wpapi_response->plugins );
56 $response['body'] = json_encode( $wpapi_response );
57 }
58
59 return $response;
60 }
61
62 function link_library_get_terms_filter_only_publish( $terms, $taxonomies, $args ) {
63 global $wpdb;
64 global $hide_if_empty_filter;
65
66 $taxonomy = $taxonomies[0];
67 if ( ! is_array( $terms ) && count( $terms ) < 1 ) {
68 return $terms;
69 }
70
71 $filtered_terms = array();
72
73 foreach ( $terms as $term ) {
74 $result = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts p JOIN $wpdb->term_relationships rl ON p.ID = rl.object_id WHERE rl.term_taxonomy_id = $term->term_id AND p.post_status = 'publish' LIMIT 1" );
75
76 if ( intval( $result ) > 0 || ( !$hide_if_empty_filter && 0 == intval( $result ) ) ) {
77 $filtered_terms[] = $term;
78 }
79 }
80 return $filtered_terms;
81 }
82
83 function link_library_get_terms_filter_publish_pending( $terms, $taxonomies, $args ) {
84 global $wpdb;
85 global $hide_if_empty_filter;
86
87 $taxonomy = $taxonomies[0];
88 if ( ! is_array( $terms ) && count( $terms ) < 1 ) {
89 return $terms;
90 }
91
92 $filtered_terms = array();
93
94 foreach ( $terms as $term ) {
95 $result = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts p JOIN $wpdb->term_relationships rl ON p.ID = rl.object_id WHERE rl.term_taxonomy_id = $term->term_id AND ( p.post_status = 'publish' or p.post_status = 'pending' ) LIMIT 1" );
96 if ( intval( $result ) > 0 || ( !$hide_if_empty_filter && 0 == intval( $result ) ) ) {
97 $filtered_terms[] = $term;
98 }
99 }
100 return $filtered_terms;
101 }
102
103 function link_library_get_terms_filter_publish_draft( $terms, $taxonomies, $args ) {
104 global $wpdb;
105 global $hide_if_empty_filter;
106
107 $taxonomy = $taxonomies[0];
108 if ( ! is_array( $terms ) && count( $terms ) < 1 ) {
109 return $terms;
110 }
111
112 $filtered_terms = array();
113
114 foreach ( $terms as $term ) {
115 $result = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts p JOIN $wpdb->term_relationships rl ON p.ID = rl.object_id WHERE rl.term_taxonomy_id = $term->term_id AND ( p.post_status = 'publish' or p.post_status = 'draft' ) LIMIT 1" );
116 if ( intval( $result ) > 0 || ( !$hide_if_empty_filter && 0 == intval( $result ) ) ) {
117 $filtered_terms[] = $term;
118 }
119 }
120 return $filtered_terms;
121 }
122
123 function link_library_get_terms_filter_publish_draft_pending( $terms, $taxonomies, $args ) {
124 global $wpdb;
125 global $hide_if_empty_filter;
126
127 $taxonomy = $taxonomies[0];
128 if ( ! is_array( $terms ) && count( $terms ) < 1 ) {
129 return $terms;
130 }
131
132 $filtered_terms = array();
133
134 foreach ( $terms as $term ) {
135 $result = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts p JOIN $wpdb->term_relationships rl ON p.ID = rl.object_id WHERE rl.term_taxonomy_id = $term->term_id AND ( p.post_status = 'publish' or p.post_status = 'draft' or p.post_status = 'pending' ) LIMIT 1" );
136 if ( intval( $result ) > 0 || ( !$hide_if_empty_filter && 0 == intval( $result ) ) ) {
137 $filtered_terms[] = $term;
138 }
139 }
140 return $filtered_terms;
141 }
142
143 function link_library_strposX( $haystack, $needle, $number ) {
144 if( $number == '1' ){
145 return strpos($haystack, $needle);
146 } elseif( $number > '1' ){
147 return strpos( $haystack, $needle, link_library_strposX( $haystack, $needle, $number - 1 ) + strlen( $needle ) );
148 } else {
149 return error_log( 'Error: Value for parameter $number is out of range' );
150 }
151 }
152
153 function link_library_modify_http_response( $plugins_response ) {
154
155 foreach ( $plugins_response as $response_key => $plugin_response ) {
156 if ( plugin_basename(__FILE__) == $plugin_response->plugin ) {
157 if ( 3 <= substr_count( $plugin_response->new_version, '.' ) ) {
158 $plugin_info = get_plugin_data( __FILE__ );
159 $period_position = link_library_strposX( $plugin_info['Version'], '.', 3 );
160 if ( false !== $period_position ) {
161 $current_version = substr( $plugin_info['Version'], 0, $period_position );
162 } else {
163 $current_version = $plugin_info['Version'];
164 }
165
166 $period_position2 = link_library_strposX( $plugin_response->new_version, '.', 3 );
167 if ( false !== $period_position ) {
168 $new_version = substr( $plugin_response->new_version, 0, $period_position2 );
169 } else {
170 $new_version = $plugin_response->new_version;
171 }
172
173 $version_diff = version_compare( $current_version, $new_version );
174
175 if ( -1 < $version_diff ) {
176 unset( $plugins_response->$response_key );
177 }
178 }
179 }
180 }
181
182 return $plugins_response;
183 }
184
185 function ll_expand_posts_search( $search, $query ) {
186 global $wpdb;
187
188 if ( $query->query_vars['post_type'] == 'link_library_links' && !empty( $query->query['s'] ) ) {
189
190 $query_words = explode( ' ', $query->query['s'] );
191 if ( ! empty( $query_words ) ) {
192 $number_of_words = sizeof( $query_words );
193 if ( $number_of_words > 5 ) {
194 $number_of_words = 5;
195 }
196
197 $sql = "
198 or exists (
199 select * from {$wpdb->postmeta} where post_id={$wpdb->posts}.ID
200 and meta_key in ( 'link_description', 'link_notes', 'link_textfield', 'link_url' )
201 ";
202
203 for ( $counter = 0; $counter < $number_of_words; $counter ++ ) {
204 $sql .= 'and meta_value like %s ';
205 }
206
207 $sql .= "
208 )
209 ";
210
211 $like = array();
212
213 foreach ( $query_words as $index => $query_word ) {
214 $like[ $index ] = '%' . $wpdb->esc_like( $query_words[ $index ] ) . '%';
215 }
216
217 if ( 1 == $number_of_words ) {
218 $search = preg_replace( "#\({$wpdb->posts}.post_title LIKE [^)]+\)\K#", $wpdb->prepare( $sql, $like[0] ), $search );
219 } elseif ( 2 == $number_of_words ) {
220 $search = preg_replace( "#\({$wpdb->posts}.post_title LIKE [^)]+\)\K#", $wpdb->prepare( $sql, $like[0], $like[1] ), $search );
221 } elseif ( 3 == $number_of_words ) {
222 $search = preg_replace( "#\({$wpdb->posts}.post_title LIKE [^)]+\)\K#", $wpdb->prepare( $sql, $like[0], $like[1], $like[2] ), $search );
223 } elseif ( 4 == $number_of_words ) {
224 $search = preg_replace( "#\({$wpdb->posts}.post_title LIKE [^)]+\)\K#", $wpdb->prepare( $sql, $like[0], $like[1], $like[2], $like[3] ), $search );
225 } elseif ( 5 == $number_of_words ) {
226 $search = preg_replace( "#\({$wpdb->posts}.post_title LIKE [^)]+\)\K#", $wpdb->prepare( $sql, $like[0], $like[1], $like[2], $like[3], $like[4] ), $search );
227 }
228
229 }
230 }
231
232 return $search;
233 }
234
235 /*********************************** Link Library Class *****************************************************************************/
236 class link_library_plugin {
237
238 //constructor of class, PHP4 compatible construction for backward compatibility
239 function __construct() {
240
241 // Functions to be called when plugin is activated and deactivated
242 register_activation_hook( __FILE__, array( $this, 'll_install' ) );
243 register_deactivation_hook( __FILE__, array( $this, 'll_uninstall' ) );
244
245 add_action( 'init', array( $this, 'll_init' ) );
246 add_action( 'wp_loaded', array( $this, 'll_update_60' ) );
247
248 $newoptions = get_option( 'LinkLibraryPP1', '' );
249
250 if ( empty( $newoptions ) ) {
251 global $my_link_library_plugin_admin;
252
253 if ( empty( $my_link_library_plugin_admin ) ) {
254 require plugin_dir_path( __FILE__ ) . 'link-library-admin.php';
255 $my_link_library_plugin_admin = new link_library_plugin_admin();
256 }
257
258 ll_reset_options( 1, 'list', 'return_and_set' );
259 ll_reset_gen_settings( 'return_and_set' );
260 }
261
262 $genoptions = get_option( 'LinkLibraryGeneral' );
263 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
264
265 // Add short codes
266 add_shortcode( 'link-library', array( $this, 'link_library_func' ) );
267 add_shortcode( 'link-library-cats', array( $this, 'link_library_cats_func' ) );
268 add_shortcode( 'cats-link-library', array( $this, 'link_library_cats_func' ) );
269 add_shortcode( 'link-library-search', array( $this, 'link_library_search_func' ) );
270 add_shortcode( 'search-link-library', array( $this, 'link_library_search_func' ) );
271 add_shortcode( 'link-library-addlink', array( $this, 'link_library_addlink_func' ) );
272 add_shortcode( 'addlink-link-library', array( $this, 'link_library_addlink_func' ) );
273 add_shortcode( 'link-library-addlinkcustommsg', array( $this, 'link_library_addlink_func' ) );
274 add_shortcode( 'addlinkcustommsg-link-library', array( $this, 'link_library_addlink_func' ) );
275 add_shortcode( 'link-library-count', array( $this, 'link_library_count_func' ) );
276 add_shortcode( 'link-library-filters', array( $this, 'link_library_filters' ) );
277 add_shortcode( 'link-library-tagcloud', array( $this, 'link_library_tagcloud' ) );
278 add_shortcode( 'rss-library', array( $this, 'rss_library_func' ) );
279
280 // Function to determine if Link Library is used on a page before printing headers
281 // the_posts gets triggered before wp_head
282 add_filter( 'the_posts', array( $this, 'conditionally_add_scripts_and_styles' ) );
283
284 // Function to print information in page header when plugin present
285 add_action( 'wp_head', array( $this, 'll_rss_link' ) );
286
287 add_filter( 'wp_title', array( $this, 'll_title_creator' ) );
288
289 add_filter( 'get_the_excerpt', array( $this, 'll_display_single_link' ) );
290 add_filter( 'the_excerpt', array( $this, 'll_display_single_link' ) );
291 add_filter( 'post_type_link', array( $this, 'll_get_permalink' ), 10, 4 );
292 add_filter( 'the_title', array( $this, 'll_get_title' ), 10, 2 );
293
294 add_filter( 'block_categories_all', array( $this, 'll_block_categories' ), 10, 2 );
295 add_action( 'rest_api_init', array( $this, 'll_rest_api_init' ) );
296
297
298 // Re-write rules filters to allow for custom permalinks
299 add_filter( 'rewrite_rules_array', array( $this, 'll_insertMyRewriteRules' ) );
300 add_filter( 'query_vars', array( $this, 'll_insertMyRewriteQueryVars' ) );
301
302 add_action( 'template_redirect', array( $this, 'll_template_redirect' ) );
303 add_filter( 'template_include', array( $this, 'll_template_include' ) );
304 add_action( 'wp_ajax_link_library_tracker', array( $this, 'link_library_ajax_tracker' ) );
305 add_action( 'wp_ajax_nopriv_link_library_tracker', array( $this, 'link_library_ajax_tracker' ) );
306 add_action( 'wp_ajax_link_library_ajax_update', array( $this, 'link_library_func') );
307 add_action( 'wp_ajax_nopriv_link_library_ajax_update', array( $this, 'link_library_func') );
308 add_action( 'wp_ajax_link_library_generate_image', array( $this, 'link_library_generate_image') );
309 add_action( 'wp_ajax_nopriv_link_library_generate_image', array( $this, 'link_library_generate_image') );
310 add_action( 'wp_ajax_link_library_popup_content', array( $this, 'll_popup_content') );
311 add_action( 'wp_ajax_nopriv_link_library_popup_content', array( $this, 'll_popup_content') );
312
313 add_action( 'wp_enqueue_scripts', array( $this, 'll_register_script' ) );
314
315 add_filter( 'posts_where', array( $this, 'll_posts_where' ), 10, 2 );
316
317 // Load text domain for translation of admin pages and text strings
318 load_plugin_textdomain( 'link-library', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
319
320 add_filter( 'kses_allowed_protocols', array( $this, 'll_add_protocols' ) );
321
322 add_filter( 'wp_feed_cache_transient_lifetime' , array( $this, 'feed_cache_filter_handler' ) );
323
324 add_filter( 'post_type_link', array( $this, 'permalink_structure' ), 10, 4 );
325
326 add_action('auth_redirect', array( $this, 'add_pending_count_filter') ); // modify esc_attr on auth_redirect
327 add_action('admin_menu', array( $this, 'esc_attr_restore' ) ); // restore on admin_menu (very soon)
328
329 if ( $genoptions['add_to_main_rss'] ) {
330 add_action( 'request', array( $this, 'link_library_rss_feed_request' ) );
331 add_filter( 'the_content_feed', array( $this, 'll_display_single_link' ) );
332 }
333
334 add_action( 'link_library_import_links', array( $this, 'll_import_links' ), 10, 0 );
335 add_action( 'link_library_gen_thumbs', array( $this, 'll_gen_thumbs' ), 10, 0 );
336 }
337
338 function ll_import_links() {
339 require_once plugin_dir_path( __FILE__ ) . 'link-library-link-importer.php';
340
341 $genoptions = get_option( 'LinkLibraryGeneral' );
342 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
343
344 $row = 0;
345 $successfulimport = 0;
346 $successfulupdate = 0;
347
348 link_library_import_links( $genoptions, $row, $successfulimport, $successfulupdate );
349 }
350
351 function ll_gen_thumbs() {
352 require_once plugin_dir_path( __FILE__ ) . 'link-library-image-generator.php';
353
354 $options = array();
355 $options['categorylist_cpt'] = '';
356 $options['uselocalimagesoverthumbshots'] = true;
357
358 link_library_image_generator( $this, $options, true );
359 }
360
361 function ll_rest_api_init() {
362 register_rest_route( 'link-library/v1', '/settingslist', array(
363 'methods' => 'GET',
364 'callback' => array( $this, 'll_rest_settings_list' ),
365 'permission_callback' => '__return_true'
366 ) );
367 }
368
369 function ll_rest_settings_list( WP_REST_Request $request ) {
370 $genoptions = get_option( 'LinkLibraryGeneral' );
371 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
372
373 if ( empty( $genoptions['numberstylesets'] ) ) {
374 $numberofsets = 1;
375 } else {
376 $numberofsets = $genoptions['numberstylesets'];
377 }
378 $settings_array = array();
379 for ( $counter = 1; $counter <= $numberofsets; $counter ++ ) {
380 $tempoptionname = "LinkLibraryPP" . $counter;
381 $tempoptions = get_option( $tempoptionname );
382
383 if ( ! empty( $tempoptions ) && isset( $tempoptions['settingssetname'] ) ) {
384 $settings_array[$counter] = $tempoptions['settingssetname'];
385 }
386 }
387
388 $response = new WP_REST_Response( $settings_array );
389
390 return $response;
391 }
392
393 function ll_block_categories( $categories, $post ) {
394 return array_merge(
395 $categories,
396 array(
397 array(
398 'slug' => 'link-library',
399 'title' => __( 'Link Library', 'link-library' ),
400 'icon' => 'wordpress',
401 ),
402 )
403 );
404 }
405
406 function ll_posts_where( $where, $query ) {
407 global $wpdb;
408
409 $starts_with = $query->get( 'link_starts_with' );
410
411 if ( $starts_with ) {
412 $where .= " AND $wpdb->posts.post_title LIKE '$starts_with%'";
413 }
414
415 return $where;
416 }
417
418 function link_library_rss_feed_request( $qv ) {
419
420 if ( isset( $qv['feed'] ) && !isset( $qv['post_type'] ) ) {
421 $qv['post_type'] = array( 'post', 'link_library_links' );
422 } elseif ( isset( $qv['feed'] ) && isset( $qv['post_type'] ) ) {
423 $qv['post_type'][] = 'link_library_links';
424 }
425
426 return $qv;
427 }
428
429
430 function add_pending_count_filter() {
431 add_filter('attribute_escape', array( $this, 'remove_esc_attr_and_count' ), 20, 2);
432 }
433
434 function esc_attr_restore() {
435 remove_filter('attribute_escape', array( $this, 'remove_esc_attr_and_count' ), 20, 2);
436 }
437
438 function remove_esc_attr_and_count( $safe_text = '', $text = '' ) {
439 if ( substr_count($text, '%%PENDING_COUNT%%') ) {
440 $text = trim( str_replace('%%PENDING_COUNT%%', '', $text) );
441 // run only once!
442 remove_filter('attribute_escape', 'remove_esc_attr_and_count', 20, 2);
443 $safe_text = esc_attr($text);
444 // remember to set the right cpt name below
445 $linkmoderatecount = 0;
446
447 $args = array(
448 'numberposts' => -1,
449 'post_type' => 'link_library_links',
450 'post_status' => array( 'pending' )
451 );
452 $linkmoderatecount = count( get_posts( $args ) );
453 if ( $linkmoderatecount > 0 ) {
454 // we have pending, add the count
455 $text = esc_attr($text) . '<span class="awaiting-mod count-' . $linkmoderatecount . '"><span class="pending-count">' . $linkmoderatecount . '</span></span>';
456 return $text;
457 }
458 }
459 return $safe_text;
460 }
461
462 function feed_cache_filter_handler( $seconds ) {
463 $genoptions = get_option( 'LinkLibraryGeneral' );
464 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
465
466 return $genoptions['rsscachedelay'];
467 }
468
469 function ll_init() {
470 $genoptions = get_option( 'LinkLibraryGeneral' );
471 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
472
473 $post_type_args = array(
474 'labels' => array(
475 'name' => __( 'Link Library', 'link-library' ),
476 'singular_name' => __( 'Link', 'link-library' ),
477 'add_new' => __( 'Add New', 'link-library' ),
478 'add_new_item' => __( 'Add New Link', 'link-library' ),
479 'edit' => __( 'Edit', 'link-library' ),
480 'edit_item' => __( 'Edit Link', 'link-library' ),
481 'new_item' => __( 'New Link', 'link-library' ),
482 'view' => __( 'View', 'link-library' ),
483 'view_item' => __( 'View Link', 'link-library' ),
484 'search_items' => __( 'Search Links', 'link-library' ),
485 'not_found' => __( 'No Links found', 'link-library' ),
486 'not_found_in_trash' => __( 'No Links found in Trash', 'link-library' ),
487 'parent' => __( 'Parent Link', 'link-library' ),
488 'all_items' => __( 'All Links', 'link-library' ),
489 'menu_name' => _x('Link Library %%PENDING_COUNT%%', 'Link Library', 'link-library'),
490 ),
491 'show_in_nav_menu' => true,
492 'show_ui' => true,
493 'exclude_from_search' => !$genoptions['exclude_from_search'],
494 'publicly_queryable' => $genoptions['publicly_queryable'],
495 'menu_position' => 10,
496 'supports' =>
497 array( 'title', 'editor', 'comments' ),
498 'taxonomies' => array( $genoptions['cattaxonomy'], $genoptions['tagtaxonomy'] ),
499 'menu_icon' =>
500 'dashicons-admin-links',
501 'has_archive' => false,
502 'rewrite' => array( 'slug' => $genoptions['cptslug'] . '/%' . $genoptions['cattaxonomy'] . '%' )
503 );
504
505 if ( class_exists( 'WPGraphQL' ) ) {
506 $post_type_args['show_in_graphql'] = true;
507 $post_type_args['hierarchical'] = true;
508 $post_type_args['graphql_single_name'] = 'linklibrary';
509 $post_type_args['graphql_plural_name'] = 'linklibrary';
510 }
511
512 if ( $genoptions['exclude_from_search'] && $genoptions['publicly_queryable'] ) {
513 unset( $post_type_args['exclude_from_search'] );
514 unset( $post_type_args['publicly_queryable'] );
515 $post_type_args['public'] = true;
516 }
517
518 if ( $genoptions['showexcerpt'] ) {
519 $post_type_args['supports'][] = 'excerpt';
520 }
521
522 register_post_type( 'link_library_links', $post_type_args );
523
524 register_taxonomy(
525 'link_library_category',
526 'link_library_links',
527 array(
528 'labels' => array(
529 'name' => __( 'Link Library Categories', 'link-library' ),
530 'add_new_item' => __( 'Add New Link Library Category', 'link-library' ),
531 'new_item_name' => __( 'New Link Library Category', 'link-library' )
532 ),
533 'show_ui' => ( $genoptions['cattaxonomy'] == 'link_library_category' ? true : false ),
534 'show_tagcloud' => false,
535 'hierarchical' => true,
536 'rewrite' => $genoptions['publicly_queryable'],
537 'show_in_rest' => true
538 )
539 );
540
541 register_taxonomy(
542 'link_library_tags',
543 'link_library_links',
544 array(
545 'hierarchical' => false,
546 'labels' => array( 'name' => __( 'Tags', 'link-library' ),
547 'add_new_item' => __( 'Add New Link Library Tag', 'link-library' ),
548 'new_item_name' => __( 'New Link Library Tag', 'link-library' ) ),
549 'show_ui' => ( $genoptions['tagtaxonomy'] == 'link_library_tags' ? true : false ),
550 'rewrite' => false,
551 'show_in_rest' => true
552 )
553 );
554
555 add_feed( 'linklibraryfeed', 'link_library_generate_rss_feed' );
556
557 if ( function_exists( 'register_block_type' ) ) {
558 $asset_file = include( plugin_dir_path( __FILE__ ) . 'build/index.asset.php');
559
560 wp_register_script(
561 'link-library-block',
562 plugins_url( 'build/index.js', __FILE__ ),
563 $asset_file['dependencies'],
564 $asset_file['version']
565 );
566
567 register_block_type( 'link-library/link-block', array(
568 'editor_script' => 'link-library-block',
569 'render_callback' => array( $this, 'link_library_func' ),
570 'attributes' => array(
571 'settings' => array(
572 'type' => 'string',
573 'default' => '1',
574 ),
575 'linkorderoverride' => array(
576 'type' => 'string',
577 'default' => '',
578 ),
579 'linkdirectionoverride' => array(
580 'type' => 'string',
581 'default' => '',
582 ),
583 'categorylistoverride' => array(
584 'type' => 'array',
585 'default' => array(),
586 'items' => array(
587 'type' => 'string',
588 ),
589 ),
590 'excludecategoryoverride' => array(
591 'type' => 'array',
592 'default' => array(),
593 'items' => array(
594 'type' => 'string',
595 ),
596 ),
597 'taglistoverride' => array(
598 'type' => 'array',
599 'default' => array(),
600 'items' => array(
601 'type' => 'string',
602 ),
603 ),
604 'maxlinksoverride' => array(
605 'type' => 'string',
606 'default' => '',
607 ),
608 'notesoverride' => array(
609 'type' => 'boolean',
610 'default' => false,
611 ),
612 'descoverride' => array(
613 'type' => 'boolean',
614 'default' => false,
615 ),
616 'rssoverride' => array(
617 'type' => 'boolean',
618 'default' => false,
619 ),
620 'categorylistoverrideCSV' => array(
621 'type' => 'string',
622 'default' => '',
623 ),
624 'excludecategoryoverrideCSV' => array(
625 'type' => 'string',
626 'default' => '',
627 ),
628 'taglistoverrideCSV' => array(
629 'type' => 'string',
630 'default' => '',
631 ),
632 ),
633 ) );
634
635 register_block_type( 'link-library/cats-block', array(
636 'editor_script' => 'link-library-block',
637 'render_callback' => array( $this, 'link_library_cats_func' ),
638 'attributes' => array(
639 'settings' => array(
640 'type' => 'string',
641 'default' => '1',
642 ),
643 'categorylistoverride' => array(
644 'type' => 'array',
645 'default' => array(),
646 'items' => array(
647 'type' => 'string',
648 ),
649 ),
650 'excludecategoryoverride' => array(
651 'type' => 'array',
652 'default' => array(),
653 'items' => array(
654 'type' => 'string',
655 ),
656 ),
657 'taglistoverride' => array(
658 'type' => 'array',
659 'default' => array(),
660 'items' => array(
661 'type' => 'string',
662 ),
663 ),
664 'targetlibrary' => array(
665 'type' => 'string',
666 'default' => '',
667 ),
668 'categorylistoverrideCSV' => array(
669 'type' => 'string',
670 'default' => '',
671 ),
672 'excludecategoryoverrideCSV' => array(
673 'type' => 'string',
674 'default' => '',
675 ),
676 'taglistoverrideCSV' => array(
677 'type' => 'string',
678 'default' => '',
679 ),
680 ),
681 ) );
682
683 register_block_type( 'link-library/addlink-block', array(
684 'editor_script' => 'link-library-block',
685 'render_callback' => array( $this, 'link_library_addlink_func' ),
686 'attributes' => array(
687 'settings' => array(
688 'type' => 'string',
689 'default' => '1',
690 ),
691 ),
692 ) );
693
694 register_block_type( 'link-library/search-block', array(
695 'editor_script' => 'link-library-block',
696 'render_callback' => array( $this, 'link_library_search_func' ),
697 'attributes' => array(
698 'settings' => array(
699 'type' => 'string',
700 'default' => '1',
701 ),
702 ),
703 ) );
704
705 register_block_type( 'link-library/count-block', array(
706 'editor_script' => 'link-library-block',
707 'render_callback' => array( $this, 'link_library_count_func' ),
708 'attributes' => array(
709 'settings' => array(
710 'type' => 'string',
711 'default' => '1',
712 ),
713 ),
714 ) );
715 }
716 }
717
718 function link_library_block_callback( $attributes ) {
719 $settings = 1;
720
721 if ( isset( $attributes['settingsid']) && !empty( $attributes['settingsid'] ) ) {
722 $settings = $attributes['settingsid'];
723 }
724
725 $genoptions = get_option( 'LinkLibraryGeneral' );
726 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
727
728 $linkcount = 1;
729
730 $settingsname = 'LinkLibraryPP' . $settings;
731 $options = get_option( $settingsname );
732 $options = wp_parse_args( $options, ll_reset_options( 1, 'list', 'return' ) );
733
734 if ( isset( $attributes['categoryOverrideArray'] ) && !empty( $attributes['categoryOverrideArray'] ) ) {
735 $options['categorylist_cpt'] = implode( ',', $attributes['categoryOverrideArray'] );
736 }
737
738 require_once plugin_dir_path( __FILE__ ) . 'render-link-library-sc.php';
739
740 return RenderLinkLibrary( $linkcount, $this, $genoptions, $options, $settings, false, 0, 0, true, false );
741 }
742
743 function link_library_cats_block_callback( $attributes ) {
744 $settings = 1;
745
746 if ( isset( $attributes['settingsid']) && !empty( $attributes['settingsid'] ) ) {
747 $settings = $attributes['settingsid'];
748 }
749
750 $genoptions = get_option( 'LinkLibraryGeneral' );
751 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
752
753 $linkcount = 1;
754
755 $settingsname = 'LinkLibraryPP' . $settings;
756 $options = get_option( $settingsname );
757 $options = wp_parse_args( $options, ll_reset_options( 1, 'list', 'return' ) );
758
759 if ( isset( $attributes['categoryOverrideArray'] ) && !empty( $attributes['categoryOverrideArray'] ) ) {
760 $options['categorylist_cpt'] = implode( ',', $attributes['categoryOverrideArray'] );
761 }
762 $targetlibrary = $settings;
763
764 require_once plugin_dir_path( __FILE__ ) . 'render-link-library-cats-sc.php';
765
766 return RenderLinkLibraryCategories( $this, $genoptions, $options, $settings, $targetlibrary );
767 }
768
769 function ll_update_60() {
770
771 $link_library_60_update = get_option( 'LinkLibrary60Update' );
772 $genoptions = get_option( 'LinkLibraryGeneral' );
773
774 if ( isset( $_GET['ll60reupdate'] ) ) {
775 if ( !current_user_can( 'manage_options' ) ) {
776 return;
777 }
778
779 check_admin_referer( 'll60reupdate' );
780
781 global $wpdb;
782
783 $wpdb->get_results ( 'DELETE a,b,c
784 FROM wp_posts a
785 LEFT JOIN wp_term_relationships b
786 ON (a.ID = b.object_id)
787 LEFT JOIN wp_postmeta c
788 ON (a.ID = c.post_id)
789 WHERE a.post_type = \'link_library_links\';' );
790
791 $link_category_terms = get_terms( 'link_library_category', array( 'fields' => 'ids', 'hide_empty' => false ) );
792 foreach ( $link_category_terms as $value ) {
793 wp_delete_term( $value, 'link_library_category' );
794 }
795
796 require plugin_dir_path( __FILE__ ) . 'link-library-update-60.php';
797 link_library_60_update( $this );
798 } elseif ( isset( $_GET['continue60update'] ) ) {
799 if ( !current_user_can( 'manage_options' ) ) {
800 return;
801 }
802
803 require plugin_dir_path( __FILE__ ) . 'link-library-update-60.php';
804 link_library_60_update( $this, true );
805 } else {
806 if ( ( false == $link_library_60_update && !empty( $genoptions ) ) ) {
807 if ( !current_user_can( 'manage_options' ) ) {
808 return;
809 }
810
811 require plugin_dir_path( __FILE__ ) . 'link-library-update-60.php';
812 link_library_60_update( $this );
813 }
814 }
815 }
816
817 function permalink_structure( $post_link, $post, $leavename, $sample ) {
818
819 $genoptions = get_option( 'LinkLibraryGeneral' );
820 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
821
822 if ( $post->post_type == 'link_library_links' ) {
823 if ( !$genoptions['publicly_queryable'] ) {
824 $link_url = get_post_meta( $post->ID, 'link_url', true );
825
826 if ( !empty( $link_url ) ) {
827 $post_link = $link_url;
828 }
829 } else {
830 if ( !empty( $post_link ) && false !== strpos( $post_link, '%' . $genoptions['cattaxonomy'] . '%' ) ) {
831 $link_cat_type_term = get_the_terms( $post->ID, $genoptions['cattaxonomy'] );
832 if ( !empty( $link_cat_type_term ) ) {
833 $post_link = str_replace( '%' . $genoptions['cattaxonomy'] . '%', array_pop( $link_cat_type_term )->slug, $post_link );
834 }
835 }
836 }
837 }
838
839 return $post_link;
840 }
841
842 /************************** Link Library Installation Function **************************/
843 function ll_install() {
844 global $wpdb;
845
846 if ( function_exists( 'is_multisite' ) && is_multisite() ) {
847 if ( isset( $_GET['networkwide'] ) && ( $_GET['networkwide'] == 1 ) ) {
848 $originalblog = $wpdb->blogid;
849
850 $bloglist = $wpdb->get_col( 'SELECT blog_id FROM ' . $wpdb->blogs );
851 foreach ( $bloglist as $blog ) {
852 switch_to_blog( $blog );
853 $this->create_table_and_settings();
854 }
855 switch_to_blog( $originalblog );
856 return;
857 }
858 }
859 $this->create_table_and_settings();
860 }
861
862 function new_network_site( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
863 global $wpdb;
864
865 if ( ! function_exists( 'is_plugin_active_for_network' ) )
866 require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
867
868 if ( is_plugin_active_for_network( 'link-library/link-library.php' ) ) {
869 $originalblog = $wpdb->blogid;
870 switch_to_blog( $blog_id );
871 $this->create_table_and_settings();
872 switch_to_blog( $originalblog );
873 }
874 }
875
876 function create_table_and_settings() {
877 global $wpdb;
878
879 $genoptions = get_option( 'LinkLibraryGeneral' );
880
881 if ( !empty( $genoptions ) ) {
882 if ( empty( $genoptions['schemaversion'] ) || floatval( $genoptions['schemaversion'] ) < 3.5 ) {
883 $genoptions['schemaversion'] = '3.5';
884 update_option( 'LinkLibraryGeneral', $genoptions );
885 } elseif ( floatval( $genoptions['schemaversion'] ) < '4.6' ) {
886 $genoptions['schemaversion'] = '4.6';
887 update_option( 'LinkLibraryGeneral', $genoptions );
888 } elseif ( floatval( $genoptions['schemaversion'] ) < '4.7' ) {
889 $genoptions['schemaversion'] = '4.7';
890 update_option( 'LinkLibraryGeneral', $genoptions );
891 } elseif ( floatval( $genoptions['schemaversion'] ) < '4.9' ) {
892 $genoptions['schemaversion'] = '4.9';
893 update_option( 'LinkLibraryGeneral', $genoptions );
894 }
895
896 for ( $i = 1; $i <= $genoptions['numberstylesets']; $i++ ) {
897 $settingsname = 'LinkLibraryPP' . $i;
898 $options = get_option( $settingsname );
899 $options = wp_parse_args( $options, ll_reset_options( 1, 'list', 'return' ) );
900
901 if ( !empty( $options ) ) {
902 if ( empty( $options['showname'] ) ) {
903 $options['showname'] = true;
904 }
905
906 if ( isset( $options['show_image_and_name'] ) && $options['show_image_and_name'] == true ) {
907 $options['showname'] = true;
908 $options['show_images'] = true;
909 }
910
911 if ( empty( $options['sourcename'] ) ) {
912 $options['sourcename'] = 'primary';
913 }
914
915 if ( empty( $options['sourceimage'] ) ) {
916 $options['sourceimage'] = 'primary';
917 }
918
919 if ( empty( $options['dragndroporder'] ) ) {
920 if ( $options['imagepos'] == 'beforename' ) {
921 $options['dragndroporder'] = '1,2,3,4,5,6,7,8,9,10,11,12';
922 } elseif ( $options['imagepos'] == 'aftername' ) {
923 $options['dragndroporder'] = '2,1,3,4,5,6,7,8,9,10,11,12';
924 } elseif ( $options['imagepos'] == 'afterrssicons' ) {
925 $options['dragndroporder'] = '2,3,4,5,6,1,7,8,9,10,11,12';
926 }
927 } else if ( !empty( $options['dragndroporder'] ) ) {
928 $elementarray = explode( ',', $options['dragndroporder'] );
929
930 $allelements = array( '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12' );
931 foreach ( $allelements as $element ) {
932 if ( !in_array( $element, $elementarray ) ) {
933 $elementarray[] = $element;
934 $options['dragndroporder'] = implode( ',', $elementarray );
935 }
936 }
937 }
938
939 if ( $options['flatlist'] === true ) {
940 $options['flatlist'] = 'unordered';
941 } elseif ( $options['flatlist'] === false ) {
942 $options['flatlist'] = 'table';
943 }
944 }
945
946 update_option( $settingsname, $options );
947 }
948 } else {
949 update_option( 'LinkLibrary60Update', true );
950 }
951
952 $genoptions['schemaversion'] = '5.0';
953 update_option( 'LinkLibraryGeneral', $genoptions );
954 }
955
956 function remove_querystring_var( $url, $key ) {
957
958 $keypos = strpos( $url, $key );
959 if ( $keypos ) {
960 $ampersandpos = strpos( $url, '&', $keypos );
961 $newurl = substr( $url, 0, $keypos - 1 );
962
963 if ( $ampersandpos ) {
964 $newurl .= substr($url, $ampersandpos);
965 }
966 } else {
967 $newurl = $url;
968 }
969
970 return $newurl;
971 }
972
973 /************************** Link Library Uninstall Function **************************/
974 function ll_uninstall() {
975 $genoptions = get_option( 'LinkLibraryGeneral' );
976
977 if ( !empty( $genoptions ) ) {
978 if ( isset( $genoptions['stylesheet'] ) && isset( $genoptions['fullstylesheet'] ) && !empty( $genoptions['stylesheet'] ) && empty( $genoptions['fullstylesheet'] ) ) {
979 $stylesheetlocation = plugins_url( $genoptions['stylesheet'], __FILE__ );
980 if ( file_exists( $stylesheetlocation ) )
981 $genoptions['fullstylesheet'] = file_get_contents( $stylesheetlocation );
982
983 update_option( 'LinkLibraryGeneral', $genoptions );
984 }
985 }
986 }
987
988 function ll_register_script() {
989 wp_register_script( 'form-validator', plugins_url( '/form-validator/jquery.form-validator.min.js' , __FILE__ ), array( 'jquery' ), '1.0.0', true );
990 wp_register_script( 'tiptip', plugins_url( '/tiptip/jquery.tipTip.minified.js' , __FILE__ ), array( 'jquery' ), '1.0.0', true );
991 }
992
993 function db_prefix() {
994 global $wpdb;
995 if ( method_exists( $wpdb, 'get_blog_prefix' ) ) {
996 return $wpdb->get_blog_prefix();
997 } else {
998 return $wpdb->prefix;
999 }
1000 }
1001
1002 function ll_add_protocols( $protocols ) {
1003 $genoptions = get_option( 'LinkLibraryGeneral' );
1004
1005 if ( isset( $genoptions['extraprotocols'] ) && !empty( $genoptions['extraprotocols'] ) ) {
1006 $extra_protocol_array = explode( ',', $genoptions['extraprotocols'] );
1007
1008 if ( !empty( $extra_protocol_array ) ) {
1009 foreach( $extra_protocol_array as $extra_protocol ) {
1010 $protocols[] = $extra_protocol;
1011 }
1012 }
1013 }
1014
1015 return $protocols;
1016 }
1017
1018 /******************************************** Print style data to header *********************************************/
1019
1020 function ll_rss_link() {
1021 global $llstylesheet, $rss_settings, $settingssetsids;
1022
1023 if ( !empty( $rss_settings ) ) {
1024 $settingsname = 'LinkLibraryPP' . $rss_settings;
1025 $options = get_option( $settingsname );
1026 $options = wp_parse_args( $options, ll_reset_options( 1, 'list', 'return' ) );
1027
1028 $feedtitle = ( empty( $options['rssfeedtitle'] ) ? __('Link Library Generated Feed', 'link-library') : $options['rssfeedtitle'] );
1029
1030 $xpath = $this->relativePath( dirname( __FILE__ ), ABSPATH );
1031 echo '<link rel="alternate" type="application/rss+xml" title="' . esc_html( stripslashes( $feedtitle ) ) . '" href="' . home_url('/feed/linklibraryfeed?settingsset=' . $rss_settings/* . '&xpath=' . $xpath*/) . '" />';
1032 unset( $xpath );
1033 }
1034
1035 if ( $llstylesheet ) {
1036 $genoptions = get_option( 'LinkLibraryGeneral' );
1037 if ( isset( $genoptions['fullstylesheet'] ) ) {
1038 echo "<style id='LinkLibraryStyle' type='text/css'>\n";
1039 echo stripslashes( sanitize_text_field( $genoptions['fullstylesheet'] ) );
1040 echo "</style>\n";
1041 }
1042 }
1043
1044 if ( !empty( $settingssetsids ) ) {
1045 $processedsettings = array();
1046 foreach ( $settingssetsids as $setting ) {
1047 if ( !in_array( $setting, $processedsettings ) ) {
1048 $processedsettings[] = $setting;
1049 $settingsname = 'LinkLibraryPP' . $setting;
1050 $options = get_option( $settingsname );
1051 $options = wp_parse_args( $options, ll_reset_options( 1, 'list', 'return' ) );
1052
1053 if ( !empty( $options['stylesheet'] ) ) {
1054 echo "<style id='LinkLibrarySettings" . $setting . "Style' type='text/css'>\n";
1055 echo stripslashes( sanitize_text_field( $options['stylesheet'] ) ) . "\n";
1056 echo "</style>\n";
1057 }
1058 }
1059 }
1060 }
1061 }
1062
1063 /****************************************** Add Link Category name to page title when option is present ********************************/
1064 function ll_title_creator( $title ) {
1065 global $wp_query;
1066 global $wpdb;
1067 global $llstylesheet;
1068
1069 if ( $llstylesheet ) {
1070 $genoptions = get_option( 'LinkLibraryGeneral' );
1071
1072 $categoryname = ( isset( $wp_query->query_vars['cat_name'] ) ? $wp_query->query_vars['cat_name'] : '' );
1073 $catid = ( isset( $_GET['cat_id'] ) ? intval( $_GET['cat_id'] ) : '' );
1074
1075 $linkcatquery = 'SELECT t.name ';
1076 $linkcatquery .= 'FROM ' . $this->db_prefix() . 'terms t LEFT JOIN ' . $this->db_prefix(). 'term_taxonomy tt ON (t.term_id = tt.term_id) ';
1077 $linkcatquery .= 'LEFT JOIN ' . $this->db_prefix() . 'term_relationships tr ON (tt.term_taxonomy_id = tr.term_taxonomy_id) ';
1078 $linkcatquery .= 'WHERE tt.taxonomy = "link_category" AND ';
1079
1080 if ( !empty( $categoryname ) ) {
1081 $linkcatquery .= 't.slug = "' . $categoryname . '"';
1082 $nicecatname = $wpdb->get_var( $linkcatquery );
1083 return $title . $genoptions['pagetitleprefix'] . $nicecatname . $genoptions['pagetitlesuffix'];
1084 } elseif ( !empty( $catid ) ) {
1085 $linkcatquery .= 't.term_id = "' . $catid . '"';
1086 $nicecatname = $wpdb->get_var( $linkcatquery );
1087 return $title . $genoptions['pagetitleprefix'] . $nicecatname . $genoptions['pagetitlesuffix'];
1088 }
1089 }
1090
1091 return $title;
1092 }
1093
1094 function ll_get_permalink( $url, $post_id, $sample, $type ) {
1095 if ( !is_admin() && is_search() && 'link_library_links' == get_post_type() ) {
1096 $genoptions = get_option( 'LinkLibraryGeneral' );
1097 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
1098
1099 if ( $genoptions['globalsearchresultslinkurl'] ) {
1100 $link_url = get_post_meta( get_the_ID(), 'link_url', true );
1101
1102 if ( !empty( $link_url ) ) {
1103 return $link_url;
1104 }
1105 }
1106 }
1107
1108 return $url;
1109 }
1110
1111 function ll_get_title( $title, $id = null ) {
1112 if ( !is_admin() && is_search() && 'link_library_links' == get_post_type() ) {
1113 $genoptions = get_option( 'LinkLibraryGeneral' );
1114 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
1115
1116 $post = get_post( $id );
1117 if ( $post instanceof WP_Post && !empty( $genoptions['globalsearchresultstitleprefix'] ) ) {
1118 return $genoptions['globalsearchresultstitleprefix'] . $title;
1119 }
1120 }
1121
1122 return $title;
1123 }
1124
1125 /************************************* Function to add to rewrite rules for permalink support **********************************/
1126 function ll_insertMyRewriteRules( $rules ) {
1127 $newrules = array();
1128
1129 $genoptions = get_option( 'LinkLibraryGeneral' );
1130 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
1131
1132 if ( !empty( $genoptions ) ) {
1133 for ( $i = 1; $i <= $genoptions['numberstylesets']; $i++ ) {
1134 $settingsname = 'LinkLibraryPP' . $i;
1135 $options = get_option( $settingsname );
1136 $options = wp_parse_args( $options, ll_reset_options( 1, 'list', 'return' ) );
1137
1138 if ( $options['enablerewrite'] && !empty( $options['rewritepage'] ) ) {
1139 if ( is_multisite() ) {
1140 $newrules['(' . $options['rewritepage'] . ')/(.+?)$'] = 'index.php?pagename=$matches[2]&cat_name=$matches[3]';
1141 } else {
1142 $newrules['(' . $options['rewritepage'] . ')/(.+?)$'] = 'index.php?pagename=$matches[1]&cat_name=$matches[2]';
1143 }
1144 }
1145
1146 if ( $options['publishrssfeed'] ) {
1147 $xpath = $this->relativePath( dirname( __FILE__ ), ABSPATH );
1148
1149 if ( !empty( $options['rssfeedaddress'] ) ) {
1150 $newrules['(' . $options['rssfeedaddress'] . ')/(.+?)$'] = home_url() . '/feed/linklibraryfeed?settingsset=$matches[1]';
1151 }
1152 unset( $xpath );
1153 }
1154 }
1155 }
1156
1157 return $newrules + $rules;
1158 }
1159
1160 // Adding the id var so that WP recognizes it
1161 function ll_insertMyRewriteQueryVars( $vars ) {
1162 array_push( $vars, 'cat_name' );
1163 return $vars;
1164 }
1165
1166 function relativePath( $from, $to, $ps = DIRECTORY_SEPARATOR ) {
1167 $arFrom = explode( $ps, rtrim( $from, $ps ) );
1168 $arTo = explode( $ps, rtrim( $to, $ps ) );
1169 while( count( $arFrom ) && count( $arTo ) && ( $arFrom[0] == $arTo[0] ) ) {
1170 array_shift( $arFrom );
1171 array_shift( $arTo );
1172 }
1173 $return = str_pad( '', count($arFrom) * 3, '..'.$ps ) . implode( $ps, $arTo );
1174
1175 // Don't disclose anything about the path is it's not needed, i.e. is the standard
1176 if( $return === '../../../' ) {
1177 $return = '';
1178 }
1179
1180 return $return;
1181 }
1182
1183 function CheckReciprocalLink( $RecipCheckAddress = '', $external_link = '', $request_type = 'reciprocal' ) {
1184
1185 $response = wp_safe_remote_get( $external_link, array( 'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36', 'timeout' => 10 ) );
1186
1187 if( is_wp_error( $response ) ) {
1188 $data = file_get_contents( $external_link );
1189 if ( false !== $data ) {
1190 return 'exists_found';
1191 } else {
1192 $response_code = $response->get_error_code();
1193 if ( 'http_request_failed' == $response_code ) {
1194 return 'error_403';
1195 }
1196 }
1197 } elseif ( $response['response']['code'] == '200' ) {
1198
1199 $original_protocol = '';
1200 $actual_protocol = '';
1201
1202 if ( false !== strpos( $external_link, 'http://' ) ) {
1203 $original_protocol = 'http://';
1204 } else if ( false !== strpos( $external_link, 'https://' ) ) {
1205 $original_protocol = 'https://';
1206 }
1207
1208 if ( false !== strpos( $response['http_response']->get_response_object()->url, 'http://' ) ) {
1209 $actual_protocol = 'http://';
1210 } else if ( false !== strpos( $response['http_response']->get_response_object()->url, 'https://' ) ) {
1211 $actual_protocol = 'https://';
1212 }
1213
1214 $link_url_without_protocol = str_replace( 'http://', '', $external_link );
1215 $link_url_without_protocol = str_replace( 'https://', '', $link_url_without_protocol );
1216 $link_url_without_protocol = rtrim( $link_url_without_protocol, '/' );
1217
1218 $response_url_without_protocol = str_replace( 'http://', '', $response['http_response']->get_response_object()->url );
1219 $response_url_without_protocol = str_replace( 'https://', '', $response_url_without_protocol );
1220 $response_url_without_protocol = rtrim( $response_url_without_protocol, '/' );
1221
1222 $parse_original_url = parse_url( $external_link );
1223 $parse_actual_url = parse_url( $response['http_response']->get_response_object()->url );
1224
1225 if ( ( 'broken' == $request_type || 'rss' == $request_type || 'secondary' == $request_type || 'image' == $request_type ) && $parse_original_url['host'] != $parse_actual_url['host'] ) {
1226 $original_host_segments = explode( '.', $parse_original_url['host'] );
1227 $actual_host_segments = explode( '.', $parse_actual_url['host'] );
1228
1229 if ( sizeof( $actual_host_segments ) > sizeof( $original_host_segments ) ) {
1230 if ( $original_host_segments[ sizeof( $original_host_segments ) - 1 ] == $actual_host_segments[ sizeof( $actual_host_segments ) - 1] && $original_host_segments[ sizeof( $original_host_segments ) - 2 ] == $actual_host_segments[ sizeof( $actual_host_segments ) - 2] ) {
1231 return 'exists_subdomain_redirect';
1232 }
1233 }
1234 }
1235
1236 if ( ( 'broken' == $request_type || 'rss' == $request_type || 'secondary' == $request_type || 'image' == $request_type ) && $parse_original_url['host'] == $parse_actual_url['host'] && $link_url_without_protocol != $response_url_without_protocol ) {
1237 $basename = basename( $parse_actual_url['path'] );
1238 if ( false !== strpos( $basename, '.' ) ) {
1239 return 'exists_redirected_fileurl';
1240 } else {
1241 return 'exists_redirected_subfolder';
1242 }
1243 }
1244
1245 if ( ( 'broken' == $request_type || 'rss' == $request_type || 'secondary' == $request_type || 'image' == $request_type ) && $link_url_without_protocol != $response_url_without_protocol ) {
1246 return 'exists_redirected';
1247 } elseif ( ( 'broken' == $request_type || 'rss' == $request_type || 'secondary' == $request_type || 'image' == $request_type ) && !empty( $original_protocol ) && !empty( $actual_protocol ) && $original_protocol != $actual_protocol ) {
1248 return 'exists_protocol_redirect';
1249 } elseif ( ( 'broken' == $request_type || 'rss' == $request_type || 'secondary' == $request_type || 'image' == $request_type ) && empty( $RecipCheckAddress ) ) {
1250 return 'exists_notfound';
1251 } else {
1252 return 'exists_found';
1253 }
1254
1255 if ( 'reciprocal' == $request_type && strpos( $response['body'], $RecipCheckAddress ) === false ) {
1256 return 'exists_notfound';
1257 } elseif ( 'reciprocal' == $request_type && strpos( $response['body'], $RecipCheckAddress ) !== false ) {
1258 return 'exists_found';
1259 }
1260 }
1261
1262 return 'unreachable';
1263 }
1264
1265 /* Output for users trying to directly call Link Library function, as was possible in pre-1.0 versions */
1266
1267 function LinkLibraryCategories() {
1268 return __( 'Link Library no longer supports calling this function with individual arguments. Please use the admin panel to configure Link Library and the do_shortcode function to use Link Library output in your code.', 'link-library' );
1269 }
1270
1271 function LinkLibrary() {
1272 return __( 'Link Library no longer supports calling this function with individual arguments. Please use the admin panel to configure Link Library and the do_shortcode function to use Link Library output in your code.', 'link-library' );
1273 }
1274
1275 /********************************************** Function to Process [link-library-cats] shortcode *********************************************/
1276
1277 function link_library_cats_func( $atts ) {
1278 $categorylistoverride = '';
1279 $excludecategoryoverride = '';
1280 $settings = '';
1281 $targetlibrary = '';
1282
1283 if ( isset( $atts['categorylistoverride'] ) && !empty( $atts['categorylistoverride'] ) && is_array( $atts['categorylistoverride'] ) ) {
1284 $atts['categorylistoverride'] = implode( ',', $atts['categorylistoverride'] );
1285 }
1286
1287 if ( isset( $atts['categorylistoverrideCSV'] ) && !empty( $atts['categorylistoverrideCSV'] ) ) {
1288 $atts['categorylistoverride'] = $atts['categorylistoverrideCSV'];
1289 }
1290
1291 if ( isset( $atts['excludecategoryoverride'] ) && !empty( $atts['excludecategoryoverride'] ) && is_array( $atts['excludecategoryoverride'] ) ) {
1292 $atts['excludecategoryoverride'] = implode( ',', $atts['excludecategoryoverride'] );
1293 }
1294
1295 if ( isset( $atts['excludecategoryoverrideCSV'] ) && !empty( $atts['excludecategoryoverrideCSV'] ) ) {
1296 $atts['excludecategoryoverride'] = $atts['excludecategoryoverrideCSV'];
1297 }
1298
1299 if ( isset( $atts['taglistoverride'] ) && !empty( $atts['taglistoverride'] ) && is_array( $atts['taglistoverride'] ) ) {
1300 $atts['taglistoverride'] = implode( ',', $atts['taglistoverride'] );
1301 }
1302
1303 if ( isset( $atts['taglistoverrideCSV'] ) && !empty( $atts['taglistoverrideCSV'] ) ) {
1304 $atts['taglistoverrideCSV'] = $atts['taglistoverrideCSV'];
1305 }
1306
1307 extract( shortcode_atts( array (
1308 'categorylistoverride' => '',
1309 'excludecategoryoverride' => '',
1310 'settings' => '',
1311 'targetlibrary' => '',
1312 'taglistoverride' => '',
1313 'parent_cat_id' => 0
1314 ), $atts ) );
1315
1316 $genoptions = get_option( 'LinkLibraryGeneral' );
1317 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
1318
1319 if ( empty( $settings ) ) {
1320 $settings = 1;
1321 }
1322
1323 if ( $settings > $genoptions['numberstylesets'] ) {
1324 $settings = 1;
1325 }
1326
1327 $settingsname = 'LinkLibraryPP' . intval( $settings );
1328 $options = get_option( $settingsname );
1329 $options = wp_parse_args( $options, ll_reset_options( 1, 'list', 'return' ) );
1330
1331 if ( empty( $options ) ) {
1332 $settingsname = 'LinkLibraryPP1';
1333 $options = get_option( $settingsname );
1334 $options = wp_parse_args( $options, ll_reset_options( 1, 'list', 'return' ) );
1335 }
1336
1337 if ( !empty( $categorylistoverride ) ) {
1338 $options['categorylist_cpt'] = $categorylistoverride;
1339
1340 $update_list = false;
1341 $category_list_array = explode( ',', $categorylistoverride );
1342 foreach( $category_list_array as $index => $category_text ) {
1343 if ( !is_numeric( $category_text ) ) {
1344 $update_list = true;
1345 $matched_term = get_term_by( 'slug', $category_text, $genoptions['cattaxonomy'] );
1346
1347 if ( $matched_term ) {
1348 $category_list_array[$index] = $matched_term->term_id;
1349 } else {
1350 unset( $category_list_array[$index] );
1351 }
1352 }
1353 }
1354 if ( $update_list ) {
1355 $options['categorylist_cpt'] = implode( ',', $category_list_array );
1356 }
1357 }
1358
1359 if ( !empty( $excludecategoryoverride ) ) {
1360 $options['excludecategorylist_cpt'] = $excludecategoryoverride;
1361
1362 $update_list = false;
1363 $exclude_category_list_array = explode( ',', $excludecategoryoverride );
1364 foreach( $exclude_category_list_array as $index => $category_text ) {
1365 if ( !is_numeric( $category_text ) ) {
1366 $update_list = true;
1367 $matched_term = get_term_by( 'slug', $category_text, $genoptions['cattaxonomy'] );
1368
1369 if ( $matched_term ) {
1370 $exclude_category_list_array[$index] = $matched_term->term_id;
1371 } else {
1372 unset( $exclude_category_list_array[$index] );
1373 }
1374 }
1375 }
1376 if ( $update_list ) {
1377 $options['categorylist_cpt'] = implode( ',', $exclude_category_list_array );
1378 }
1379 }
1380
1381 if ( !empty( $taglistoverride ) ) {
1382 $options['taglist_cpt'] = $taglistoverride;
1383
1384 $update_list = false;
1385 $tag_list_array = explode( ',', $taglistoverride );
1386 foreach( $tag_list_array as $index => $tag_text ) {
1387 if ( !is_numeric( $tag_text ) ) {
1388 $update_list = true;
1389 $matched_term = get_term_by( 'slug', $tag_text, $genoptions['tagtaxonomy'] );
1390
1391 if ( $matched_term ) {
1392 $tag_list_array[$index] = $matched_term->term_id;
1393 } else {
1394 unset( $tag_list_array[$index] );
1395 }
1396 }
1397 }
1398 if ( $update_list ) {
1399 $options['taglist_cpt'] = implode( ',', $tag_list_array );
1400 }
1401 }
1402
1403 if ( !empty( $excludetagoverride ) ) {
1404 $options['excludetaglist_cpt'] = $excludetagoverride;
1405
1406 $update_list = false;
1407 $exclude_tag_list_array = explode( ',', $excludetagoverride );
1408 foreach( $exclude_tag_list_array as $index => $tag_text ) {
1409 if ( !is_numeric( $tag_text ) ) {
1410 $update_list = true;
1411 $matched_term = get_term_by( 'slug', $tag_text, $genoptions['tagtaxonomy'] );
1412
1413 if ( $matched_term ) {
1414 $exclude_tag_list_array[$index] = $matched_term->term_id;
1415 } else {
1416 unset( $exclude_tag_list_array[$index] );
1417 }
1418 }
1419 }
1420 if ( $update_list ) {
1421 $options['excludetaglist_cpt'] = implode( ',', $exclude_tag_list_array );
1422 }
1423 }
1424
1425 if ( $genoptions['debugmode'] ) {
1426 $mainoutputstarttime = microtime( true );
1427 $timeoutputstart = "\n<!-- Start Link Library Cats Time: " . $mainoutputstarttime . "-->\n";
1428 }
1429
1430 require_once plugin_dir_path( __FILE__ ) . 'render-link-library-cats-sc.php';
1431
1432 if ( $genoptions['debugmode'] ) {
1433 $timeoutput = "\n<!-- [link-library-cats] shortcode execution time: " . ( microtime( true ) - $mainoutputstarttime ) . "-->\n";
1434 }
1435
1436
1437 $parent_id_array = array();
1438 return ( true == $genoptions['debugmode'] ? $timeoutputstart : '' ) . RenderLinkLibraryCategories( $this, $genoptions, $options, intval( $settings ), $targetlibrary, $parent_cat_id, 0, $parent_id_array ) . ( true == $genoptions['debugmode'] ? $timeoutput : '' );
1439 }
1440
1441 /********************************************** Function to Process [link-library-search] shortcode *********************************************/
1442
1443 function link_library_search_func($atts) {
1444 $settings = '';
1445 $genoptions = get_option( 'LinkLibraryGeneral' );
1446 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
1447
1448 extract(shortcode_atts(array(
1449 'settings' => ''
1450 ), $atts));
1451
1452 if ( empty( $settings ) ) {
1453 $settings = 1;
1454 }
1455
1456 if ( $settings > $genoptions['numberstylesets'] ) {
1457 $settings = 1;
1458 }
1459
1460 $settingsname = 'LinkLibraryPP' . intval( $settings );
1461 $options = get_option( $settingsname );
1462 $options = wp_parse_args( $options, ll_reset_options( 1, 'list', 'return' ) );
1463
1464 if ( empty( $options ) ) {
1465 $settingsname = 'LinkLibraryPP1';
1466 $options = get_option( $settingsname );
1467 $options = wp_parse_args( $options, ll_reset_options( 1, 'list', 'return' ) );
1468 }
1469
1470 require_once plugin_dir_path( __FILE__ ) . 'render-link-library-search-sc.php';
1471 return RenderLinkLibrarySearchForm( $options );
1472 }
1473
1474 /********************************************** Function to Process [link-library-add-link] shortcode *********************************************/
1475
1476 function link_library_addlink_func( $atts, $content, $code ) {
1477 $settings = '';
1478 $categorylistoverride = '';
1479 $excludecategoryoverride = '';
1480 $addlinkdefaultcatoverride = '';
1481
1482 extract(shortcode_atts(array(
1483 'settings' => '',
1484 'categorylistoverride' => '',
1485 'excludecategoryoverride' => '',
1486 'addlinkdefaultcatoverride' => ''
1487 ), $atts));
1488
1489 if ( empty( $code ) || is_object( $code ) ) {
1490 $code = 'link-library-addlink';
1491 }
1492
1493 $genoptions = get_option( 'LinkLibraryGeneral' );
1494 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
1495
1496 if ( empty( $settings ) ) {
1497 $settings = 1;
1498 } else if ( isset( $_POST['settings'] ) ) {
1499 $settings = intval( $_POST['settings'] );
1500 }
1501
1502 if ( $settings > $genoptions['numberstylesets'] ) {
1503 $settings = 1;
1504 }
1505
1506 $settingsname = 'LinkLibraryPP' . intval( $settings );
1507 $options = get_option( $settingsname );
1508 $options = wp_parse_args( $options, ll_reset_options( 1, 'list', 'return' ) );
1509
1510 if ( empty( $options ) ) {
1511 $settingsname = 'LinkLibraryPP1';
1512 $options = get_option( $settingsname );
1513 $options = wp_parse_args( $options, ll_reset_options( 1, 'list', 'return' ) );
1514 }
1515
1516 if ( !empty( $categorylistoverride ) ) {
1517 $options['categorylist_cpt'] = $categorylistoverride;
1518 } elseif ( !empty( $options['addlinkcatlistoverride'] ) ) {
1519 $options['categorylist_cpt'] = $options['addlinkcatlistoverride'];
1520 }
1521
1522 if ( !empty( $excludecategoryoverride ) ) {
1523 $options['excludecategorylist_cpt'] = $excludecategoryoverride;
1524 }
1525
1526 if ( !empty( $addlinkdefaultcatoverride ) ) {
1527 $options['addlinkdefaultcat'] = $addlinkdefaultcatoverride;
1528 }
1529
1530 require_once plugin_dir_path( __FILE__ ) . 'render-link-library-addlink-sc.php';
1531 if ( 'inline' == $options['addlinkformdisplaymode'] ) {
1532 return RenderLinkLibraryAddLinkForm( $this, $genoptions, $options, intval( $settings ), $code );
1533 } elseif ( 'popup' == $options['addlinkformdisplaymode'] ) {
1534 return RenderLinkLibraryAddLinkButton( $this, $genoptions, $options, intval( $settings ), $code );
1535 }
1536
1537 }
1538
1539 /********************************************** Function to Process [link-library-count] shortcode ***************************************/
1540
1541 function link_library_count_func( $atts ) {
1542 extract( shortcode_atts( array(
1543 'categorylistoverride' => '',
1544 'excludecategoryoverride' => '',
1545 'settings' => ''
1546 ), $atts ) );
1547
1548 $genoptions = get_option( 'LinkLibraryGeneral' );
1549 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
1550
1551 if ( empty( $settings ) ) {
1552 $settings = 1;
1553 } else if ( isset( $_POST['settings'] ) ) {
1554 $settings = intval( $_POST['settings'] );
1555 }
1556
1557 if ( $settings > $genoptions['numberstylesets'] ) {
1558 $settings = 1;
1559 }
1560
1561 $settingsname = 'LinkLibraryPP' . intval( $settings );
1562 $options = get_option( $settingsname );
1563 $options = wp_parse_args( $options, ll_reset_options( 1, 'list', 'return' ) );
1564
1565 if ( empty( $options ) ) {
1566 $settingsname = 'LinkLibraryPP1';
1567 $options = get_option( $settingsname );
1568 $options = wp_parse_args( $options, ll_reset_options( 1, 'list', 'return' ) );
1569 }
1570
1571 $linkeditoruser = current_user_can( 'manage_options' );
1572
1573 if ( !empty( $categorylistoverride ) ) {
1574 $options['categorylist_cpt'] = $categorylistoverride;
1575 }
1576
1577 if ( !empty( $excludecategoryoverride ) ) {
1578 $options['excludecategorylist_cpt'] = $excludecategoryoverride;
1579 }
1580
1581 $link_query_args = array( 'post_type' => 'link_library_links', 'posts_per_page' => -1 );
1582 $link_query_args['post_status'] = array( 'publish' );
1583
1584 if ( !empty( $options['categorylist_cpt'] ) ) {
1585 $catlistarray = explode( ',', $options['categorylist_cpt'] );
1586 $link_query_args['tax_query'] = array( array( 'taxonomy' => $genoptions['cattaxonomy'],
1587 'field' => 'term_id',
1588 'terms' => $catlistarray,
1589 'operator' => 'IN' ) );
1590 }
1591
1592 if ( !empty( $options['excludecategorylist_cpt'] ) ) {
1593 $catlistexcludearray = explode( ',', $options['excludecategorylist_cpt'] );
1594 $link_query_args['tax_query'] = array( array( 'taxonomy' => $genoptions['cattaxonomy'],
1595 'field' => 'term_id',
1596 'terms' => $catlistexcludearray,
1597 'operator' => 'NOT IN' ) );
1598 }
1599
1600 if ( $options['showuserlinks'] ) {
1601 $link_query_args['post_status'][] = 'pending';
1602 }
1603
1604 if ( $options['showinvisible'] || ( $options['showinvisibleadmin'] && $linkeditoruser ) ) {
1605 $link_query_args['post_status'][] = 'draft';
1606 }
1607
1608 if ( $options['showscheduledlinks'] ) {
1609 $link_query_args['post_status'][] = 'future';
1610 }
1611
1612 $the_link_query = new WP_Query( $link_query_args );
1613
1614 wp_reset_postdata();
1615
1616 return $the_link_query->found_posts;
1617 }
1618
1619 /********************************************** Function to Process [link-library-filters] shortcode ***************************************/
1620
1621 function link_library_filters( $atts ) {
1622 extract( shortcode_atts( array(
1623 'includetagsids' => '',
1624 'excludetagsids' => '',
1625 'showtagfilters' => true,
1626 'taglabel' => __( 'Tag', 'link-library' ),
1627 'showpricefilters' => true,
1628 'pricelabel' => __( 'Price', 'link-library' ),
1629 'alphabeticlabel' => __( 'Link Name', 'link-library' ),
1630 'showalphabeticfilters' => true,
1631 'showapplybutton' => false,
1632 'settings' => ''
1633 ), $atts ) );
1634
1635 $genoptions = get_option( 'LinkLibraryGeneral' );
1636 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
1637
1638 if ( empty( $settings ) ) {
1639 $settings = 1;
1640 } else if ( isset( $_POST['settings'] ) ) {
1641 $settings = intval( $_POST['settings'] );
1642 }
1643
1644 if ( $settings > $genoptions['numberstylesets'] ) {
1645 $settings = 1;
1646 }
1647
1648 $settingsname = 'LinkLibraryPP' . intval( $settings );
1649 $options = get_option( $settingsname );
1650 $options = wp_parse_args( $options, ll_reset_options( 1, 'list', 'return' ) );
1651
1652 if ( empty( $options ) ) {
1653 $settingsname = 'LinkLibraryPP1';
1654 $options = get_option( $settingsname );
1655 $options = wp_parse_args( $options, ll_reset_options( 1, 'list', 'return' ) );
1656 }
1657
1658 require_once plugin_dir_path( __FILE__ ) . 'render-link-library-tag-filter-sc.php';
1659 return RenderLinkLibraryFilterBox( $this, $genoptions, $options, intval( $settings ), $includetagsids, $excludetagsids, $showtagfilters, $taglabel, $showpricefilters, $pricelabel, $showalphabeticfilters, $alphabeticlabel, $showapplybutton );
1660 }
1661
1662 /********************************************** Function to Process [link-library] shortcode *********************************************/
1663
1664 function link_library_func( $atts = '' ) {
1665 if ( isset( $_POST['ajaxupdate'] ) ) {
1666 check_ajax_referer( 'link_library_ajax_refresh' );
1667 }
1668
1669 $settings = '';
1670 $notesoverride = '';
1671 $descoverride = '';
1672 $rssoverride = '';
1673 $categorylistoverride = '';
1674 $excludecategoryoverride = '';
1675 $tableoverride = '';
1676 $singlelinkid = '';
1677 $showonecatonlyoverride = false;
1678 $taglistoverride = '';
1679 $maxlinksoverride = '';
1680 $linkorderoverride = '';
1681 $linkdirectionoverride = '';
1682 $addlinkdefaultcatoverride = '';
1683 $urltextfilteroverride = '';
1684
1685 if ( isset( $atts['categorylistoverride'] ) && !empty( $atts['categorylistoverride'] ) && is_array( $atts['categorylistoverride'] ) ) {
1686 $atts['categorylistoverride'] = implode( ',', $atts['categorylistoverride'] );
1687 }
1688
1689 if ( isset( $atts['categorylistoverrideCSV'] ) && !empty( $atts['categorylistoverrideCSV'] ) ) {
1690 $atts['categorylistoverride'] = $atts['categorylistoverrideCSV'];
1691 }
1692
1693 if ( isset( $atts['excludecategoryoverride'] ) && !empty( $atts['excludecategoryoverride'] ) && is_array( $atts['excludecategoryoverride'] ) ) {
1694 $atts['excludecategoryoverride'] = implode( ',', $atts['excludecategoryoverride'] );
1695 }
1696
1697 if ( isset( $atts['excludecategoryoverrideCSV'] ) && !empty( $atts['excludecategoryoverrideCSV'] ) ) {
1698 $atts['excludecategoryoverride'] = $atts['excludecategoryoverrideCSV'];
1699 }
1700
1701 if ( isset( $atts['taglistoverride'] ) && !empty( $atts['taglistoverride'] ) && is_array( $atts['taglistoverride'] ) ) {
1702 $atts['taglistoverride'] = implode( ',', $atts['taglistoverride'] );
1703 }
1704
1705 if ( isset( $atts['taglistoverrideCSV'] ) && !empty( $atts['taglistoverrideCSV'] ) ) {
1706 $atts['taglistoverride'] = $atts['taglistoverrideCSV'];
1707 }
1708
1709 extract( shortcode_atts( array(
1710 'categorylistoverride' => '',
1711 'excludecategoryoverride' => '',
1712 'notesoverride' => '',
1713 'descoverride' => '',
1714 'rssoverride' => '',
1715 'tableoverride' => '',
1716 'settings' => '',
1717 'singlelinkid' => '',
1718 'showonecatonlyoverride' => '',
1719 'taglistoverride' => '',
1720 'excludetagoverride' => '',
1721 'maxlinksoverride' => '',
1722 'linkorderoverride' => '',
1723 'linkdirectionoverride' => '',
1724 'addlinkdefaultcatoverride' => '',
1725 'urltextfilteroverride' => '',
1726 ), $atts ) );
1727
1728 $genoptions = get_option( 'LinkLibraryGeneral' );
1729 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
1730
1731 if ( floatval( $genoptions['schemaversion'] ) < '5.0' ) {
1732 $this->ll_install();
1733 }
1734
1735 if ( empty( $settings ) && !isset( $_POST['settings'] ) ) {
1736 $settings = 1;
1737 } else if ( isset( $_POST['settings'] ) ) {
1738 $settings = intval( $_POST['settings'] );
1739 }
1740
1741 if ( $settings > $genoptions['numberstylesets'] ) {
1742 $settings = 1;
1743 }
1744
1745 $settingsname = 'LinkLibraryPP' . intval( $settings );
1746 $options = get_option( $settingsname );
1747 $options = wp_parse_args( $options, ll_reset_options( 1, 'list', 'return' ) );
1748
1749 if ( empty( $options ) ) {
1750 $settingsname = 'LinkLibraryPP1';
1751 $options = get_option( $settingsname );
1752 $options = wp_parse_args( $options, ll_reset_options( 1, 'list', 'return' ) );
1753 }
1754
1755 $options['AJAXcatid'] = '';
1756 $options['AJAXpageid'] = '';
1757
1758 if ( !empty( $notesoverride ) ) {
1759 $options['shownotes'] = $notesoverride;
1760 }
1761
1762 if ( !empty( $descoverride ) ) {
1763 $options['showdescription'] = $descoverride;
1764 }
1765
1766 if ( !empty( $rssoverride ) ) {
1767 $options['show_rss'] = $rssoverride;
1768 }
1769
1770 if ( !empty( $maxlinksoverride ) ) {
1771 $options['maxlinks'] = intval( $maxlinksoverride );
1772 }
1773
1774 if ( !empty( $urltextfilteroverride ) ) {
1775 $options['urltextfilter'] = $urltextfilteroverride;
1776 }
1777
1778 if ( !empty( $linkorderoverride ) ) {
1779 $validlinkorder = array( 'name', 'id', 'random', 'date', 'hits', 'scpo', 'pubdate', 'uservotes' );
1780 if ( in_array( $linkorderoverride, $validlinkorder ) ) {
1781 $options['linkorder'] = $linkorderoverride;
1782 }
1783 }
1784
1785 if ( !empty( $linkdirectionoverride ) ) {
1786 $validlinkdirection = array( 'ASC', 'DESC' );
1787 if ( in_array( $linkdirectionoverride, $validlinkdirection ) ) {
1788 $options['linkdirection'] = $linkdirectionoverride;
1789 }
1790 }
1791
1792 if ( !empty( $categorylistoverride ) ) {
1793 $options['categorylist_cpt'] = $categorylistoverride;
1794
1795 $update_list = false;
1796 $category_list_array = explode( ',', $categorylistoverride );
1797 foreach( $category_list_array as $index => $category_text ) {
1798 if ( !is_numeric( $category_text ) ) {
1799 $update_list = true;
1800 $matched_term = get_term_by( 'slug', $category_text, $genoptions['cattaxonomy'] );
1801
1802 if ( $matched_term ) {
1803 $category_list_array[$index] = $matched_term->term_id;
1804 } else {
1805 unset( $category_list_array[$index] );
1806 }
1807 }
1808 }
1809 if ( $update_list ) {
1810 $options['categorylist_cpt'] = implode( ',', $category_list_array );
1811 }
1812 }
1813
1814 if ( !empty( $excludecategoryoverride ) ) {
1815 $options['excludecategorylist_cpt'] = $excludecategoryoverride;
1816
1817 $update_list = false;
1818 $exclude_category_list_array = explode( ',', $excludecategoryoverride );
1819 foreach( $exclude_category_list_array as $index => $category_text ) {
1820 if ( !is_numeric( $category_text ) ) {
1821 $update_list = true;
1822 $matched_term = get_term_by( 'slug', $category_text, $genoptions['cattaxonomy'] );
1823
1824 if ( $matched_term ) {
1825 $exclude_category_list_array[$index] = $matched_term->term_id;
1826 } else {
1827 unset( $exclude_category_list_array[$index] );
1828 }
1829 }
1830 }
1831 if ( $update_list ) {
1832 $options['excludecategorylist_cpt'] = implode( ',', $exclude_category_list_array );
1833 }
1834 }
1835
1836 if ( !empty( $taglistoverride ) ) {
1837 $options['taglist_cpt'] = $taglistoverride;
1838
1839 $update_list = false;
1840 $tag_list_array = explode( ',', $taglistoverride );
1841 foreach( $tag_list_array as $index => $tag_text ) {
1842 if ( !is_numeric( $tag_text ) ) {
1843 $update_list = true;
1844 $matched_term = get_term_by( 'slug', $tag_text, $genoptions['tagtaxonomy'] );
1845
1846 if ( $matched_term ) {
1847 $tag_list_array[$index] = $matched_term->term_id;
1848 } else {
1849 unset( $tag_list_array[$index] );
1850 }
1851 }
1852 }
1853 if ( $update_list ) {
1854 $options['taglist_cpt'] = implode( ',', $tag_list_array );
1855 }
1856 }
1857
1858 if ( !empty( $excludecategoryoverride ) ) {
1859 $options['excludetaglist_cpt'] = $excludetagoverride;
1860
1861 $update_list = false;
1862 $exclude_tag_list_array = explode( ',', $excludetagoverride );
1863 foreach( $exclude_tag_list_array as $index => $tag_text ) {
1864 if ( !is_numeric( $tag_text ) ) {
1865 $update_list = true;
1866 $matched_term = get_term_by( 'slug', $tag_text, $genoptions['tagtaxonomy'] );
1867
1868 if ( $matched_term ) {
1869 $exclude_tag_list_array[$index] = $matched_term->term_id;
1870 } else {
1871 unset( $exclude_tag_list_array[$index] );
1872 }
1873 }
1874 }
1875 if ( $update_list ) {
1876 $options['excludetaglist_cpt'] = implode( ',', $exclude_category_list_array );
1877 }
1878 }
1879
1880 if ( !empty( $singlelinkid ) ) {
1881 $options['singlelinkid'] = $singlelinkid;
1882 }
1883
1884 if ( $showonecatonlyoverride == 'false' || $showonecatonlyoverride == 'true' ) {
1885 if ( $showonecatonlyoverride == 'false' ) {
1886 $options['showonecatonly'] = false;
1887 } elseif ( $showonecatonlyoverride == 'true' ) {
1888 $options['showonecatonly'] = true;
1889 }
1890 }
1891
1892 if ( $options['showonecatonly'] ) {
1893 if ( !empty( $categorylistoverride ) && !empty( $options['defaultsinglecat_cpt'] ) ) {
1894 if ( !str_contains( $categorylistoverride, ',' ) ) {
1895 $options['defaultsinglecat_cpt'] = $categorylistoverride;
1896 }
1897 }
1898 }
1899
1900 if ( !empty( $tableoverride ) ) {
1901 $options['displayastable'] = $tableoverride;
1902 }
1903
1904 if ( isset( $_POST['ajaxupdate'] ) ) {
1905 if ( isset( $_POST['id'] ) ) {
1906 $catID = intval( $_POST['id'] );
1907 $options['AJAXcatid'] = $catID;
1908 }
1909
1910 if ( isset( $_POST['linkresultpage'] ) ) {
1911 $pageID = intval( $_POST['linkresultpage'] );
1912 $options['AJAXpageid'] = $pageID;
1913 }
1914 }
1915
1916 $linklibraryoutput = '';
1917
1918 if ( $genoptions['debugmode'] ) {
1919 $linklibraryoutput .= "\n<!-- Library Settings Info:" . print_r( $options, true ) . "-->\n";
1920 $mainoutputstarttime = microtime( true );
1921 $linklibraryoutput .= "\n<!-- Start Time: " . $mainoutputstarttime . "-->\n";
1922 }
1923
1924 require_once plugin_dir_path( __FILE__ ) . 'render-link-library-sc.php';
1925 $linkcount = 1;
1926 $linklibraryoutput .= RenderLinkLibrary( $linkcount, $this, $genoptions, $options, intval( $settings ), false, 0, 0, true, false );
1927
1928 if ( isset( $_POST['ajaxupdate'] ) ) {
1929 echo $linklibraryoutput;
1930
1931 if ( $genoptions['debugmode'] ) {
1932 echo "\n<!-- Execution Time: " . ( microtime( true ) - $mainoutputstarttime ) . "-->\n";
1933 }
1934 exit;
1935 } else {
1936 if ( $genoptions['debugmode'] ) {
1937 $timeoutput = "\n<!-- [link-library] shortcode execution time: " . ( microtime( true ) - $mainoutputstarttime ) . "-->\n";
1938 }
1939 return $linklibraryoutput . ( true == $genoptions['debugmode'] ? $timeoutput : '' );
1940 }
1941 }
1942
1943 /********************************************** Function to Process [link-library] shortcode *********************************************/
1944
1945 function rss_library_func( $atts = '' ) {
1946 $settings = '';
1947 $categorylistoverride = '';
1948 $excludecategoryoverride = '';
1949 $taglistoverride = '';
1950
1951 if ( isset( $atts['categorylistoverride'] ) && !empty( $atts['categorylistoverride'] ) && is_array( $atts['categorylistoverride'] ) ) {
1952 $atts['categorylistoverride'] = implode( ',', $atts['categorylistoverride'] );
1953 }
1954
1955 if ( isset( $atts['categorylistoverrideCSV'] ) && !empty( $atts['categorylistoverrideCSV'] ) ) {
1956 $atts['categorylistoverride'] = $atts['categorylistoverrideCSV'];
1957 }
1958
1959 if ( isset( $atts['excludecategoryoverride'] ) && !empty( $atts['excludecategoryoverride'] ) && is_array( $atts['excludecategoryoverride'] ) ) {
1960 $atts['excludecategoryoverride'] = implode( ',', $atts['excludecategoryoverride'] );
1961 }
1962
1963 if ( isset( $atts['excludecategoryoverrideCSV'] ) && !empty( $atts['excludecategoryoverrideCSV'] ) ) {
1964 $atts['excludecategoryoverride'] = $atts['excludecategoryoverrideCSV'];
1965 }
1966
1967 if ( isset( $atts['taglistoverride'] ) && !empty( $atts['taglistoverride'] ) && is_array( $atts['taglistoverride'] ) ) {
1968 $atts['taglistoverride'] = implode( ',', $atts['taglistoverride'] );
1969 }
1970
1971 if ( isset( $atts['taglistoverrideCSV'] ) && !empty( $atts['taglistoverrideCSV'] ) ) {
1972 $atts['taglistoverride'] = $atts['taglistoverrideCSV'];
1973 }
1974
1975 extract( shortcode_atts( array(
1976 'settings' => '',
1977 'categorylistoverride' => '',
1978 'excludecategoryoverride' => '',
1979 'taglistoverride' => '',
1980 ), $atts ) );
1981
1982 $genoptions = get_option( 'LinkLibraryGeneral' );
1983 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
1984
1985 if ( floatval( $genoptions['schemaversion'] ) < '5.0' ) {
1986 $this->ll_install();
1987 }
1988
1989 if ( empty( $settings ) && !isset( $_POST['settings'] ) ) {
1990 $settings = 1;
1991 } else if ( isset( $_POST['settings'] ) ) {
1992 $settings = intval( $_POST['settings'] );
1993 }
1994
1995 if ( $settings > $genoptions['numberstylesets'] ) {
1996 $settings = 1;
1997 }
1998
1999 $settingsname = 'LinkLibraryPP' . intval( $settings );
2000 $options = get_option( $settingsname );
2001 $options = wp_parse_args( $options, ll_reset_options( 1, 'list', 'return' ) );
2002
2003 if ( !empty( $categorylistoverride ) ) {
2004 $options['categorylist_cpt'] = $categorylistoverride;
2005
2006 $update_list = false;
2007 $category_list_array = explode( ',', $categorylistoverride );
2008 foreach( $category_list_array as $index => $category_text ) {
2009 if ( !is_numeric( $category_text ) ) {
2010 $update_list = true;
2011 $matched_term = get_term_by( 'slug', $category_text, $genoptions['cattaxonomy'] );
2012
2013 if ( $matched_term ) {
2014 $category_list_array[$index] = $matched_term->term_id;
2015 } else {
2016 unset( $category_list_array[$index] );
2017 }
2018 }
2019 }
2020 if ( $update_list ) {
2021 $options['categorylist_cpt'] = implode( ',', $category_list_array );
2022 }
2023 }
2024
2025 if ( !empty( $excludecategoryoverride ) ) {
2026 $options['excludecategorylist_cpt'] = $excludecategoryoverride;
2027
2028 $update_list = false;
2029 $exclude_category_list_array = explode( ',', $excludecategoryoverride );
2030 foreach( $exclude_category_list_array as $index => $category_text ) {
2031 if ( !is_numeric( $category_text ) ) {
2032 $update_list = true;
2033 $matched_term = get_term_by( 'slug', $category_text, $genoptions['cattaxonomy'] );
2034
2035 if ( $matched_term ) {
2036 $exclude_category_list_array[$index] = $matched_term->term_id;
2037 } else {
2038 unset( $exclude_category_list_array[$index] );
2039 }
2040 }
2041 }
2042 if ( $update_list ) {
2043 $options['excludecategorylist_cpt'] = implode( ',', $exclude_category_list_array );
2044 }
2045 }
2046
2047 if ( !empty( $taglistoverride ) ) {
2048 $options['taglist_cpt'] = $taglistoverride;
2049
2050 $update_list = false;
2051 $tag_list_array = explode( ',', $taglistoverride );
2052 foreach( $tag_list_array as $index => $tag_text ) {
2053 if ( !is_numeric( $tag_text ) ) {
2054 $update_list = true;
2055 $matched_term = get_term_by( 'slug', $tag_text, $genoptions['tagtaxonomy'] );
2056
2057 if ( $matched_term ) {
2058 $tag_list_array[$index] = $matched_term->term_id;
2059 } else {
2060 unset( $tag_list_array[$index] );
2061 }
2062 }
2063 }
2064 if ( $update_list ) {
2065 $options['taglist_cpt'] = implode( ',', $tag_list_array );
2066 }
2067 }
2068
2069 if ( !empty( $excludecategoryoverride ) ) {
2070 $options['excludetaglist_cpt'] = $excludetagoverride;
2071
2072 $update_list = false;
2073 $exclude_tag_list_array = explode( ',', $excludetagoverride );
2074 foreach( $exclude_tag_list_array as $index => $tag_text ) {
2075 if ( !is_numeric( $tag_text ) ) {
2076 $update_list = true;
2077 $matched_term = get_term_by( 'slug', $tag_text, $genoptions['tagtaxonomy'] );
2078
2079 if ( $matched_term ) {
2080 $exclude_tag_list_array[$index] = $matched_term->term_id;
2081 } else {
2082 unset( $exclude_tag_list_array[$index] );
2083 }
2084 }
2085 }
2086 if ( $update_list ) {
2087 $options['excludetaglist_cpt'] = implode( ',', $exclude_category_list_array );
2088 }
2089 }
2090
2091 $linklibraryoutput = '';
2092
2093 if ( $genoptions['debugmode'] ) {
2094 $linklibraryoutput .= "\n<!-- RSS Library Settings Info:" . print_r( $options, true ) . "-->\n";
2095 $mainoutputstarttime = microtime( true );
2096 $linklibraryoutput .= "\n<!-- Start Time: " . $mainoutputstarttime . "-->\n";
2097 }
2098
2099 require_once plugin_dir_path( __FILE__ ) . 'render-rss-library-sc.php';
2100 $linkcount = 1;
2101 $rss_array_items = array();
2102 $linklibraryoutput .= RenderRSSLibrary( $this, $genoptions, $options, intval( $settings ), 0, 0, false, $linkcount, $rss_array_items );
2103
2104 if ( $genoptions['debugmode'] ) {
2105 $timeoutput = "\n<!-- [link-library] shortcode execution time: " . ( microtime( true ) - $mainoutputstarttime ) . "-->\n";
2106 }
2107 return $linklibraryoutput . ( true == $genoptions['debugmode'] ? $timeoutput : '' );
2108 }
2109
2110 function link_library_tagcloud( $atts = '' ) {
2111 $genoptions = get_option( 'LinkLibraryGeneral' );
2112 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
2113
2114 $link_library_terms = get_terms( array( 'taxonomy' => $genoptions['cattaxonomy'] ) );
2115 $output = wp_generate_tag_cloud( $link_library_terms );
2116 return $output;
2117 }
2118
2119 function conditionally_add_scripts_and_styles( $posts ) {
2120 if ( empty( $posts ) ) {
2121 return $posts;
2122 }
2123
2124 global $llstylesheet;
2125 global $settingssetsids;
2126 $settingssetsids = array();
2127 $load_jquery = false;
2128 $load_thickbox = false;
2129 $load_colorbox = false;
2130 $load_recaptcha = false;
2131 $load_masonry = false;
2132 $load_style = '';
2133 $has_block = false;
2134
2135 if ( $llstylesheet ) {
2136 $load_style = true;
2137 } else {
2138 $load_style = false;
2139 }
2140
2141 $genoptions = get_option( 'LinkLibraryGeneral' );
2142 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
2143
2144 if ( is_admin() ) {
2145 $load_jquery = false;
2146 $load_thickbox = false;
2147 $load_colorbox = false;
2148 $load_style = false;
2149 } else {
2150 foreach ( $posts as $post ) {
2151 if ( 'link_library_links' != get_post_type( $post->ID ) ) {
2152 $tag_array = array( 'link-library', 'link-library-cats', 'link-library-addlink', 'link-library-search', 'link-library-count', 'rss-library' );
2153 preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches );
2154 if( isset( $matches[2] ) ) {
2155 foreach( ( array ) $matches[2] as $key => $value ) {
2156 $load_style = true;
2157 foreach( $tag_array as $tag ) {
2158 if( $tag === $value ) {
2159 if ( 'link-library-addlink' == $tag ) {
2160 $load_recaptcha = true;
2161 }
2162 $atts_list = shortcode_parse_atts( $matches[3][$key] );
2163 if ( !empty( $atts_list ) ) {
2164 foreach ( $atts_list as $key => $value ) {
2165 if ( $key = 'settings' && ( empty( $settingssetsids ) || false === array_search( $value, $settingssetsids ) ) ) {
2166 $settingssetsids[] = intval( $value );
2167 }
2168 }
2169 } else {
2170 $settingssetsids[] = 1;
2171 }
2172
2173 }
2174 }
2175 }
2176 }
2177
2178 if ( function_exists( 'has_blocks' ) && function_exists( 'parse_blocks' ) ) {
2179 if ( has_blocks( $post->ID ) ) {
2180 $blocks = parse_blocks( $post->post_content );
2181
2182 foreach ( $blocks as $block ) {
2183 if ( in_array( $block['blockName'], array( 'link-library/link-block', 'link-library/cat-block', 'link-library/search-block', 'link-library/addlink-block', 'link-library/count-block' ) ) ) {
2184 $load_style = true;
2185 }
2186
2187 if ( in_array( $block['blockName'], array( 'link-library/addlink-block' ) ) ) {
2188 $load_recaptcha = true;
2189 }
2190
2191 if ( isset( $block['attr']['settings'] ) && false === array_search( $block['attr']['settings'], $settingssetsids ) ) {
2192 $settingssetsids[] = $block['attr']['settings'];
2193 } elseif ( !isset( $block['attr']['settings'] ) ) {
2194 $settingssetsids[] = 1;
2195 }
2196 }
2197 }
2198 }
2199 }
2200 }
2201
2202 if ( empty( $settingssetsids ) ) {
2203 $settingssetsids[] = 1;
2204 }
2205
2206 if ( $settingssetsids ) {
2207 foreach ( $settingssetsids as $settingsetid ) {
2208 $settingsname = 'LinkLibraryPP' . $settingsetid;
2209 $options = get_option( $settingsname );
2210 $options = wp_parse_args( $options, ll_reset_options( 1, 'list', 'return' ) );
2211
2212 if ( $options['showonecatonly'] ) {
2213 $load_jquery = true;
2214 }
2215
2216 if ( 'linkmasonrygrid' == $options['displayastable'] || 'categorymasonrygrid' == $options['displayastable'] ) {
2217 $load_masonry = true;
2218 }
2219
2220 if ( $options['rsspreview'] || ( isset( $options['enable_link_popup'] ) && $options['enable_link_popup'] ) ) {
2221 $load_thickbox = true;
2222 }
2223
2224 if ( $options['publishrssfeed'] == true ) {
2225 global $rss_settings;
2226 $rss_settings = $settingsetid;
2227 }
2228
2229 if ( $options['addlinkformdisplaymode'] ) {
2230 $load_colorbox = true;
2231 }
2232 }
2233 }
2234
2235 if ( !empty( $genoptions['includescriptcss'] ) ) {
2236 $pagelist = explode ( ',', $genoptions['includescriptcss'] );
2237 $loadscripts = false;
2238 foreach( $pagelist as $pageid ) {
2239 if ( ( $pageid == 'front' && is_front_page() ) ||
2240 ( $pageid == 'category' && is_category() ) ||
2241 ( $pageid == 'all') ||
2242 ( is_page( $pageid ) ) ) {
2243 $load_jquery = true;
2244 $load_thickbox = true;
2245 $load_style = true;
2246 }
2247 }
2248 }
2249 }
2250
2251 if ( $load_style ) {
2252 $llstylesheet = true;
2253 } else {
2254 $llstylesheet = false;
2255 }
2256
2257 if ( $load_jquery ) {
2258 wp_enqueue_script( 'jquery' );
2259 }
2260
2261 if ( $load_thickbox ) {
2262 wp_enqueue_script( 'thickbox' );
2263 wp_enqueue_style ( 'thickbox' );
2264 }
2265
2266 if ( $load_colorbox ) {
2267 wp_enqueue_script( 'colorbox', plugins_url( 'colorbox/jquery.colorbox-min.js', __FILE__ ), array( 'jquery' ), "1.3.9" );
2268 wp_enqueue_style( 'colorboxstyle', plugins_url( 'colorbox/colorbox.css', __FILE__ ) );
2269 }
2270
2271 if ( $load_recaptcha && $genoptions['captchagenerator'] ) {
2272 wp_enqueue_script( 'google_recaptcha', 'https://www.google.com/recaptcha/api.js', array(), false, true );
2273 }
2274
2275 if ( $load_masonry ) {
2276 wp_enqueue_script( 'jquery-masonry' );
2277 }
2278
2279 return $posts;
2280 }
2281
2282 function ll_popup_content() {
2283 require_once plugin_dir_path( __FILE__ ) . 'linkpopup.php';
2284 link_library_popup_content( $this );
2285 }
2286
2287 function ll_template_redirect( $template ) {
2288 if ( !empty( $_POST['link_library_user_link_submission'] ) ) {
2289 require_once plugin_dir_path( __FILE__ ) . 'usersubmission.php';
2290 link_library_process_user_submission( $this );
2291 return '';
2292 } elseif ( !empty( $_GET['link_library_rss_preview'] ) ) {
2293 require_once plugin_dir_path( __FILE__ ) . 'rsspreview.php';
2294 link_library_generate_rss_preview( $this );
2295 return '';
2296 } elseif( !empty( $_GET['link_library_css'] ) ) {
2297 require_once plugin_dir_path( __FILE__ ) . 'cssgenerator.php';
2298 link_library_generate_css( $this );
2299 return '';
2300 } elseif ( !empty( $_GET['link_library_popup_content'] ) ) {
2301 require_once plugin_dir_path( __FILE__ ) . 'render-link-library-addlink-sc.php';
2302 link_library_link_submission_popup_form( $this );
2303 exit();
2304 } else {
2305 return $template;
2306 }
2307 }
2308
2309 function ll_template_include( $template_path ) {
2310 if ( get_post_type() == 'link_library_links' && is_single() && !is_admin() ) {
2311 // checks if the file exists in the theme first,
2312 // otherwise serve the file from the plugin
2313 if ( $theme_file = locate_template( array ( 'single-link_library_links.php' ) ) ) {
2314 $template_path = $theme_file;
2315 } else {
2316 add_filter( 'the_content', array( $this, 'll_display_single_link' ), 20 );
2317 }
2318 }
2319 return $template_path;
2320 }
2321
2322 function ll_get_string_between($string, $start, $end){
2323 $string = ' ' . $string;
2324 $ini = strpos($string, $start);
2325 if ($ini == 0) return '';
2326 $ini += strlen($start);
2327 $len = strpos($string, $end, $ini) - $ini;
2328 return substr($string, $ini, $len);
2329 }
2330
2331 function ll_replace_all_between( $beginning, $end, $string, $replace ) {
2332 $beginningPos = strpos($string, $beginning);
2333 $endPos = strpos($string, $end);
2334 if ($beginningPos === false || $endPos === false) {
2335 return $string;
2336 }
2337
2338 $textToDelete = substr($string, $beginningPos, ($endPos + strlen($end)) - $beginningPos);
2339
2340 return str_replace($textToDelete, $replace, $string);
2341 }
2342
2343 function ll_display_single_link( $content ) {
2344
2345 $genoptions = get_option( 'LinkLibraryGeneral' );
2346 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
2347
2348 if ( ( is_search() || is_feed() ) && 'link_library_links' == get_post_type() ) {
2349 $content = htmlspecialchars_decode( stripslashes( $genoptions['global_search_results_layout'] ) );
2350 } elseif ( is_single() && 'link_library_links' == get_post_type() ) {
2351 $content = htmlspecialchars_decode( stripslashes( $genoptions['single_link_layout'] ) );
2352 } else {
2353 return $content;
2354 }
2355
2356 $item_id = get_the_ID();
2357 if ( !empty( $item_id ) ) {
2358 $link = get_post( get_the_ID() );
2359 if ( !empty( $link ) ) {
2360 $link_url = esc_url( get_post_meta( get_the_ID(), 'link_url', true ) );
2361 $link_description = esc_html( get_post_meta( get_the_ID(), 'link_description', true ) );
2362 $link_large_description = get_post_meta( get_the_ID(), 'link_textfield', true );
2363 $link_image = esc_url( get_post_meta( get_the_ID(), 'link_image', true ) );
2364 $link_price = number_format( floatval( get_post_meta( get_the_ID(), 'link_price', true ) ), 2 );
2365 $link_price_currency = $this->ll_get_string_between( $content, '[currency]', '[/currency]' );
2366 $link_price_currency_or_free = $this->ll_get_string_between( $content, '[currency_or_free]', '[/currency_or_free]' );
2367 $link_email = esc_html( get_post_meta( get_the_ID(), 'link_email', true ) );
2368 $link_phone_number = esc_html( get_post_meta( get_the_ID(), 'link_telephone', true ) );
2369
2370 $link_terms = wp_get_post_terms( get_the_ID(), $genoptions['cattaxonomy'] );
2371 $link_terms_list = '';
2372 $link_terms_array = array();
2373 if ( is_array( $link_terms ) ) {
2374 foreach( $link_terms as $link_term ) {
2375 $link_terms_array[] = $link_term->name;
2376 }
2377
2378 if ( !empty( $link_terms_array ) ) {
2379 $link_terms_list = implode( ', ', $link_terms_array );
2380 }
2381 }
2382
2383 if ( false !== strpos( $content, '[if_link_address]' ) && false !== strpos( $content, '[/if_link_address]' ) ) {
2384 if ( empty( $link_url ) ) {
2385 $content = preg_replace( '/\[if_link_address].*\[\/if_link_address\]?/', '', $content );
2386 } else {
2387 $content = str_replace( '[if_link_address]', '', $content );
2388 $content = str_replace( '[/if_link_address]', '', $content );
2389 }
2390 }
2391
2392 if ( false !== strpos( $content, '[if_link_image]' ) && false !== strpos( $content, '[/if_link_image]' ) ) {
2393 if ( empty( $link_image ) ) {
2394 $content = preg_replace( '/\[if_link_image].*\[\/if_link_image\]?/', '', $content );
2395 } else {
2396 $content = str_replace( '[if_link_image]', '', $content );
2397 $content = str_replace( '[/if_link_image]', '', $content );
2398 }
2399 }
2400
2401 $content = str_replace( '[link_title]', $link->post_title, $content );
2402 $content = str_replace( '[link_content]', $link->post_content, $content );
2403 $content = str_replace( '[link_description]', $link_description, $content );
2404 $content = str_replace( '[link_large_description]', $link_large_description, $content );
2405 $content = str_replace( '[link_image]', $link_image, $content );
2406 $content = str_replace( '[link_email]', $link_email, $content );
2407 $content = str_replace( '[link_telephone]', $link_phone_number, $content );
2408
2409 $content = str_replace( '[link_price]', $link_price, $content );
2410 $content = $this->ll_replace_all_between( '[currency]', '[/currency]', $content, $link_price_currency );
2411
2412 if ( floatval( $link_price ) > 0.0 ) {
2413 $content = str_replace( '[link_price_or_free]', $link_price, $content );
2414 $content = $this->ll_replace_all_between( '[currency_or_free]', '[/currency_or_free]', $content, $link_price_currency_or_free );
2415 } else {
2416 $content = str_replace( '[link_price_or_free]', __( 'Free', 'link-library' ), $content );
2417 $content = $this->ll_replace_all_between( '[currency_or_free]', '[/currency_or_free]', $content, '' );
2418 }
2419
2420 $content = str_replace( '[link_address]', $link_url, $content );
2421 $content = str_replace( '[link]', '<a href="' . $link_url . '">' . $link->post_title . '</a>', $content );
2422 $content = str_replace( '[link_category]', $link_terms_list, $content );
2423 }
2424 }
2425
2426 global $wp_embed;
2427 $content = $wp_embed->run_shortcode( $content );
2428
2429 $content = do_shortcode( $content );
2430
2431 //return nl2br( $content );
2432 return $content;
2433 }
2434
2435 function link_library_ajax_tracker() {
2436 require_once plugin_dir_path( __FILE__ ) . 'tracker.php';
2437 link_library_process_ajax_tracker( $this );
2438 }
2439
2440 function link_library_generate_image() {
2441 global $my_link_library_plugin_admin;
2442
2443 if ( empty( $my_link_library_plugin_admin ) ) {
2444 require_once plugin_dir_path( __FILE__ ) . 'link-library-admin.php';
2445 $my_link_library_plugin_admin = new link_library_plugin_admin();
2446 }
2447
2448 require_once plugin_dir_path( __FILE__ ) . 'link-library-image-generator.php';
2449 link_library_ajax_image_generator( $my_link_library_plugin_admin );
2450 }
2451 }
2452
2453 global $my_link_library_plugin;
2454 $my_link_library_plugin = new link_library_plugin();
2455
2456 if ( is_admin() ) {
2457
2458 /* Determine update method selected by user under General Settings or under Network Settings */
2459 $updatechannel = 'standard';
2460
2461 if ( ( function_exists( 'is_multisite' ) && !is_multisite() ) || !function_exists( 'is_multisite' ) ) {
2462 $genoptions = get_option( 'LinkLibraryGeneral' );
2463 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
2464
2465 if ( !empty( $genoptions['updatechannel'] ) ) {
2466 $updatechannel = $genoptions['updatechannel'];
2467 }
2468 } else if ( function_exists( 'is_multisite' ) && function_exists( 'is_network_admin' ) && is_multisite() && is_network_admin() ) {
2469 $networkoptions = get_site_option( 'LinkLibraryNetworkOptions' );
2470
2471 if ( isset( $networkoptions ) && !empty( $networkoptions['updatechannel'] ) ) {
2472 $updatechannel = $networkoptions['updatechannel'];
2473 }
2474 }
2475
2476 /* Install filter is user selected monthly updates to filter out dot dot dot minor releases (e.g. 5.8.8.x) */
2477 if ( 'monthly' == $updatechannel ) {
2478 add_filter( 'http_response', 'link_library_tweak_plugins_http_filter', 10, 3 );
2479 }
2480
2481 if ( empty( $my_link_library_plugin_admin ) ) {
2482 global $my_link_library_plugin_admin;
2483 require plugin_dir_path( __FILE__ ) . 'link-library-admin.php';
2484 $my_link_library_plugin_admin = new link_library_plugin_admin();
2485 }
2486 }
2487
2488 add_action( 'widgets_init', 'll_create_widgets' );
2489
2490 function ll_create_widgets() {
2491 register_widget( 'Link_Library_Widget' );
2492 }
2493
2494 class Link_Library_Widget extends WP_Widget {
2495 // Construction function
2496 function __construct () {
2497 parent::__construct( 'link_library', 'Link Library',
2498 array( 'description' =>
2499 'Displays links as configured under Link Library configurations' ) );
2500 }
2501
2502 function form( $instance ) {
2503 $genoptions = get_option( 'LinkLibraryGeneral' );
2504 $genoptions = wp_parse_args( $genoptions, ll_reset_gen_settings( 'return' ) );
2505
2506 $selected_library = ( !empty( $instance['selected_library'] ) ? $instance['selected_library'] : 1 );
2507 $widget_title = ( !empty( $instance['widget_title'] ) ? esc_attr( $instance['widget_title'] ) : 'Links' );
2508 $category_list_override = ( !empty( $instance['cat_list_override'] ) ? esc_attr( $instance['cat_list_override'] ) : '' );
2509 ?>
2510 <p>
2511 <label for="<?php echo $this->get_field_id( 'widget_title' ); ?>">
2512 <?php echo 'Widget Title:'; ?>
2513 <input type="text"
2514 id="<?php echo $this->get_field_id( 'widget_title' );?>"
2515 name="<?php echo $this->get_field_name( 'widget_title' ); ?>"
2516 value="<?php echo $widget_title; ?>" />
2517 </label>
2518 </p>
2519 <p>
2520 <label for="<?php echo $this->get_field_id( 'selected_library' ); ?>">
2521 <?php echo 'Select library configuration to display:'; ?>
2522 <select id="<?php echo $this->get_field_id( 'selected_library' ); ?>"
2523 name="<?php echo $this->get_field_name( 'selected_library' ); ?>">
2524 <?php if ( empty( $genoptions['numberstylesets'] ) ) {
2525 $numberofsets = 1;
2526 } else {
2527 $numberofsets = $genoptions['numberstylesets'];
2528 }
2529
2530 for ( $counter = 1; $counter <= $numberofsets; $counter ++ ) {
2531 $tempoptionname = "LinkLibraryPP" . $counter;
2532 $tempoptions = get_option( $tempoptionname );
2533 $tempoptions = wp_parse_args( $tempoptions, ll_reset_options( 1, 'list', 'return' ) );
2534
2535 echo '<option value="' . $counter . '" ' . selected( $selected_library, $counter ) . '>' . $tempoptions['settingssetname'] . '</option>';
2536 }
2537 ?>
2538 </select>
2539 </label>
2540 </p>
2541 <p>
2542 <label for="<?php echo $this->get_field_id( 'cat_list_override' ); ?>">
2543 <?php echo 'Comma-separated list of categories to display:'; ?>
2544 <input type="text"
2545 id="<?php echo $this->get_field_id( 'cat_list_override' );?>"
2546 name="<?php echo $this->get_field_name( 'cat_list_override' ); ?>"
2547 value="<?php echo $category_list_override; ?>" />
2548 </label>
2549 </p>
2550 <?php }
2551
2552 function widget( $args, $instance ) {
2553 // Extract members of args array as individual variables
2554 extract( $args );
2555 // Retrieve widget configuration options
2556 $selected_library = ( !empty( $instance['selected_library'] ) ? $instance['selected_library'] : 1 );
2557 $widget_title = ( !empty( $instance['widget_title'] ) ? esc_attr( $instance['widget_title'] ) : 'Links' );
2558 $category_list_override = ( !empty( $instance['cat_list_override'] ) ? esc_attr( $instance['cat_list_override'] ) : '' );
2559
2560 // Display widget title
2561 echo $before_widget . $before_title;
2562 echo apply_filters( 'widget_title', $widget_title );
2563 echo $after_title;
2564
2565 $shortcode_string = '[link-library settings="' . $selected_library . '"';
2566
2567 if ( !empty( $category_list_override ) ) {
2568 $shortcode_string .= ' categorylistoverride="' . $category_list_override . '"';
2569 }
2570
2571 $shortcode_string .= ']';
2572
2573 echo do_shortcode( $shortcode_string );
2574
2575 echo $after_widget;
2576 }
2577 }
2578
2579 if ( ! function_exists('linklibrary_write_log')) {
2580 function linklibrary_write_log ( $log ) {
2581 if ( is_array( $log ) || is_object( $log ) ) {
2582 error_log( print_r( $log, true ) );
2583 } else {
2584 error_log( $log );
2585 }
2586 }
2587 }
2588 }
2589
2590 add_filter('wp_get_object_terms', function($terms, $object_ids, $taxonomies, $args)
2591 {
2592 if ( !$terms && basename( $_SERVER['PHP_SELF'] ) == 'post-new.php' ) {
2593
2594 // Category - note: only 1 category is supported currently
2595 if ( $taxonomies == "'link_library_category'" && isset( $_REQUEST['link_library_cat'] ) ) {
2596 $id = intval( $_REQUEST['link_library_cat'] );
2597
2598 if ( $id ) {
2599 return array( $id );
2600 }
2601 }
2602 }
2603 return $terms;
2604 }, 10, 4);
2605