FapiLevels.php
2 years ago
FapiMemberTools.php
2 years ago
FapiMembership.php
2 years ago
FapiTermEnvelope.php
2 years ago
FapiMemberTools.php
958 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FapiMember\Deprecated; |
| 4 | |
| 5 | use FapiMember\Model\Enums\Keys\MetaKey; |
| 6 | use FapiMember\Model\Enums\Keys\OptionKey; |
| 7 | use FapiMember\Model\Enums\Keys\SettingsKey; |
| 8 | use FapiMember\Utils\PostTypeHelper; |
| 9 | use WP_User; |
| 10 | use function get_posts; |
| 11 | use function htmlentities; |
| 12 | use function in_array; |
| 13 | use function sprintf; |
| 14 | |
| 15 | /** @deprecated */ |
| 16 | final class FapiMemberTools { |
| 17 | |
| 18 | /** |
| 19 | * @param string $subpage |
| 20 | * @param string $label |
| 21 | * @param string $activeSubpage |
| 22 | * @return string |
| 23 | */ |
| 24 | public static function subSubmenuItem( $subpage, $label, $activeSubpage ) { |
| 25 | $classes = array( 'subsubmenuitem' ); |
| 26 | |
| 27 | if ( $activeSubpage === $subpage ) { |
| 28 | $classes[] = 'active'; |
| 29 | } |
| 30 | |
| 31 | $level = ( isset( $_GET['level'] ) ) ? self::sanitizeLevelId( $_GET['level'] ) : null; |
| 32 | |
| 33 | if ( $level ) { |
| 34 | $tail = sprintf( '&level=%s', $level ); |
| 35 | } else { |
| 36 | $tail = ''; |
| 37 | } |
| 38 | |
| 39 | return sprintf( |
| 40 | '<a href="%s%s" class="%s">%s</a>', |
| 41 | self::fapilink( $subpage ), |
| 42 | $tail, |
| 43 | implode( ' ', $classes ), |
| 44 | $label |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @param int $levelId |
| 50 | * @return int|null |
| 51 | */ |
| 52 | public static function sanitizeLevelId( $levelId ) { |
| 53 | global $FapiPlugin; |
| 54 | if ( ! is_numeric( $levelId ) ) { |
| 55 | return null; |
| 56 | } |
| 57 | $t = $FapiPlugin->levels()->loadById( $levelId ); |
| 58 | if ( ! $t ) { |
| 59 | return null; |
| 60 | } |
| 61 | |
| 62 | return (int) $levelId; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @param string $subpage |
| 67 | * @return string |
| 68 | */ |
| 69 | public static function fapilink( $subpage ) { |
| 70 | return admin_url( sprintf( '/admin.php?page=fapi-member-options&subpage=%s', $subpage ) ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @return string |
| 75 | */ |
| 76 | public static function help() { |
| 77 | return ' |
| 78 | <div class="help"> |
| 79 | <h3>Nápověda</h3> |
| 80 | <div class="inner"> |
| 81 | <div> |
| 82 | <h4>Jak propojit plugin s FAPI?</h4> |
| 83 | <p>Prvním krokem ke zprovoznění členských sekcí je propojení pluginu s vaším účtem FAPI.</p> |
| 84 | <a href="https://napoveda.fapi.cz/article/97-fapi-member-propojeni-s-fapi" target="_blank" class="btn outline">Přečíst</a> |
| 85 | </div> |
| 86 | <div> |
| 87 | <h4>Jak vytvořit členskou sekci?</h4> |
| 88 | <p>Zde se dozvíte, co je to členská sekce nebo úroveň a jak ji správně nastavit.</p> |
| 89 | <a href="https://napoveda.fapi.cz/article/98-fapi-member-nastaveni-clenske-sekce" target="_blank" class="btn outline">Přečíst</a> |
| 90 | </div> |
| 91 | <div> |
| 92 | <h4>Jak přidat uživatele do členské sekce?</h4> |
| 93 | <p>Zjistěte, jak nastavit prodejní formulář FAPI, aby automaticky zakládal členství vašim klientům.</p> |
| 94 | <a href="https://napoveda.fapi.cz/article/99-fapi-member-zakladani-clenstvi" target="_blank" class="btn outline">Přečíst</a> |
| 95 | </div> |
| 96 | </div> |
| 97 | </div> |
| 98 | '; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @return void |
| 103 | */ |
| 104 | public static function levels() { |
| 105 | global $FapiPlugin; |
| 106 | $envelopes = $FapiPlugin->levels()->loadAsTermEnvelopes(); |
| 107 | |
| 108 | $lis = array(); |
| 109 | $actions = '<button class="edit"></button><button class="remove"></button><button class="up"></button><button class="down"></button>'; |
| 110 | |
| 111 | foreach ( $envelopes as $envelope ) { |
| 112 | $term = $envelope->getTerm(); |
| 113 | $under = array(); |
| 114 | if ( $term->parent === 0 ) { |
| 115 | foreach ( $envelopes as $underEnvelope ) { |
| 116 | $underTerm = $underEnvelope->getTerm(); |
| 117 | if ( $underTerm->parent === $term->term_id ) { |
| 118 | $under[] = sprintf( |
| 119 | '<li data-id="%s" data-name="%s"><span title="%s">%s</span>%s</li>', |
| 120 | $underTerm->term_id, |
| 121 | htmlentities( $underTerm->name ), |
| 122 | $underTerm->term_id, |
| 123 | self::trimName( $underTerm->name ), |
| 124 | $actions |
| 125 | ); |
| 126 | } |
| 127 | } |
| 128 | $lis[] = sprintf( |
| 129 | '<li data-id="%s" data-name="%s"><span title="%s">%s</span>%s<ol>%s</ol></li>', |
| 130 | $term->term_id, |
| 131 | htmlentities( $term->name ), |
| 132 | $term->term_id, |
| 133 | self::trimName( $term->name ), |
| 134 | $actions, |
| 135 | implode( '', $under ) |
| 136 | ); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | ?> |
| 141 | <div class="levels"> |
| 142 | <ol> |
| 143 | <?php echo implode( '', $lis ); ?> |
| 144 | </ol> |
| 145 | </div> |
| 146 | <form method="post" action="<?php echo admin_url( 'admin-post.php' ); ?>" id="LevelRemoveForm"> |
| 147 | <input type="hidden" name="action" value="fapi_member_remove_level"> |
| 148 | <input type="hidden" name="fapi_member_remove_level_nonce" |
| 149 | value="<?php echo wp_create_nonce( 'fapi_member_remove_level_nonce' ); ?>"> |
| 150 | <input type="hidden" name="level_id" value=""> |
| 151 | </form> |
| 152 | <form method="post" action="<?php echo admin_url( 'admin-post.php' ); ?>" id="LevelEditForm"> |
| 153 | <input type="hidden" name="action" value="fapi_member_edit_level"> |
| 154 | <input type="hidden" name="fapi_member_edit_level_nonce" |
| 155 | value="<?php echo wp_create_nonce( 'fapi_member_edit_level_nonce' ); ?>"> |
| 156 | <input type="hidden" name="name" value=""> |
| 157 | <input type="hidden" name="level_id" value=""> |
| 158 | </form> |
| 159 | <form method="post" action="<?php echo admin_url( 'admin-post.php' ); ?>" id="LevelOrderForm"> |
| 160 | <input type="hidden" name="action" value="fapi_member_order_level"> |
| 161 | <input type="hidden" name="fapi_member_order_level_nonce" |
| 162 | value="<?php echo wp_create_nonce( 'fapi_member_order_level_nonce' ); ?>"> |
| 163 | <input type="hidden" name="direction" value=""> |
| 164 | <input type="hidden" name="id" value=""> |
| 165 | </form> |
| 166 | <?php |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * @param string $name |
| 171 | * @param int $chars |
| 172 | * @return string |
| 173 | */ |
| 174 | public static function trimName( $name, $chars = 30 ) { |
| 175 | if ( mb_strlen( $name ) > $chars ) { |
| 176 | return sprintf( '%s…', mb_substr( $name, 0, $chars - 1 ) ); |
| 177 | } |
| 178 | |
| 179 | return $name; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * @return void |
| 184 | */ |
| 185 | public static function levelsSelection() { |
| 186 | global $FapiPlugin; |
| 187 | global $templateProvider; |
| 188 | |
| 189 | $subpage = $templateProvider->findSubpage(); |
| 190 | |
| 191 | $subpage = ( $subpage === 'settingsContentSelect' ) ? 'settingsContentRemove' : $subpage; |
| 192 | $selected = ( isset( $_GET['level'] ) ) ? self::sanitizeLevelId( $_GET['level'] ) : null; |
| 193 | |
| 194 | $envelopes = $FapiPlugin->levels()->loadAsTermEnvelopes(); |
| 195 | |
| 196 | $lis = array(); |
| 197 | |
| 198 | foreach ( $envelopes as $envelope ) { |
| 199 | $term = $envelope->getTerm(); |
| 200 | $under = array(); |
| 201 | if ( $term->parent === 0 ) { |
| 202 | foreach ( $envelopes as $underEnvelope ) { |
| 203 | $underTerm = $underEnvelope->getTerm(); |
| 204 | if ( $underTerm->parent === $term->term_id ) { |
| 205 | $under[] = self::oneLevelSelection( |
| 206 | $underTerm->term_id, |
| 207 | self::fapilink( $subpage ) . sprintf( '&level=%s', $underTerm->term_id ), |
| 208 | $underTerm->name, |
| 209 | '', |
| 210 | $underTerm->term_id === $selected |
| 211 | ); |
| 212 | } |
| 213 | } |
| 214 | $lis[] = self::oneLevelSelection( |
| 215 | $term->term_id, |
| 216 | self::fapilink( $subpage ) . sprintf( '&level=%s', $term->term_id ), |
| 217 | $term->name, |
| 218 | implode( '', $under ), |
| 219 | $term->term_id === $selected |
| 220 | ); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | ?> |
| 225 | <div class="levels"> |
| 226 | <ol> |
| 227 | <?php echo implode( '', $lis ); ?> |
| 228 | </ol> |
| 229 | </div> |
| 230 | <?php |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * @param int $id |
| 235 | * @param string $link |
| 236 | * @param string $name |
| 237 | * @param string $children |
| 238 | * @param bool $highlight |
| 239 | * @return string |
| 240 | */ |
| 241 | public static function oneLevelSelection( $id, $link, $name, $children = '', $highlight = false ) { |
| 242 | $c = ( $highlight ) ? 'class="selected"' : ''; |
| 243 | $ch = ( ! empty( $children ) ) ? sprintf( '<ol>%s</ol>', $children ) : ''; |
| 244 | |
| 245 | return sprintf( |
| 246 | '<li data-id="%s" %s><a href="%s">%s</a>%s</li>', |
| 247 | $id, |
| 248 | $c, |
| 249 | $link, |
| 250 | $name, |
| 251 | $ch |
| 252 | ); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * @return void |
| 257 | */ |
| 258 | public static function levelsSelectionNonJs() { |
| 259 | global $FapiPlugin; |
| 260 | global $templateProvider; |
| 261 | |
| 262 | $subpage = $templateProvider->findSubpage(); |
| 263 | |
| 264 | $subpage = ( $subpage === 'settingsContentSelect' ) ? 'settingsContentRemove' : $subpage; |
| 265 | $selected = ( isset( $_GET['level'] ) ) ? self::sanitizeLevelId( $_GET['level'] ) : null; |
| 266 | |
| 267 | $evnelopes = $FapiPlugin->levels()->loadAsTermEnvelopes(); |
| 268 | |
| 269 | $lis = array(); |
| 270 | |
| 271 | foreach ( $evnelopes as $envelope ) { |
| 272 | $term = $envelope->getTerm(); |
| 273 | $under = array(); |
| 274 | if ( $term->parent === 0 ) { |
| 275 | foreach ( $evnelopes as $underEnvelope ) { |
| 276 | $underTerm = $underEnvelope->getTerm(); |
| 277 | if ( $underTerm->parent === $term->term_id ) { |
| 278 | $under[] = self::oneLevelSelection( |
| 279 | $underTerm->term_id, |
| 280 | self::fapilink( $subpage ) . sprintf( '&level=%s', $underTerm->term_id ), |
| 281 | self::trimName( $underTerm->name ), |
| 282 | '', |
| 283 | $underTerm->term_id === $selected |
| 284 | ); |
| 285 | } |
| 286 | } |
| 287 | $lis[] = self::oneLevelSelection( |
| 288 | $term->term_id, |
| 289 | self::fapilink( $subpage ) . sprintf( '&level=%s', $term->term_id ), |
| 290 | self::trimName( $term->name ), |
| 291 | implode( '', $under ), |
| 292 | $term->term_id === $selected |
| 293 | ); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | ?> |
| 298 | <div class="levelsNonJs"> |
| 299 | <ol> |
| 300 | <?php echo implode( '', $lis ); ?> |
| 301 | </ol> |
| 302 | </div> |
| 303 | <?php |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * @return string |
| 308 | */ |
| 309 | public static function getLevelOptions() { |
| 310 | global $FapiPlugin; |
| 311 | $t = $FapiPlugin->levels()->loadAsTermEnvelopes(); |
| 312 | |
| 313 | $options = array(); |
| 314 | |
| 315 | foreach ( $t as $termEnvelope ) { |
| 316 | $term = $termEnvelope->getTerm(); |
| 317 | if ( $term->parent === 0 ) { |
| 318 | $options[] = sprintf( '<option value="%s">%s</option>', $term->term_id, $term->name ); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | return implode( '', $options ); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * @param int $levelId |
| 327 | * @return string |
| 328 | */ |
| 329 | public static function allPagesForForm( $levelId ) { |
| 330 | global $FapiPlugin; |
| 331 | |
| 332 | $posts = get_posts( |
| 333 | array( |
| 334 | 'post_type' => PostTypeHelper::getSupportedPostTypes(), |
| 335 | 'post_status' => array( 'publish' ), |
| 336 | 'numberposts' => -1, |
| 337 | 'orderby' => 'post_title', |
| 338 | 'order' => 'ASC', |
| 339 | ) |
| 340 | ); |
| 341 | $levelTerm = $FapiPlugin->levels()->loadById( $levelId ); |
| 342 | $postsInLevel = $FapiPlugin->levels()->pageIdsForLevel( $levelTerm ); |
| 343 | $output = array(); |
| 344 | |
| 345 | $posts_by_type = array(); |
| 346 | foreach ( $posts as $post ) { |
| 347 | $posts_by_type[ $post->post_type ][] = $post; |
| 348 | } |
| 349 | |
| 350 | ksort( $posts_by_type ); |
| 351 | |
| 352 | foreach ( array( 'page', 'post' ) as $type ) { |
| 353 | |
| 354 | if ( isset( $posts_by_type[ $type ] ) ) { |
| 355 | |
| 356 | $output[] = '<br/><h2><strong>' . __( $type . 's', 'fapi-member' ) . '</strong></h2>'; |
| 357 | |
| 358 | foreach ( $posts_by_type[ $type ] as $post ) { |
| 359 | |
| 360 | $checked = ( in_array( $post->ID, $postsInLevel, true ) ) ? ' checked ' : ''; |
| 361 | |
| 362 | $output[] = sprintf( |
| 363 | '<div class="onePage"><input type="checkbox" name="selection[]" value="%s" %s> %s </div>', |
| 364 | $post->ID, |
| 365 | $checked, |
| 366 | $post->post_title |
| 367 | ); |
| 368 | } |
| 369 | |
| 370 | unset( $posts_by_type[ $type ] ); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | $all_post_types = PostTypeHelper::getSupportedPostTypes( true ); |
| 375 | $all_stored_post_types = get_option(OptionKey::POST_TYPES, array()); |
| 376 | |
| 377 | $stored_post_types = empty( $all_stored_post_types[ $levelId ] ) ? array() : $all_stored_post_types[ $levelId ]; |
| 378 | |
| 379 | if ( $all_post_types ) { |
| 380 | |
| 381 | $output[] = '<br/><h2><strong>' . __( 'CPT', 'fapi-member' ) . '</strong></h2>'; |
| 382 | |
| 383 | foreach ( $all_post_types as $post_type ) { |
| 384 | |
| 385 | $checked = ( in_array( $post_type, $stored_post_types, true ) ) ? ' checked ' : ''; |
| 386 | |
| 387 | $output[] = sprintf( |
| 388 | '<div class="onePage"><input type="checkbox" name="cpt_selection[]" value="%s" %s> %s</div>', |
| 389 | $post_type, |
| 390 | $checked, |
| 391 | ucfirst( $post_type ) |
| 392 | ); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | return implode( '', $output ); |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * @param int $levelId |
| 401 | * @return string |
| 402 | */ |
| 403 | public static function allPagesInLevel( $levelId ) { |
| 404 | global $FapiPlugin; |
| 405 | $levelTerm = $FapiPlugin->levels()->loadById( $levelId ); |
| 406 | $pageIds = $FapiPlugin->levels()->pageIdsForLevel( $levelTerm ); |
| 407 | |
| 408 | if ( count( $pageIds ) === 0 ) { |
| 409 | // return ''; |
| 410 | $pageIds = array( PHP_INT_MAX ); // TODO[hack] ;-) |
| 411 | } |
| 412 | |
| 413 | $posts = get_posts( |
| 414 | array( |
| 415 | 'post_type' => PostTypeHelper::getSupportedPostTypes(), |
| 416 | 'post_status' => array( 'publish' ), |
| 417 | 'numberposts' => -1, |
| 418 | 'include' => $pageIds, |
| 419 | 'orderby' => 'page_title', |
| 420 | 'order' => 'ASC', |
| 421 | ) |
| 422 | ); |
| 423 | |
| 424 | $posts_by_type = array(); |
| 425 | |
| 426 | foreach ( $posts as $post ) { |
| 427 | $posts_by_type[ $post->post_type ][] = $post; |
| 428 | } |
| 429 | |
| 430 | ksort( $posts_by_type ); |
| 431 | |
| 432 | $o = array(); |
| 433 | |
| 434 | foreach ( array( 'page', 'post' ) as $type ) { |
| 435 | |
| 436 | if ( isset( $posts_by_type[ $type ] ) ) { |
| 437 | |
| 438 | $o[] = '<br/><h2><strong>' . __( $type . 's', 'fapi-member' ) . '</strong></h2>'; |
| 439 | |
| 440 | $arrayMap = array_map( |
| 441 | static function ( $p ) { |
| 442 | return sprintf( |
| 443 | '<div class="onePage">%s</div>', |
| 444 | $p->post_title |
| 445 | ); |
| 446 | }, |
| 447 | $posts_by_type[ $type ] |
| 448 | ); |
| 449 | |
| 450 | $o = array_merge( |
| 451 | $o, |
| 452 | $arrayMap |
| 453 | ); |
| 454 | |
| 455 | unset( $posts_by_type[ $type ] ); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | if ($posts_by_type) { |
| 461 | |
| 462 | $o[] = '<br/><big><strong>' . __('CPT (příspěvky)', 'fapi-member') . '</strong></big><br/>'; |
| 463 | |
| 464 | foreach( $posts_by_type as $posts ) { |
| 465 | |
| 466 | $o = array_merge($o, array_map( |
| 467 | function ( $p ) { |
| 468 | return sprintf( |
| 469 | '<div class="onePage">%s <small><em>(%s)</em></small></div>', |
| 470 | $p->post_title, |
| 471 | $p->post_type |
| 472 | ); |
| 473 | }, |
| 474 | $posts |
| 475 | )); |
| 476 | |
| 477 | } |
| 478 | }*/ |
| 479 | |
| 480 | $all_post_types = PostTypeHelper::getSupportedPostTypes( true ); |
| 481 | $all_stored_post_types = get_option( OptionKey::POST_TYPES, array() ); |
| 482 | |
| 483 | $stored_post_types = empty( $all_stored_post_types[ $levelId ] ) ? array() : $all_stored_post_types[ $levelId ]; |
| 484 | |
| 485 | $displayed_post_types = array_intersect( $stored_post_types, $all_post_types ); |
| 486 | |
| 487 | if ( $displayed_post_types ) { |
| 488 | |
| 489 | $o[] = '<br/><h2><strong>' . __( 'CPT', 'fapi-member' ) . '</strong></h2><br/>'; |
| 490 | |
| 491 | $o = array_merge( |
| 492 | $o, |
| 493 | array_map( |
| 494 | static function ( $p ) { |
| 495 | return sprintf( |
| 496 | '<div class="onePage">%s</div>', |
| 497 | ucfirst( $p ) |
| 498 | ); |
| 499 | }, |
| 500 | $displayed_post_types |
| 501 | ) |
| 502 | ); |
| 503 | } |
| 504 | |
| 505 | return implode( '', $o ); |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * @param int $pageId |
| 510 | * @return string|null |
| 511 | */ |
| 512 | public static function getPageTitle( $pageId ) { |
| 513 | $posts = get_posts( |
| 514 | array( |
| 515 | 'post_type' => PostTypeHelper::getSupportedPostTypes(), |
| 516 | 'post_status' => array( 'publish' ), |
| 517 | 'numberposts' => -1, |
| 518 | ) |
| 519 | ); |
| 520 | |
| 521 | foreach ( $posts as $post ) { |
| 522 | if ( (int) $post->ID !== $pageId ) { |
| 523 | continue; |
| 524 | } |
| 525 | |
| 526 | return $post->post_title; |
| 527 | } |
| 528 | |
| 529 | return null; |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * @param int $currentId |
| 534 | * @return string |
| 535 | */ |
| 536 | public static function allPagesAsOptions( $currentId ) { |
| 537 | $posts = get_posts( |
| 538 | array( |
| 539 | 'post_type' => 'page', |
| 540 | 'post_status' => array( 'publish' ), |
| 541 | 'numberposts' => -1, |
| 542 | 'orderby' => 'post_title', |
| 543 | 'order' => 'ASC', |
| 544 | ) |
| 545 | ); |
| 546 | $output = array(); |
| 547 | |
| 548 | foreach ( $posts as $post ) { |
| 549 | $selected = ( $currentId === $post->ID ) ? 'selected' : ''; |
| 550 | |
| 551 | $output[] = sprintf( '<option value="%s" %s>%s</option>\n', $post->ID, $selected, $post->post_title ); |
| 552 | } |
| 553 | |
| 554 | return implode( ' ', $output ); |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * @return string |
| 559 | */ |
| 560 | public static function levelToPageJson() { |
| 561 | global $FapiPlugin; |
| 562 | |
| 563 | return json_encode( $FapiPlugin->levels()->levelsToPages() ); |
| 564 | } |
| 565 | |
| 566 | public static function shortcodeSectionExpirationDate(array $attrs): string |
| 567 | { |
| 568 | global $FapiPlugin; |
| 569 | global $membershipRepository; |
| 570 | |
| 571 | if ( ! isset( $attrs['section'] ) ) { |
| 572 | return __( 'neznámá sekce nebo úrověň', 'fapi-member' ); |
| 573 | } |
| 574 | |
| 575 | $user = wp_get_current_user(); |
| 576 | |
| 577 | if ( $user === null ) { |
| 578 | return __( 'uživatel není přihlášen', 'fapi-member' ); |
| 579 | } |
| 580 | |
| 581 | $sectionOrLevelId = (int) $attrs['section']; |
| 582 | |
| 583 | $dateFormat = get_option( 'date_format' ); |
| 584 | |
| 585 | if ( $dateFormat === null ) { |
| 586 | $dateFormat = 'Y-m-d'; |
| 587 | } |
| 588 | |
| 589 | $memberships = $membershipRepository->getActiveByUserId($user->ID); |
| 590 | $currentMemberShip = null; |
| 591 | |
| 592 | /** @var FapiMembership $membership */ |
| 593 | foreach ( $memberships as $membership ) { |
| 594 | if ( $membership->getLevelId() === $sectionOrLevelId) { |
| 595 | $currentMemberShip = $membership; |
| 596 | |
| 597 | break; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | if ( $currentMemberShip === null ) { |
| 602 | return __( 'bez přístupu', 'fapi-member' ); |
| 603 | |
| 604 | } |
| 605 | |
| 606 | if ( $currentMemberShip->getUntil() === null ) { |
| 607 | return __( 'neomezeně', 'fapi-member' ); |
| 608 | } |
| 609 | |
| 610 | return $currentMemberShip->getUntil()->format($dateFormat); |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * @param array<mixed> $attrs |
| 615 | * @return string |
| 616 | */ |
| 617 | public static function shortcodeUnlockLevel(array $attrs): string |
| 618 | { |
| 619 | if ( ! isset( $attrs['level'] ) ) { |
| 620 | return __( 'neznámá sekce nebo úrověň', 'fapi-member' ); |
| 621 | } |
| 622 | |
| 623 | $sectionOrLevelId = (int) $attrs['level']; |
| 624 | $html = self::formStart('button_level_unlock'); |
| 625 | $html .= '<input type="hidden" name="level" value="' . $sectionOrLevelId . '">'; |
| 626 | |
| 627 | if (isset($attrs['page'])) { |
| 628 | $html .= '<input type="hidden" name="page" value="' . $attrs['page'] . '">'; |
| 629 | } |
| 630 | |
| 631 | $html .= '<button class="unlock-level-button" type="submit" name="submit"">Odemknout úroveň</button></form>'; |
| 632 | |
| 633 | return $html; |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * @param array<mixed> $attrs |
| 638 | * @return string |
| 639 | */ |
| 640 | public static function shortcodeLevelUnlockDate(array $attrs): string |
| 641 | { |
| 642 | global $FapiPlugin; |
| 643 | global $membershipRepository; |
| 644 | |
| 645 | if ( ! isset( $attrs['section'] ) ) { |
| 646 | return __( 'neznámá sekce nebo úrověň', 'fapi-member' ); |
| 647 | } |
| 648 | |
| 649 | $user = wp_get_current_user(); |
| 650 | |
| 651 | if ( $user === null ) { |
| 652 | return __( 'uživatel není přihlášen', 'fapi-member' ); |
| 653 | } |
| 654 | |
| 655 | $sectionOrLevelId = (int) $attrs['section']; |
| 656 | |
| 657 | $dateFormat = get_option( 'date_format' ); |
| 658 | |
| 659 | if ( $dateFormat === null ) { |
| 660 | $dateFormat = 'Y-m-d'; |
| 661 | } |
| 662 | |
| 663 | $memberships = $membershipRepository->getActiveByUserId($user->ID); |
| 664 | $parentLevel = $FapiPlugin->levels()->loadParentById($sectionOrLevelId); |
| 665 | $currentMemberShip = null; |
| 666 | $parentMembership = null; |
| 667 | |
| 668 | /** @var FapiMembership $membership */ |
| 669 | foreach ($memberships as $membership) { |
| 670 | if ($membership->getLevelId() === $sectionOrLevelId) { |
| 671 | $currentMemberShip = $membership; |
| 672 | } |
| 673 | |
| 674 | if ($membership->getLevelId() === $parentLevel->term_id) { |
| 675 | $parentMembership = $membership; |
| 676 | } |
| 677 | |
| 678 | if ($currentMemberShip !== null && $parentMembership !== null) { |
| 679 | break; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | if ( $currentMemberShip === null || |
| 684 | ($currentMemberShip === null && |
| 685 | (bool) get_term_meta($sectionOrLevelId, MetaKey::TIME_UNLOCK, true) === false) |
| 686 | ) { |
| 687 | return __( 'bez přístupu', 'fapi-member' ); |
| 688 | } |
| 689 | |
| 690 | |
| 691 | if ( $currentMemberShip === null && $parentMembership->registered !== null) { |
| 692 | $daysToUnlock = get_term_meta($sectionOrLevelId, MetaKey::DAYS_TO_UNLOCK, true); |
| 693 | |
| 694 | $unlockDate = date( |
| 695 | 'd.m.Y', |
| 696 | strtotime($parentMembership->registered->format($dateFormat)) |
| 697 | + (86400 * (int) $daysToUnlock), |
| 698 | ); |
| 699 | |
| 700 | return __( 'Bude odemčeno', 'fapi-member' ) . " " . $unlockDate; |
| 701 | } |
| 702 | |
| 703 | if ( $currentMemberShip->getUntil() === null ) { |
| 704 | return __( 'neomezeně', 'fapi-member' ); |
| 705 | } |
| 706 | |
| 707 | return $currentMemberShip->getUntil()->format( $dateFormat ); |
| 708 | } |
| 709 | |
| 710 | public static function shortcodeLoginForm(): string |
| 711 | { |
| 712 | return ' |
| 713 | <div class="fapiShortcodeLoginForm"> |
| 714 | <form method="post" action="' . wp_login_url() . '"> |
| 715 | <div class="f-m-row"> |
| 716 | <label for="log">' . __( 'Přihlašovací jméno', 'fapi-member' ) . '</label> |
| 717 | <input type="text" name="log" id="user_login" value="" size="20"> |
| 718 | </div> |
| 719 | <div class="f-m-row"> |
| 720 | <label for="pwd">' . __( 'Heslo', 'fapi-member' ) . '</label> |
| 721 | <input type="password" name="pwd" id="user_pass" value="" size="20"> |
| 722 | </div> |
| 723 | <div class="f-m-row"> |
| 724 | <a href="' . wp_lostpassword_url() . '">' . __( 'Zapomněli jste heslo?', 'fapi-member' ) . '</a> |
| 725 | </div> |
| 726 | <div class="f-m-row controls"> |
| 727 | <input type="submit" class="primary" value="' . __( 'Přihlásit se', 'fapi-member' ) . '"> |
| 728 | </div> |
| 729 | </form> |
| 730 | </div> |
| 731 | '; |
| 732 | } |
| 733 | |
| 734 | public static function shortcodeUser(): string |
| 735 | { |
| 736 | global $FapiPlugin; |
| 737 | global $settingsRepository; |
| 738 | |
| 739 | $u = wp_get_current_user(); |
| 740 | if ( $u instanceof WP_User && is_user_logged_in() ) { |
| 741 | return ' |
| 742 | <div class="fapiShortcodeUser"> |
| 743 | <span class="i"> |
| 744 | <svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" |
| 745 | viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve"> |
| 746 | <g> |
| 747 | <g> |
| 748 | <path d="M437.02,330.98c-27.883-27.882-61.071-48.523-97.281-61.018C378.521,243.251,404,198.548,404,148 |
| 749 | C404,66.393,337.607,0,256,0S108,66.393,108,148c0,50.548,25.479,95.251,64.262,121.962 |
| 750 | c-36.21,12.495-69.398,33.136-97.281,61.018C26.629,379.333,0,443.62,0,512h40c0-119.103,96.897-216,216-216s216,96.897,216,216 |
| 751 | h40C512,443.62,485.371,379.333,437.02,330.98z M256,256c-59.551,0-108-48.448-108-108S196.449,40,256,40 |
| 752 | c59.551,0,108,48.448,108,108S315.551,256,256,256z"/> |
| 753 | </g> |
| 754 | </g> |
| 755 | </svg> |
| 756 | </span> |
| 757 | <span class="h">' . __( 'Uživatel', 'fapi-member' ) . '</span> |
| 758 | <div> |
| 759 | <span class="l">' . $u->user_login . '</span><span class="dots">...</span> |
| 760 | </div> |
| 761 | <div class="f-m-submenu"> |
| 762 | <a href="' . wp_logout_url( get_permalink() ) . '">' . __( 'Odhlásit se', 'fapi-member' ) . '</a> |
| 763 | </div> |
| 764 | </div> |
| 765 | '; |
| 766 | } |
| 767 | |
| 768 | $setLoginPageId = $settingsRepository->getSetting(SettingsKey::LOGIN_PAGE); |
| 769 | |
| 770 | if ( $setLoginPageId === null ) { |
| 771 | $url = wp_login_url(); |
| 772 | } else { |
| 773 | $url = get_permalink( $setLoginPageId ); |
| 774 | } |
| 775 | |
| 776 | return ' |
| 777 | <div class="fapiShortcodeUser notLogged"> |
| 778 | <span class="i"> |
| 779 | <svg id="bold" enable-background="new 0 0 24 24" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="m18.75 9h-.75v-3c0-3.309-2.691-6-6-6s-6 2.691-6 6v3h-.75c-1.24 0-2.25 1.009-2.25 2.25v10.5c0 1.241 1.01 2.25 2.25 2.25h13.5c1.24 0 2.25-1.009 2.25-2.25v-10.5c0-1.241-1.01-2.25-2.25-2.25zm-10.75-3c0-2.206 1.794-4 4-4s4 1.794 4 4v3h-8zm5 10.722v2.278c0 .552-.447 1-1 1s-1-.448-1-1v-2.278c-.595-.347-1-.985-1-1.722 0-1.103.897-2 2-2s2 .897 2 2c0 .737-.405 1.375-1 1.722z"/></svg> |
| 780 | </span> |
| 781 | <span class="l"><a href="' . $url . '">' . __( 'Přihlásit se', 'fapi-member' ) . '</a></span> |
| 782 | </div> |
| 783 | '; |
| 784 | } |
| 785 | |
| 786 | /** |
| 787 | * @param string $hook |
| 788 | * @param array<string> $formClasses |
| 789 | * @return string |
| 790 | */ |
| 791 | public static function formStart( $hook, $formClasses = array() ) { |
| 792 | $class = ( empty( $formClasses ) ) ? '' : sprintf( ' class="%s"', implode( ' ', $formClasses ) ); |
| 793 | |
| 794 | return ' |
| 795 | <form ' . $class . ' method="post" action="' . admin_url( 'admin-post.php' ) . '"> |
| 796 | <input type="hidden" name="action" value="fapi_member_' . $hook . '"> |
| 797 | <input type="hidden" name="fapi_member_' . $hook . '_nonce" |
| 798 | value="' . wp_create_nonce( 'fapi_member_' . $hook . '_nonce' ) . '"> |
| 799 | '; |
| 800 | } |
| 801 | |
| 802 | /** |
| 803 | * @return string |
| 804 | */ |
| 805 | public static function heading() { |
| 806 | return sprintf( |
| 807 | '%s<div class="baseGrid">%s%s%s', |
| 808 | self::resolutionMessage(), |
| 809 | self::h1(), |
| 810 | self::nav(), |
| 811 | self::submenu() |
| 812 | ); |
| 813 | } |
| 814 | |
| 815 | /** |
| 816 | * @return string |
| 817 | */ |
| 818 | public static function resolutionMessage() { |
| 819 | return '<p class="resolutionAlert">' . __( 'Tento doplněk není optimalizován pro telefony a malé monitory.', 'fapi-member' ) . '</p>'; |
| 820 | } |
| 821 | |
| 822 | /** |
| 823 | * @return string |
| 824 | */ |
| 825 | public static function h1() { |
| 826 | $svg = file_get_contents( __DIR__ . '/../../_sources/LOGO_FAPI_svg.svg' ); |
| 827 | |
| 828 | return sprintf( '<div class="h1"><a href="https://web.fapi.cz">%s</a></div>', $svg ); |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * @return string |
| 833 | */ |
| 834 | public static function nav() { |
| 835 | global $FapiPlugin; |
| 836 | global $apiService; |
| 837 | global $templateProvider; |
| 838 | |
| 839 | $subpage = $templateProvider->findSubpage(); |
| 840 | $areApiCredentialsSet = $apiService->areApiCredentialsSet(); |
| 841 | |
| 842 | $c = file_get_contents( __DIR__ . '/../../_sources/connect.svg' ); |
| 843 | $h = file_get_contents( __DIR__ . '/../../_sources/home-solid.svg' ); |
| 844 | $p = file_get_contents( __DIR__ . '/../../_sources/padlock.svg' ); |
| 845 | |
| 846 | $testActionLink = ''; |
| 847 | if ( $FapiPlugin::isDevelopment() ) { |
| 848 | $testActionLink = ' |
| 849 | <a href="' . self::fapilink( 'test' ) . '" ' . ( ( $subpage === 'test' ) ? 'class="active"' : '' ) . '> |
| 850 | <span class="a" style="color: #9a1818;">Testovací akce</span> |
| 851 | </a>'; |
| 852 | } |
| 853 | |
| 854 | if ( ! $areApiCredentialsSet ) { |
| 855 | return ' |
| 856 | <nav> |
| 857 | <span class="disabled"> |
| 858 | <span class="a">' . __( 'Přehled', 'fapi-member' ) . '</span> |
| 859 | ' . $h . ' |
| 860 | </span> |
| 861 | <span class="disabled"> |
| 862 | <span class="a">' . __( 'Členské sekce', 'fapi-member' ) . '</span> |
| 863 | ' . $p . ' |
| 864 | </span> |
| 865 | <a href="#" class="active"> |
| 866 | <span class="a">' . __( 'Propojení s FAPI', 'fapi-member' ) . '</span> |
| 867 | ' . $c . ' |
| 868 | </a> |
| 869 | ' . $testActionLink . ' |
| 870 | </nav>'; |
| 871 | } |
| 872 | |
| 873 | return ' |
| 874 | <nav> |
| 875 | <a href="' . self::fapilink( 'index' ) . '" ' . ( ( $subpage === 'index' ) ? 'class="active"' : '' ) . '> |
| 876 | <span class="a">' . __( 'Přehled', 'fapi-member' ) . '</span> |
| 877 | ' . $h . ' |
| 878 | </a> |
| 879 | <a href="' . self::fapilink( 'settingsSectionNew' ) . '" ' . ( ( strpos( |
| 880 | $subpage, |
| 881 | 'settings' |
| 882 | ) === 0 ) ? 'class="active"' : '' ) . '> |
| 883 | <span class="a">' . __( 'Členské sekce', 'fapi-member' ) . '</span> |
| 884 | ' . $p . ' |
| 885 | </a> |
| 886 | <a href="' . self::fapilink( 'connection' ) . '" ' . ( ( $subpage === 'connection' ) ? 'class="active"' : '' ) . '> |
| 887 | <span class="a">' . __( 'Propojení s FAPI', 'fapi-member' ) . '</span> |
| 888 | ' . $c . ' |
| 889 | </a> |
| 890 | ' . $testActionLink . ' |
| 891 | </nav>'; |
| 892 | } |
| 893 | |
| 894 | /** |
| 895 | * @return string |
| 896 | */ |
| 897 | public static function submenu() { |
| 898 | global $templateProvider; |
| 899 | |
| 900 | $subpage = $templateProvider->findSubpage(); |
| 901 | |
| 902 | switch ( true ) { |
| 903 | case ( $subpage === 'index' ): |
| 904 | return ' |
| 905 | <div class="submenu"> |
| 906 | <span class="active">' . __( 'Přehled', 'fapi-member' ) . '</a> |
| 907 | </div> |
| 908 | '; |
| 909 | case ( $subpage === 'connection' ): |
| 910 | return ' |
| 911 | <div class="submenu"> |
| 912 | <span class="active">' . __( 'Propojení', 'fapi-member' ) . '</a> |
| 913 | </div> |
| 914 | '; |
| 915 | case ( mb_strpos( $subpage, 'settings' ) === 0 ): |
| 916 | return ' |
| 917 | <div class="submenu"> |
| 918 | ' . self::submenuItem( 'settingsSectionNew', 'Sekce / úrovně', $subpage, array( 'settingsLevelNew' ) ) . ' |
| 919 | ' . self::submenuItem( |
| 920 | 'settingsContentAdd', |
| 921 | __( 'Přiřazené stránky a příspěvky', 'fapi-member' ), |
| 922 | $subpage, |
| 923 | array( 'settingsContentRemove' ) |
| 924 | ) . ' |
| 925 | ' . self::submenuItem( 'settingsEmails', __( 'E-maily', 'fapi-member' ), $subpage ) . ' |
| 926 | ' . self::submenuItem( 'settingsPages', __( 'Servisní stránky', 'fapi-member' ), $subpage ) . ' |
| 927 | ' . self::submenuItem( 'settingsElements', __( 'Prvky pro web', 'fapi-member' ), $subpage ) . ' |
| 928 | ' . self::submenuItem( 'settingsSettings', __( 'Společné', 'fapi-member' ), $subpage ) . ' |
| 929 | ' . self::submenuItem( 'settingsUnlocking', __( 'Uvolňování obsahu', 'fapi-member' ), $subpage ) . ' |
| 930 | </div> |
| 931 | '; |
| 932 | } |
| 933 | |
| 934 | return ''; |
| 935 | } |
| 936 | |
| 937 | /** |
| 938 | * @param string $subpage |
| 939 | * @param string $label |
| 940 | * @param string $activeSubpage |
| 941 | * @param array<string> $otherChildren |
| 942 | * @return string |
| 943 | */ |
| 944 | public static function submenuItem( $subpage, $label, $activeSubpage, $otherChildren = null ) { |
| 945 | $classes = array(); |
| 946 | |
| 947 | if ( $activeSubpage === $subpage ) { |
| 948 | $classes[] = 'active'; |
| 949 | } |
| 950 | if ( $otherChildren !== null && in_array( $activeSubpage, $otherChildren, true ) ) { |
| 951 | $classes[] = 'active'; |
| 952 | } |
| 953 | |
| 954 | return sprintf( '<a href="%s" class="%s">%s</a>', self::fapilink( $subpage ), implode( ' ', $classes ), $label ); |
| 955 | } |
| 956 | |
| 957 | } |
| 958 |