PluginProbe
Code Snippets / 4.0.0-beta.1
Code Snippets v4.0.0-beta.1
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / php / cloud / list-table-shared-ops.php

list-table-shared-ops.php in Code Snippets 4.0.0-beta.1, at php/cloud/list-table-shared-ops.php

288 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Functions to perform snippet operations
4 *
5 * @package Code_Snippets
6 */
7
8 namespace Code_Snippets\Cloud;
9
10 use function Code_Snippets\code_snippets;
11
12 /**
13 * Build the nonce action string for cloud snippet state-changing operations.
14 *
15 * @param string $action Action - 'download' or 'update'.
16 * @param int $snippet_id Cloud snippet ID.
17 * @param string $source Source - 'search' or 'cloud'.
18 *
19 * @return string
20 */
21 function cloud_lts_get_snippet_action_nonce_action( string $action, int $snippet_id, string $source ): string {
22 return sprintf( 'cloud-snippet-action|%s|%s|%d', $action, $source, $snippet_id );
23 }
24
25 /**
26 * Display a hidden input field for a certain column and snippet value.
27 *
28 * @param string $column_name Column name.
29 * @param Cloud_Snippet $snippet Column item.
30 *
31 * @return void
32 */
33 function cloud_lts_display_column_hidden_input( string $column_name, Cloud_Snippet $snippet ) {
34 printf(
35 '<input id="cloud-snippet-%s-%s" class="cloud-snippet-item" type="hidden" name="%s" value="%s" />',
36 esc_attr( $column_name ),
37 esc_attr( $snippet->id ),
38 esc_attr( $column_name ),
39 esc_attr( $snippet->$column_name )
40 );
41 }
42
43 /**
44 * Display a hidden input field for a certain column and snippet value.
45 *
46 * @param string $column_name Column name.
47 * @param Cloud_Snippet $snippet Column item.
48 *
49 * @return string HTML
50 */
51 function cloud_lts_build_column_hidden_input( string $column_name, Cloud_Snippet $snippet ): string {
52 return sprintf(
53 '<input id="cloud-snippet-%s-%s" class="cloud-snippet-item" type="hidden" name="%s" value="%s" />',
54 esc_attr( $column_name ),
55 esc_attr( $snippet->id ),
56 esc_attr( $column_name ),
57 esc_attr( $snippet->$column_name )
58 );
59 }
60
61 /**
62 * Process the download snippet action
63 *
64 * @param string $action Action - 'download' or 'update'.
65 * @param string $source Source - 'search' or 'cloud'.
66 * @param string $snippet Snippet ID.
67 *
68 * @return void
69 */
70 function cloud_lts_process_download_action( string $action, string $source, string $snippet ) {
71 if ( 'download' === $action || 'update' === $action ) {
72 $result = code_snippets()->cloud_api->download_or_update_snippet( $snippet, $source, $action );
73
74 if ( $result['success'] ) {
75 $redirect_uri = $result['snippet_id'] ?
76 code_snippets()->get_snippet_edit_url( (int) $result['snippet_id'] ) :
77 add_query_arg( 'result', $result['action'] );
78
79 wp_safe_redirect( esc_url_raw( $redirect_uri ) );
80 exit;
81 }
82 }
83 }
84
85 /**
86 * Build action links for snippet.
87 *
88 * @param Cloud_Snippet $cloud_snippet Snippet/Column item.
89 * @param string $source Source - 'search' or 'codevault'.
90 *
91 * @return string Action link HTML.
92 */
93 function cloud_lts_build_action_links( Cloud_Snippet $cloud_snippet, string $source ): string {
94 $lang = Cloud_API::get_type_from_scope( $cloud_snippet->scope );
95 $link = code_snippets()->cloud_api->get_link_for_cloud_snippet( $cloud_snippet );
96 $is_licensed = code_snippets()->licensing->is_licensed();
97 $download = $is_licensed || ! in_array( $lang, [ 'css', 'js' ], true );
98 $snippet_id = (int) $cloud_snippet->id;
99
100 if ( $link ) {
101 if ( $is_licensed && $link->update_available ) {
102 $update_url = wp_nonce_url(
103 add_query_arg(
104 [
105 'action' => 'update',
106 'snippet' => $snippet_id,
107 'source' => $source,
108 ]
109 ),
110 cloud_lts_get_snippet_action_nonce_action( 'update', $snippet_id, $source )
111 );
112 return sprintf(
113 '<li><a class="button button-primary" href="%s">%s</a></li>',
114 esc_url( $update_url ),
115 esc_html__( 'Update Available', 'code-snippets' )
116 );
117 } else {
118 return sprintf(
119 '<li><a class="button" href="%s">%s</a></li>',
120 esc_url( code_snippets()->get_snippet_edit_url( $link->local_id ) ),
121 esc_html__( 'View', 'code-snippets' )
122 );
123 }
124 }
125
126 if ( $download ) {
127 $download_query = [
128 'action' => 'download',
129 'snippet' => $snippet_id,
130 'source' => $source,
131 ];
132
133 // Preserve current cloud page if present so downstream handlers receive pagination context.
134 if ( isset( $_REQUEST['cloud_page'] ) ) {
135 $download_query['cloud_page'] = (int) wp_unslash( $_REQUEST['cloud_page'] );
136 }
137
138 $download_url = wp_nonce_url(
139 add_query_arg( $download_query ),
140 cloud_lts_get_snippet_action_nonce_action( 'download', $snippet_id, $source )
141 );
142
143 $download_button = sprintf(
144 '<li><a class="button button-primary" href="%s">%s</a></li>',
145 esc_url( $download_url ),
146 esc_html__( 'Download', 'code-snippets' )
147 );
148 } else {
149 $download_button = sprintf(
150 '<li><span class="%s">%s <span class="tooltip-content">%s</span></span></li>',
151 'button button-primary button-disabled tooltip tooltip-block tooltip-end',
152 esc_html__( 'Download', 'code-snippets' ),
153 esc_html__( 'This snippet type is only available in Code Snippets Pro', 'code-snippets' )
154 );
155 }
156
157 $preview_button = sprintf(
158 '<li><a href="%s" aria-label="%s" class="%s" data-snippet="%s" data-lang="%s">%s</a></li>',
159 '#TB_inline?&width=700&height=500&inlineId=show-code-preview',
160 esc_attr( $cloud_snippet->name ),
161 'cloud-snippet-preview thickbox button',
162 esc_attr( $cloud_snippet->id ),
163 esc_attr( $lang ),
164 esc_html__( 'Preview', 'code-snippets' )
165 );
166
167 return $download_button . $preview_button;
168 }
169
170 /**
171 * Build the pagination functionality
172 *
173 * @param string $which Context where the pagination will be displayed.
174 * @param string $source Source - 'search' or 'cloud'.
175 * @param int $total_items Total number of items.
176 * @param int $total_pages Total number of pages.
177 * @param int $pagenum Current page number.
178 *
179 * @return array
180 */
181 function cloud_lts_pagination( string $which, string $source, int $total_items, int $total_pages, int $pagenum ): array {
182 /* translators: %s: Number of items. */
183 $num = sprintf( _n( '%s item', '%s items', $total_items, 'code-snippets' ), number_format_i18n( $total_items ) );
184 $output = '<span class="displaying-num">' . $num . '</span>';
185
186 $param_key = $source . '_page';
187 $current = isset( $_REQUEST[ $param_key ] ) ? (int) $_REQUEST[ $param_key ] : $pagenum;
188 $current_url = remove_query_arg( wp_removable_query_args() ) . '#' . $source;
189
190 $page_links = array();
191
192 $html_current_page = '';
193 $total_pages_before = '<span class="paging-input">';
194 $total_pages_after = '</span></span>';
195
196 $disable_first = false;
197 $disable_last = false;
198 $disable_prev = false;
199 $disable_next = false;
200
201 if ( 1 === $current ) {
202 $disable_first = true;
203 $disable_prev = true;
204 }
205
206 if ( $total_pages === $current ) {
207 $disable_last = true;
208 $disable_next = true;
209 }
210
211 if ( $disable_first ) {
212 $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&laquo;</span>';
213 } else {
214 $page_links[] = sprintf(
215 '<a class="first-page button" href="%s"><span class="screen-reader-text">%s</span><span aria-hidden="true">&laquo;</span></a>',
216 esc_url( remove_query_arg( $source . '_page', $current_url ) ),
217 esc_html__( 'First page', 'code-snippets' )
218 );
219 }
220
221 if ( $disable_prev ) {
222 $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&lsaquo;</span>';
223 } else {
224 $page_links[] = sprintf(
225 '<a class="prev-page button" href="%s"><span class="screen-reader-text">%s</span><span aria-hidden="true">&lsaquo;</span></a>',
226 esc_url( add_query_arg( $source . '_page', max( 1, $current - 1 ), $current_url ) ),
227 esc_html__( 'Previous page', 'code-snippets' )
228 );
229 }
230
231 if ( 'bottom' === $which ) {
232 $html_current_page = $current;
233 $total_pages_before = sprintf( '<span class="screen-reader-text">%s</span><span id="table-paging" class="paging-input"><span class="tablenav-paging-text">', __( 'Current page', 'code-snippets' ) );
234 }
235
236 if ( 'top' === $which ) {
237 $html_current_page = sprintf(
238 '<label for="current-page-selector" class="screen-reader-text">%s</label><input class="current-page-selector" id="current-page-selector" type="text" name="%s_page" value="%s" size="%d" aria-describedby="table-paging" /><span class="tablenav-paging-text">',
239 __( 'Current page', 'code-snippets' ),
240 $source,
241 $current,
242 strlen( $total_pages )
243 );
244 }
245
246 $html_total_pages = sprintf( '<span class="total-pages">%s</span>', number_format_i18n( $total_pages ) );
247
248 /* translators: 1: Current page, 2: Total pages. */
249 $current_html = _x( '%1$s of %2$s', 'paging', 'code-snippets' );
250 $page_links[] = $total_pages_before . sprintf( $current_html, $html_current_page, $html_total_pages ) . $total_pages_after;
251
252 if ( $disable_next ) {
253 $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&rsaquo;</span>';
254 } else {
255 $page_links[] = sprintf(
256 '<a class="next-page button" href="%s"><span class="screen-reader-text">%s</span><span aria-hidden="true">%s</span></a>',
257 esc_url( add_query_arg( $source . '_page', min( $total_pages, $current + 1 ), $current_url ) ),
258 esc_html__( 'Next page', 'code-snippets' ),
259 '&rsaquo;'
260 );
261 }
262
263 if ( $disable_last ) {
264 $page_links[] = '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&raquo;</span>';
265 } else {
266 $page_links[] = sprintf(
267 '<a class="last-page button" href="%s"><span class="screen-reader-text">%s</span><span aria-hidden="true">%s</span></a>',
268 esc_url( add_query_arg( $source . '_page', $total_pages, $current_url ) ),
269 esc_html__( 'Last page', 'code-snippets' ),
270 '&raquo;'
271 );
272 }
273
274 $pagination_links_class = 'pagination-links';
275 if ( ! empty( $infinite_scroll ) ) {
276 $pagination_links_class .= ' hide-if-js';
277 }
278
279 $output .= "\n<span class='$pagination_links_class'>" . implode( "\n", $page_links ) . '</span>';
280
281 $page_class = $total_pages ? '' : ' no-pages';
282
283 return [
284 'output' => $output,
285 'page_class' => $page_class,
286 ];
287 }
288