-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathsimpletoc-class-headline-ids.php
More file actions
81 lines (73 loc) · 1.94 KB
/
simpletoc-class-headline-ids.php
File metadata and controls
81 lines (73 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
* Class to manage headline IDs.
*
* Ensures a unique anchor for each headline.
*
* @package simpletoc
*/
namespace MToensing\SimpleTOC;
/**
* Class to manage headline IDs.
*
* Ensures a unique anchor for each headline.
*/
class SimpleTOC_Headline_Ids {
/**
* Array of headlines and their counts.
*
* @var array
*/
private $headlines = array();
/**
* Add a headline to the array
*
* @param string $headline_slug The slug of the headline.
*/
private function add_headline( $headline_slug ) {
if ( empty( $headline_slug ) ) {
return;
}
if ( ! isset( $this->headlines[ $headline_slug ] ) ) {
$this->headlines[ $headline_slug ] = 1;
} else {
$this->headlines[ $headline_slug ] = $this->get_headline_count( $headline_slug ) + 1;
}
if ( $this->headlines[ $headline_slug ] > 1 ) {
$new_headline_slug = $headline_slug . '-' . $this->headlines[ $headline_slug ];
if ( isset( $this->headlines[ $new_headline_slug ] ) ) {
$new_headline_slug = $this->add_headline( $new_headline_slug );
}
$new_headline_count = $this->get_headline_count( $new_headline_slug );
if ( 0 === $new_headline_count ) {
$new_headline_count = 1;
}
$this->headlines[ $new_headline_slug ] = $new_headline_count;
return $new_headline_slug;
}
return $headline_slug;
}
/**
* Get the anchor for a headline
*
* @param string $headline The headline.
* @return string The anchor for the headline
*/
public function get_headline_anchor( $headline ) {
if ( empty( $headline ) ) {
return '';
}
$headline_slug = simpletoc_sanitize_string( $headline );
$headline_slug = $this->add_headline( $headline_slug );
return $headline_slug;
}
/**
* Get the count of a headline slug.
*
* @param string $headline_slug The slug of the headline.
* @return mixed The count of the headline slug.
*/
private function get_headline_count( $headline_slug = '' ) {
return $this->headlines[ $headline_slug ] ?? 0;
}
}