PluginProbe
Code Snippets / 3.7.0
Code Snippets v3.7.0
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 3.0.1 All 64 releases
code-snippets / php / cloud / list-table-shared-ops.php

list-table-shared-ops.php in Code Snippets 3.7.0, at php/cloud/list-table-shared-ops.php

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