Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit d1c05b5

Browse files
author
Alexandre Germain
committed
Fixed exception on NULL for get_permalink_light
1 parent d7fc367 commit d1c05b5

File tree

6 files changed

+368
-344
lines changed

6 files changed

+368
-344
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ package-lock.json
66
.DS_Store
77
ithoughts_tt_gl.log
88
grunt_localconfig.json
9+
less/*.css

class/class-filters.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function getTermExcerpt( \WP_Post $term ) {
4747
} else {
4848
$content = wp_trim_words( $term->post_content, 25, '...' );
4949
}
50+
$content = strip_shortcodes($content);
5051
return $content;
5152
}
5253

class/shortcode/class-glossary.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
namespace ithoughts\tooltip_glossary\shortcode;
2121

2222
if ( ! defined( 'ABSPATH' ) ) {
23-
status_header( 403 );wp_die("Forbidden");// Exit if accessed directly
23+
status_header( 403 );wp_die("Forbidden");// Exit if accessed directly
2424
}
2525

2626

@@ -157,10 +157,14 @@ public function ithoughts_tt_gl_get_glossary_term_element( $term, $text = null,
157157
}
158158
} else {
159159
if ( is_numeric( $term ) ) {
160-
$term = get_post( $term );
160+
$termRetrieved = get_post( $term );
161+
if(NULL === $termRetrieved){
162+
$backbone->log( \ithoughts\v5_0\LogLevel::WARN, "Term with id \"$term\" and text \"$text\" does not exists.");
163+
}
164+
$term = $termRetrieved;
161165
} else if ( ! ($term instanceof \WP_Post) ) {
162-
// Error
163-
return $text;
166+
$backbone->log( \ithoughts\v5_0\LogLevel::WARN, "Unhandled term descriptor provided:", $term, gettype($term));
167+
$term = NULL;
164168
}
165169
}
166170

@@ -213,8 +217,12 @@ public function ithoughts_tt_gl_get_glossary_term_element( $term, $text = null,
213217

214218
if ( ! isset( $datas['linkAttrs']['href'] ) ) {
215219
$href = 'javascript::void(0)';
216-
if ( $datas['options']['termlinkopt'] != 'none' ) { // If theere need a link
217-
$href = apply_filters( 'ithoughts_tt_gl_term_link', \ithoughts\v5_0\Toolbox::get_permalink_light( $term,'glossary' ) );
220+
if ( $datas['options']['termlinkopt'] != 'none' ) { // If there need a link
221+
if ( NULL !== $term ) {
222+
$href = apply_filters( 'ithoughts_tt_gl_term_link', \ithoughts\v5_0\Toolbox::get_permalink_light( $term,'glossary' ) );
223+
} else {
224+
$backbone->log( \ithoughts\v5_0\LogLevel::WARN, "Tried to call get_permalink_light on NULL term with text \"$text\".");
225+
}
218226
}
219227
$datas['linkAttrs']['href'] = $href;
220228
}
@@ -231,9 +239,11 @@ public function ithoughts_tt_gl_get_glossary_term_element( $term, $text = null,
231239
$linkElement = '<a ' . $linkArgs . '>' . $text . '</a>';
232240

233241
$datas['attributes']['class'] = 'itg-glossary' . ((isset( $datas['attributes']['class'] ) && $datas['attributes']['class']) ? ' ' . $datas['attributes']['class'] : '');
242+
if(NULL === $term){
243+
$datas['attributes']['class'] .= ' itg-notfound';
244+
}
234245
$args = \ithoughts\v5_0\Toolbox::concat_attrs( $datas['attributes'] );
235246

236-
$backbone = \ithoughts\tooltip_glossary\Backbone::get_instance();
237247
$backbone->add_script( 'qtip' );
238248

239249
return '<span ' . $args . '>' . $linkElement . '</span>';
@@ -283,8 +293,8 @@ public function save_post_check_for_glossary_usage( $post_id, $post ) {
283293
public function glossary_usage_reset_for_post() {
284294
global $post;
285295
if ( is_singular() && get_post_meta( $post->ID, 'ithoughts_tt_gl_update_term_usage' ) ) :
286-
// Find all glossary terms that have this post noted.
287-
$args = array(
296+
// Find all glossary terms that have this post noted.
297+
$args = array(
288298
'post_type' => 'glossary',
289299
'numberposts' => -1,
290300
'post_status' => 'publish',
@@ -293,12 +303,12 @@ public function glossary_usage_reset_for_post() {
293303
'value' => $post->ID,
294304
'type' => 'DECIMAL',
295305
),
296-
),
297-
);
298-
$terms = get_posts( $args );
299-
foreach ( $terms as $term ) :
300-
// Delete the meta entry
301-
delete_post_meta( $term->ID, 'ithoughts_tt_gl_term_used', $post->ID );
306+
),
307+
);
308+
$terms = get_posts( $args );
309+
foreach ( $terms as $term ) :
310+
// Delete the meta entry
311+
delete_post_meta( $term->ID, 'ithoughts_tt_gl_term_used', $post->ID );
302312
endforeach;
303313
endif;
304314
}
@@ -314,9 +324,13 @@ public function glossary_remove_update_marker() {
314324

315325
/** */
316326
public function glossary_shortcode( $atts, $text = '' ) {
327+
/* echo '<hr/>Found shortcode: <pre>';
328+
var_dump(array('atts' => $atts, 'text' => $text));
329+
echo '</pre>';
330+
*/
317331

318332
if ( ! isset( $atts['glossary-id'] ) || ! $atts['glossary-id'] ) {
319-
return $text;
333+
return apply_filters( 'ithoughts_tt_gl_get_glossary_term_element', NULL, $text, $atts );
320334
}
321335
$id = $atts['glossary-id'];
322336
return apply_filters( 'ithoughts_tt_gl_get_glossary_term_element', $id, $text, $atts );

class/shortcode/class-termlist.php

Lines changed: 122 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
namespace ithoughts\tooltip_glossary\shortcode;
2121

2222
if ( ! defined( 'ABSPATH' ) ) {
23-
status_header( 403 );wp_die("Forbidden");// Exit if accessed directly
23+
status_header( 403 );wp_die("Forbidden");// Exit if accessed directly
2424
}
2525

2626
if ( ! class_exists( __NAMESPACE__ . '\\TermList' ) ) {
@@ -68,148 +68,148 @@ public function glossary_term_list( $atts, $content = '' ) {
6868
default:{
6969
$mode = self::LIST_MODE_NONE;
7070
}
71+
}
72+
73+
// Sanity check the list of letters (if set by user).
74+
$alphas = $this->filter_alphas_to_array( isset( $data['handled'] ) && isset( $data['handled']['alpha'] ) && $data['handled']['alpha'] ? $data['handled']['alpha'] : null );
75+
// Checks for partial listing options (on first letter, or group)
76+
$group_ids = $this->filter_groupIds_to_array( isset( $data['handled'] ) && isset( $data['handled']['group'] ) ? $data['handled']['group'] : null );
77+
78+
$terms;
79+
$count;
80+
81+
// Define default desc to none. Will generate a simple link
82+
// If no content is required, use microposts. Else, use normal terms
83+
if ( $mode & self::LIST_MODE_MICROPOST ) {
84+
// Add qtip script only if loading tooltips
85+
if ( $mode === self::LIST_MODE_TIP ) {
86+
\ithoughts\tooltip_glossary\Backbone::get_instance()->add_script( 'qtip' );
7187
}
7288

73-
// Sanity check the list of letters (if set by user).
74-
$alphas = $this->filter_alphas_to_array( isset( $data['handled'] ) && isset( $data['handled']['alpha'] ) && $data['handled']['alpha'] ? $data['handled']['alpha'] : null );
75-
// Checks for partial listing options (on first letter, or group)
76-
$group_ids = $this->filter_groupIds_to_array( isset( $data['handled'] ) && isset( $data['handled']['group'] ) ? $data['handled']['group'] : null );
77-
78-
$terms;
79-
$count;
80-
81-
// Define default desc to none. Will generate a simple link
82-
// If no content is required, use microposts. Else, use normal terms
83-
if ( $mode & self::LIST_MODE_MICROPOST ) {
84-
// Add qtip script only if loading tooltips
85-
if ( $mode === self::LIST_MODE_TIP ) {
86-
\ithoughts\tooltip_glossary\Backbone::get_instance()->add_script( 'qtip' );
87-
}
88-
89-
// Fetch microposts
90-
$termsInfos = $this->get_microposts( $group_ids,$alphas );
91-
} else {
92-
// Fetch full posts
93-
$linkdata = apply_filters( 'ithoughts_tt_gl-split-args', $linkdata );
94-
$termsInfos = $this->get_lists_terms( $group_ids,$alphas );
95-
}
96-
97-
$terms = $this->dispatch_per_char( $termsInfos['terms'], 0, 'WP_Post' );
98-
$count = $termsInfos['count'];
99-
100-
// Go through all glossaries, and restrict to alpha list if supplied.
101-
$lists = array();
102-
$countItems = 0;
103-
foreach ( $terms as $char => $terms_char ) {
104-
if ( count( $alphas ) == 0 || in_array( $char, $alphas ) ) {
105-
$lists[] = "<li class=\"glossary-item-header\">$char</li>";
89+
// Fetch microposts
90+
$termsInfos = $this->get_microposts( $group_ids,$alphas );
91+
} else {
92+
// Fetch full posts
93+
$linkdata = apply_filters( 'ithoughts_tt_gl-split-args', $linkdata );
94+
$termsInfos = $this->get_lists_terms( $group_ids,$alphas );
95+
}
96+
97+
$terms = $this->dispatch_per_char( $termsInfos['terms'], 0, 'WP_Post' );
98+
$count = $termsInfos['count'];
99+
100+
// Go through all glossaries, and restrict to alpha list if supplied.
101+
$lists = array();
102+
$countItems = 0;
103+
foreach ( $terms as $char => $terms_char ) {
104+
if ( count( $alphas ) == 0 || in_array( $char, $alphas ) ) {
105+
$lists[] = "<li class=\"glossary-item-header\">$char</li>";
106+
$countItems++;
107+
108+
foreach ( $terms_char as $term ) {
106109
$countItems++;
107-
108-
foreach ( $terms_char as $term ) {
109-
$countItems++;
110-
$term_standardized_post; // If only microposts were retrieved, cast them in WP_Post (usable by standard methods)
111-
if ( $mode & self::LIST_MODE_MICROPOST ) {
112-
$term_standardized_post = $term->to_WP_Post();
113-
} else {
114-
$term_standardized_post = &$term;
115-
}
116-
$linkAttrs = (isset( $linkdata['linkAttrs'] ) && is_array( $linkdata['linkAttrs'] )) ? $linkdata['linkAttrs'] : $linkdata;
117-
$linkAttrs['title'] = esc_attr( $term_standardized_post->post_title );
118-
$linkAttrs['alt'] = esc_attr( $term_standardized_post->post_title );
119-
120-
$link = '';
121-
$content = '';
122-
switch ( $mode ) {
123-
case self::LIST_MODE_NONE:{
124-
$href = apply_filters( 'ithoughts_tt_gl_term_link', \ithoughts\v5_0\Toolbox::get_permalink_light( $term_standardized_post, 'glossary' ) );
125-
$target = '';
126-
if ( $data['options']['termlinkopt'] != 'none' ) {
127-
$linkAttrs['target'] = '_blank';
128-
}
129-
$linkAttrs['href'] = &$href;
130-
$args = \ithoughts\v5_0\Toolbox::concat_attrs( $linkAttrs );
131-
$link = '<a ' . $args . '>' . $term_standardized_post->post_title . '</a>';
132-
}break;
133-
134-
case self::LIST_MODE_TIP:{
135-
$link = apply_filters( 'ithoughts_tt_gl_get_glossary_term_element', $term_standardized_post, null, $linkdata );
136-
} break;
137-
138-
case self::LIST_MODE_EXCERPT:{
139-
$href = apply_filters( 'ithoughts_tt_gl_term_link', \ithoughts\v5_0\Toolbox::get_permalink_light( $term_standardized_post, 'glossary' ) );
140-
$target = '';
141-
if ( $data['options']['termlinkopt'] != 'none' ) {
142-
$linkAttrs['target'] = '_blank';
143-
}
144-
$linkAttrs['href'] = &$href;
145-
$args = \ithoughts\v5_0\Toolbox::concat_attrs( $linkAttrs );
146-
$link = '<a ' . $args . '>' . $term_standardized_post->post_title . '</a>';
147-
$content = '<br>' . '<span class="glossary-item-desc">' . apply_filters( 'ithoughts_tt_gl-term-excerpt', $term_standardized_post ) . '</span>';
148-
} break;
149-
150-
case self::LIST_MODE_FULL:{
151-
$href = apply_filters( 'ithoughts_tt_gl_term_link', \ithoughts\v5_0\Toolbox::get_permalink_light( $term_standardized_post, 'glossary' ) );
152-
$target = '';
153-
if ( $data['options']['termlinkopt'] != 'none' ) {
154-
$linkAttrs['target'] = '_blank';
155-
}
156-
$linkAttrs['href'] = &$href;
157-
$args = \ithoughts\v5_0\Toolbox::concat_attrs( $linkAttrs );
158-
$link = '<a ' . $args . '>' . $term_standardized_post->post_title . '</a>';
159-
$content = '<br>' . '<span class="glossary-item-desc">' . $term->post_content . '</span>';
160-
} break;
161-
}// End switch().
162-
163-
$lists[] = "<li class=\"glossary-item\">$link$content</li>";
164-
}// End foreach().
165-
}// End if().
166-
}// End foreach().
167-
if ( ! isset( $data['handled']['cols'] ) || $data['handled']['cols'] == 0 || $data['handled']['cols'] === false ) {
168-
$data['handled']['cols'] = 1; // set col size to all items
110+
$term_standardized_post; // If only microposts were retrieved, cast them in WP_Post (usable by standard methods)
111+
if ( $mode & self::LIST_MODE_MICROPOST ) {
112+
$term_standardized_post = $term->to_WP_Post();
113+
} else {
114+
$term_standardized_post = &$term;
115+
}
116+
$linkAttrs = (isset( $linkdata['linkAttrs'] ) && is_array( $linkdata['linkAttrs'] )) ? $linkdata['linkAttrs'] : $linkdata;
117+
$linkAttrs['title'] = esc_attr( $term_standardized_post->post_title );
118+
$linkAttrs['alt'] = esc_attr( $term_standardized_post->post_title );
119+
120+
$link = '';
121+
$content = '';
122+
switch ( $mode ) {
123+
case self::LIST_MODE_NONE:{
124+
$href = apply_filters( 'ithoughts_tt_gl_term_link', \ithoughts\v5_0\Toolbox::get_permalink_light( $term_standardized_post, 'glossary' ) );
125+
$target = '';
126+
if ( $data['options']['termlinkopt'] != 'none' ) {
127+
$linkAttrs['target'] = '_blank';
128+
}
129+
$linkAttrs['href'] = &$href;
130+
$args = \ithoughts\v5_0\Toolbox::concat_attrs( $linkAttrs );
131+
$link = '<a ' . $args . '>' . $term_standardized_post->post_title . '</a>';
132+
}break;
133+
134+
case self::LIST_MODE_TIP:{
135+
$link = apply_filters( 'ithoughts_tt_gl_get_glossary_term_element', $term_standardized_post, null, $linkdata );
136+
} break;
137+
138+
case self::LIST_MODE_EXCERPT:{
139+
$href = apply_filters( 'ithoughts_tt_gl_term_link', \ithoughts\v5_0\Toolbox::get_permalink_light( $term_standardized_post, 'glossary' ) );
140+
$target = '';
141+
if ( $data['options']['termlinkopt'] != 'none' ) {
142+
$linkAttrs['target'] = '_blank';
143+
}
144+
$linkAttrs['href'] = &$href;
145+
$args = \ithoughts\v5_0\Toolbox::concat_attrs( $linkAttrs );
146+
$link = '<a ' . $args . '>' . $term_standardized_post->post_title . '</a>';
147+
$content = '<br>' . '<span class="glossary-item-desc">' . apply_filters( 'ithoughts_tt_gl-term-excerpt', $term_standardized_post ) . '</span>';
148+
} break;
149+
150+
case self::LIST_MODE_FULL:{
151+
$href = apply_filters( 'ithoughts_tt_gl_term_link', \ithoughts\v5_0\Toolbox::get_permalink_light( $term_standardized_post, 'glossary' ) );
152+
$target = '';
153+
if ( $data['options']['termlinkopt'] != 'none' ) {
154+
$linkAttrs['target'] = '_blank';
155+
}
156+
$linkAttrs['href'] = &$href;
157+
$args = \ithoughts\v5_0\Toolbox::concat_attrs( $linkAttrs );
158+
$link = '<a ' . $args . '>' . $term_standardized_post->post_title . '</a>';
159+
$content = '<br>' . '<span class="glossary-item-desc">' . do_shortcode($term->post_content) . '</span>';
160+
} break;
161+
}// End switch().
162+
163+
$lists[] = "<li class=\"glossary-item\">$link$content</li>";
164+
}// End foreach().
165+
}// End if().
166+
}// End foreach().
167+
if ( ! isset( $data['handled']['cols'] ) || $data['handled']['cols'] == 0 || $data['handled']['cols'] === false ) {
168+
$data['handled']['cols'] = 1; // set col size to all items
169+
}
170+
$chunked;
171+
if ( $data['handled']['cols'] != 1 ) {
172+
$termsPerChunkFloat = $countItems / $data['handled']['cols'];
173+
$termsPerChunk = intval( $termsPerChunkFloat );
174+
if ( $termsPerChunkFloat != $termsPerChunk ) {
175+
$termsPerChunk++;
169176
}
170-
$chunked;
171-
if ( $data['handled']['cols'] != 1 ) {
172-
$termsPerChunkFloat = $countItems / $data['handled']['cols'];
173-
$termsPerChunk = intval( $termsPerChunkFloat );
174-
if ( $termsPerChunkFloat != $termsPerChunk ) {
175-
$termsPerChunk++;
176-
}
177177

178-
if ( $termsPerChunk < 1 ) {
179-
$termsPerChunk = 1;
180-
}
181-
$chunked = array_chunk( $lists, $termsPerChunk );
182-
} else {
183-
$chunked = array( &$lists );
178+
if ( $termsPerChunk < 1 ) {
179+
$termsPerChunk = 1;
184180
}
181+
$chunked = array_chunk( $lists, $termsPerChunk );
182+
} else {
183+
$chunked = array( &$lists );
184+
}
185185

186-
$data['attributes']['class'] = 'glossary-list-details' . ((isset( $data['attributes']['class'] ) && $data['attributes']['class']) ? ' ' . $data['attributes']['class'] : '');
187-
/*
186+
$data['attributes']['class'] = 'glossary-list-details' . ((isset( $data['attributes']['class'] ) && $data['attributes']['class']) ? ' ' . $data['attributes']['class'] : '');
187+
/*
188188
if(isset($data["handled"]["masonry"])){
189189
$data["attributes"]["class"] .= " masonry";
190190
$data["attributes"]["class"] = trim($data["attributes"]["class"]);
191191
$data["attributes"]["data-cols"] = $data["handled"]["cols"];
192192
}*/
193-
$args = \ithoughts\v5_0\Toolbox::concat_attrs( $data['attributes'] );
193+
$args = \ithoughts\v5_0\Toolbox::concat_attrs( $data['attributes'] );
194194

195-
$return = '<div ' . $args . '>';
196-
if ( isset( $data['handled']['masonry'] ) ) {/*
195+
$return = '<div ' . $args . '>';
196+
if ( isset( $data['handled']['masonry'] ) ) {/*
197197
\ithoughts\tooltip_glossary\Backbone::get_instance()->add_script('list');
198198
$return .= '<ul class="glossary-list">';
199199
foreach( $chunked as $col => $items ){
200200
$return .= implode( '', $items );
201201
}
202202
$return .= '</ul>';*/
203-
} else {
204-
foreach ( $chunked as $col => $items ) {
205-
$return .= '<ul class="glossary-list">';
206-
$return .= implode( '', $items );
207-
$return .= '</ul>';
208-
}
203+
} else {
204+
foreach ( $chunked as $col => $items ) {
205+
$return .= '<ul class="glossary-list">';
206+
$return .= implode( '', $items );
207+
$return .= '</ul>';
209208
}
210-
$return .= '</div>';
209+
}
210+
$return .= '</div>';
211211

212-
return $return;
212+
return $return;
213213
} // glossary_term_list
214214
} // termlist
215215
}// End if().

0 commit comments

Comments
 (0)