-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-page-break.php
177 lines (147 loc) · 4.53 KB
/
auto-page-break.php
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
class NeonAutoPageBreak {
private $page_break = '<!--nextpage-->';
private $page_break_at_paragraph = 3;
public function __construct() {
add_action('customize_register', [$this, 'pageBreakOption']);
add_action('the_posts', [$this, 'autoPageBreak'], 10, 2);
add_filter('the_content', [$this, 'pageBreakPagination']);
add_filter('wp_link_pages_args', [$this, 'paginationConfig']);
add_action('wp_head', [$this, 'pageBreakPaginationStyle']);
}
public function pageBreakOption($wp_customize) {
$wp_customize->add_section('page_break_option', array(
'title' => 'PageBreak Option',
'description' => 'Setting Page Break',
'priority' => 160,
));
$wp_customize->add_setting('use_auto_page_break', array(
'default' => true,
'sanitize_callback' => 'sanitize_text_field',
));
$wp_customize->add_control('use_auto_page_break', array(
'label' => 'Gunakan AutoPageBreak',
'section' => 'page_break_option',
'type' => 'checkbox',
));
$wp_customize->add_setting('page_break_at_paragraph', array(
'default' => $this->page_break_at_paragraph,
'sanitize_callback' => 'sanitize_text_field',
));
$wp_customize->add_control('page_break_at_paragraph', array(
'label' => 'Break Pada Paragraf Ke',
'section' => 'page_break_option',
'type' => 'number',
'input_attrs' => array(
'min' => 3,
'step' => 1,
),
'active_callback' => function() {
return get_theme_mod('use_auto_page_break', false);
},
));
}
public function autoPageBreak($posts, $query) {
if ($query->is_main_query() &&
($query->is_single() || $query->is_page()) &&
$this->autoPageBreakIsEnabled()
) {
$break_at_paragraph = get_theme_mod('page_break_at_paragraph', $this->page_break_at_paragraph);
foreach ($posts as $post) {
$post->post_content = $this->setPageBreak($post->post_content, $break_at_paragraph);
}
}
return $posts;
}
public function paginationConfig() {
return $this->paginationArgs();
}
public function pageBreakPagination($content) {
global $multipage, $post;
;
if ( ! empty($_GET['show']) && $_GET['show'] == 'all' ) {
return $post->post_content;
}
if (
$multipage &&
$this->autoPageBreakIsEnabled() &&
! str_contains( $content, '<div class="neon-auto-page-break-pagination">' )
) {
$content .= wp_link_pages($this->paginationArgs());
}
return $content;
}
public function pageBreakPaginationStyle() {
?>
<style>
.neon-auto-page-break-pagination {
display: flex;
flex-wrap: wrap;
}
.neon-auto-page-break-pagination div {
flex:1 1 50%;
}
.neon-auto-page-break-pagination a {
text-decoration:none;
}
.neon-auto-page-break-pagination .neon-show-all-page-content {
text-align:right;
}
.neon-auto-page-break-pagination .current {
font-weight: bold;
}
@media (max-width: 500px) {
.neon-auto-page-break-pagination div {
flex-basis: 100%;
text-align:center;
}
}
</style>
<?php
}
protected function paginationArgs() {
global $post;
$before = '<div class="neon-auto-page-break-pagination"><div class="pagination"><span class="prefix">Halaman: </span>';
$link_show_all = add_query_arg([
'show'=> 'all'
], get_permalink($post));
$after = '</div><div class="neon-show-all-page-content"><a href="'. $link_show_all .'">Show All</a></div>';
return [
'before' => $before,
'after' => $after,
'link_before' => '<span class="page-number">',
'link_after' => '</span>',
'next_or_number' => 'number',
'separator' => ' ',
'aria_current' => 'page',
'nextpagelink'=> __( 'Next page' ),
'previouspagelink'=> __( 'Previous page' ),
'pagelink' => '%',
'echo' => false
];
}
protected function setPageBreak($post_content, $break_at_paragraph = 0) {
if ( empty($break_at_paragraph) ) {
return $post_content;
}
if ( str_contains( $post_content, $this->page_break ) ) {
return $post_content;
}
$paragraphs = explode('</p>', $post_content);
$n = 0;
$_content = '';
foreach ($paragraphs as $paragraph) {
$_content .= $paragraph . '</p>';
if ( $n == $break_at_paragraph ) {
$_content .= $this->page_break;
$n = 0;
}
$n++;
}
return $_content;
}
protected function autoPageBreakIsEnabled() {
return get_theme_mod('use_auto_page_break', true);
}
}
new NeonAutoPageBreak();