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