newsletter
3 months ago
contact-forms.php
3 months ago
fluent-booking.php
3 months ago
index.php
3 months ago
siteleads.php
3 months ago
index.php
140 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | require_once __DIR__ . '/contact-forms.php'; |
| 6 | require_once __DIR__ . '/newsletter/index.php'; |
| 7 | require_once __DIR__ . '/fluent-booking.php'; |
| 8 | require_once __DIR__ . '/siteleads.php'; |
| 9 | |
| 10 | |
| 11 | add_action( |
| 12 | 'rest_api_init', |
| 13 | function () { |
| 14 | $namespace = 'kubio/v1'; |
| 15 | |
| 16 | register_rest_route( |
| 17 | $namespace, |
| 18 | '/get-recommendation-page-content-by-plugin', |
| 19 | array( |
| 20 | 'methods' => 'GET', |
| 21 | 'callback' => 'kubio_api_get_recommendation_page_content_by_plugin', |
| 22 | 'permission_callback' => function () { |
| 23 | return current_user_can( 'edit_posts' ); |
| 24 | }, |
| 25 | |
| 26 | ) |
| 27 | ); |
| 28 | |
| 29 | register_rest_route( |
| 30 | $namespace, |
| 31 | '/get-recommendation-default-settings', |
| 32 | array( |
| 33 | 'methods' => 'GET', |
| 34 | 'callback' => 'kubio_api_get_recommendation_default_settings', |
| 35 | 'permission_callback' => function () { |
| 36 | return current_user_can( 'edit_posts' ); |
| 37 | }, |
| 38 | ) |
| 39 | ); |
| 40 | |
| 41 | register_rest_route( |
| 42 | $namespace, |
| 43 | '/get-recommendations-settings', |
| 44 | array( |
| 45 | 'methods' => 'GET', |
| 46 | 'callback' => 'kubio_api_get_recommendations_settings', |
| 47 | 'permission_callback' => function () { |
| 48 | return current_user_can( 'edit_posts' ); |
| 49 | }, |
| 50 | ) |
| 51 | ); |
| 52 | } |
| 53 | ); |
| 54 | |
| 55 | |
| 56 | function kubio_api_get_recommendation_page_content_by_plugin( WP_REST_Request $request ) { |
| 57 | $plugin_slug = $request->get_param( 'pluginSlug' ); |
| 58 | $page_file_name = null; |
| 59 | switch ( $plugin_slug ) { |
| 60 | case 'contact-form-7': |
| 61 | $page_file_name = 'contact-form-page.html'; |
| 62 | break; |
| 63 | case 'fluent-booking': |
| 64 | $page_file_name = 'fluent-form-page.html'; |
| 65 | break; |
| 66 | } |
| 67 | if ( empty( $page_file_name ) ) { |
| 68 | wp_send_json_error( __( 'Page content not found for given plugin', 'kubio' ), 404 ); |
| 69 | } |
| 70 | |
| 71 | $page_file_path = KUBIO_ROOT_DIR . '/defaults/recommendations-pages/' . $page_file_name; |
| 72 | |
| 73 | if ( ! file_exists( $page_file_path ) ) { |
| 74 | wp_send_json_error( __( 'Page content not found for given plugin', 'kubio' ), 404 ); |
| 75 | } |
| 76 | $content = file_get_contents( $page_file_path ); |
| 77 | wp_send_json_success( $content ); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | function kubio_api_get_recommendation_default_settings( WP_REST_Request $request ) { |
| 82 | $plugin_slug = $request->get_param( 'plugin' ); |
| 83 | if ( empty( $plugin_slug ) ) { |
| 84 | wp_send_json_error( __( 'Plugin slug is required', 'kubio' ), 400 ); |
| 85 | } |
| 86 | |
| 87 | $url = 'https://kubiobuilder.com/wp-json/kubio-recommentation-defaults/' . urlencode( $plugin_slug ); |
| 88 | |
| 89 | $response = wp_remote_get( |
| 90 | $url |
| 91 | ); |
| 92 | |
| 93 | if ( is_wp_error( $response ) ) { |
| 94 | wp_send_json_error( __( 'Failed to fetch default settings', 'kubio' ), 500 ); |
| 95 | } |
| 96 | |
| 97 | $body = wp_remote_retrieve_body( $response ); |
| 98 | $code = wp_remote_retrieve_response_code( $response ); |
| 99 | |
| 100 | if ( $code !== 200 ) { |
| 101 | wp_send_json_error( __( 'Failed to fetch default settings', 'kubio' ), $code ); |
| 102 | } |
| 103 | |
| 104 | if ( empty( $body ) ) { |
| 105 | wp_send_json_error( __( 'No default settings found for the plugin', 'kubio' ), 404 ); |
| 106 | } |
| 107 | $default_settings = json_decode( $body, true ); |
| 108 | |
| 109 | if ( ! is_array( $default_settings ) ) { |
| 110 | wp_send_json_error( __( 'Invalid default settings format', 'kubio' ), 500 ); |
| 111 | } |
| 112 | |
| 113 | wp_send_json_success( $default_settings ); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | function kubio_api_get_recommendations_settings( WP_REST_Request $request ) { |
| 118 | |
| 119 | $settings = kubio_get_recommendations_settings(); |
| 120 | |
| 121 | foreach ( array_keys( $settings ) as $key ) { |
| 122 | switch ( $key ) { |
| 123 | case 'contactForm': |
| 124 | $settings[ $key ]['itemsList'] = kubio_get_recommendation_contact_forms(); |
| 125 | break; |
| 126 | case 'newsletters': |
| 127 | $settings[ $key ]['itemsList'] = kubio_get_recommendation_newsletters(); |
| 128 | break; |
| 129 | case 'fluentBooking': |
| 130 | $settings[ $key ]['itemsList'] = kubio_get_fluent_booking_events(); |
| 131 | break; |
| 132 | case 'siteleads': |
| 133 | $settings[ $key ]['itemsList'] = kubio_get_siteleads_widgets(); |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return $settings; |
| 139 | } |
| 140 |