index.php
125 lines
| 1 | <?php |
| 2 | |
| 3 | use IlluminateAgnostic\Arr\Support\Arr; |
| 4 | use Kubio\Flags; |
| 5 | |
| 6 | add_action( |
| 7 | 'rest_api_init', |
| 8 | function () { |
| 9 | $namespace = 'kubio/v1'; |
| 10 | |
| 11 | register_rest_route( |
| 12 | $namespace, |
| 13 | '/prepare_newsletter_plugin', |
| 14 | array( |
| 15 | 'methods' => 'POST', |
| 16 | 'callback' => 'kubio_api_prepare_newsletter_plugin', |
| 17 | 'permission_callback' => function () { |
| 18 | return current_user_can( 'edit_posts' ); |
| 19 | }, |
| 20 | |
| 21 | ) |
| 22 | ); |
| 23 | |
| 24 | register_rest_route( |
| 25 | $namespace, |
| 26 | '/get_newsletters', |
| 27 | array( |
| 28 | 'methods' => 'GET', |
| 29 | 'callback' => 'kubio_api_get_newsletters', |
| 30 | 'permission_callback' => function () { |
| 31 | return current_user_can( 'edit_posts' ); |
| 32 | }, |
| 33 | |
| 34 | ) |
| 35 | ); |
| 36 | } |
| 37 | ); |
| 38 | |
| 39 | |
| 40 | |
| 41 | function kubio_api_prepare_newsletter_plugin( WP_REST_Request $request ) { |
| 42 | //in case of failures only try init once |
| 43 | $already_setup = Flags::getSetting( 'newslettersInstalled', null ); |
| 44 | if ( empty( $already_setup ) ) { |
| 45 | Flags::setSetting( 'newslettersInstalled', true ); |
| 46 | } |
| 47 | |
| 48 | if ( ! kubio_is_recommendation_newsletter_plugin_active() ) { |
| 49 | wp_send_json_error( __( 'Promo plugin is not active!', 'kubio' ), 400 ); |
| 50 | } |
| 51 | if ( ! class_exists( '\CSPromo\Core\Admin\PopupService' ) ) { |
| 52 | wp_send_json_error( __( 'At least one of required classes is missing', 'kubio' ), 400 ); |
| 53 | } |
| 54 | if ( ! method_exists( '\CSPromo\Core\Admin\PopupService', 'create' ) ) { |
| 55 | wp_send_json_error( __( 'At least one of required functions is missing', 'kubio' ), 400 ); |
| 56 | } |
| 57 | |
| 58 | if ( $already_setup ) { |
| 59 | wp_send_json_success( kubio_get_recommendation_newsletters() ); |
| 60 | } |
| 61 | |
| 62 | ob_start(); |
| 63 | try { |
| 64 | |
| 65 | // Create Popup By Click and activate |
| 66 | $popup_by_click_data = require_once __DIR__ . '/defaults/default-popup-by-click.php'; |
| 67 | |
| 68 | $popup_by_click_id = kubio_recommendations_create_promo_popup( $popup_by_click_data ); |
| 69 | |
| 70 | kubio_recommendation_set_click_promo_popup_triggers( $popup_by_click_id ); |
| 71 | |
| 72 | $popups = kubio_get_recommendation_newsletters(); |
| 73 | |
| 74 | $errors = ob_get_clean(); |
| 75 | |
| 76 | wp_send_json_success( $popups ); |
| 77 | } catch ( Exception $e ) { |
| 78 | $errors = ob_get_clean(); |
| 79 | |
| 80 | wp_send_json_error( $e->getMessage(), 400 ); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | |
| 85 | |
| 86 | function kubio_api_get_newsletters( WP_REST_Request $request ) { |
| 87 | |
| 88 | wp_send_json_success( kubio_get_recommendation_newsletters() ); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | |
| 93 | function kubio_setup_click_promo_trigger_for_blocks( $blocks ) { |
| 94 | |
| 95 | foreach ( $blocks as $block ) { |
| 96 | if ( isset( $block['blockName'] ) && 'kubio/button' === $block['blockName'] ) { |
| 97 | // Assuming the block has an attribute for the popup ID |
| 98 | $popup_id = Arr::get( $block, 'attrs.recommendation.newsletter.id', null ); |
| 99 | $link_type = Arr::get( $block, 'attrs.linkType', null ); |
| 100 | if ( $popup_id && 'newsletter' === $link_type ) { |
| 101 | kubio_recommendation_set_click_promo_popup_triggers( $popup_id ); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if ( isset( $block['innerBlocks'] ) && ! empty( $block['innerBlocks'] ) ) { |
| 106 | kubio_setup_click_promo_trigger_for_blocks( $block['innerBlocks'] ); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | |
| 112 | add_filter( |
| 113 | 'kubio/rest-pre-save-post/import-assets', |
| 114 | /** |
| 115 | * Filter to import assets when saving posts. |
| 116 | * @param \WP_Block[] $blocks The blocks to process. |
| 117 | */ |
| 118 | function ( $blocks ) { |
| 119 | |
| 120 | kubio_setup_click_promo_trigger_for_blocks( $blocks ); |
| 121 | |
| 122 | return $blocks; |
| 123 | } |
| 124 | ); |
| 125 |