class-fl-builder-services.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. /**
  3. * Helper class for connecting to third party services.
  4. *
  5. * @since 1.5.4
  6. */
  7. final class FLBuilderServices {
  8. /**
  9. * Data for working with each supported third party service.
  10. *
  11. * @since 1.5.4
  12. * @access private
  13. * @var array $services_data
  14. */
  15. static private $services_data = array(
  16. 'activecampaign' => array(
  17. 'type' => 'autoresponder',
  18. 'name' => 'ActiveCampaign',
  19. 'class' => 'FLBuilderServiceActiveCampaign',
  20. ),
  21. 'aweber' => array(
  22. 'type' => 'autoresponder',
  23. 'name' => 'AWeber',
  24. 'class' => 'FLBuilderServiceAWeber',
  25. ),
  26. 'campaign-monitor' => array(
  27. 'type' => 'autoresponder',
  28. 'name' => 'Campaign Monitor',
  29. 'class' => 'FLBuilderServiceCampaignMonitor',
  30. ),
  31. 'campayn' => array(
  32. 'type' => 'autoresponder',
  33. 'name' => 'Campayn',
  34. 'class' => 'FLBuilderServiceCampayn',
  35. ),
  36. 'constant-contact' => array(
  37. 'type' => 'autoresponder',
  38. 'name' => 'Constant Contact',
  39. 'class' => 'FLBuilderServiceConstantContact',
  40. ),
  41. 'convertkit' => array(
  42. 'type' => 'autoresponder',
  43. 'name' => 'ConvertKit',
  44. 'class' => 'FLBuilderServiceConvertKit',
  45. ),
  46. 'drip' => array(
  47. 'type' => 'autoresponder',
  48. 'name' => 'Drip',
  49. 'class' => 'FLBuilderServiceDrip',
  50. ),
  51. 'email-address' => array(
  52. 'type' => 'autoresponder',
  53. 'name' => 'Email Address',
  54. 'class' => 'FLBuilderServiceEmailAddress',
  55. ),
  56. 'enormail' => array(
  57. 'type' => 'autoresponder',
  58. 'name' => 'Enormail',
  59. 'class' => 'FLBuilderServiceEnormail',
  60. ),
  61. 'getresponse' => array(
  62. 'type' => 'autoresponder',
  63. 'name' => 'GetResponse',
  64. 'class' => 'FLBuilderServiceGetResponse',
  65. ),
  66. 'godaddy-email-marketing' => array(
  67. 'type' => 'autoresponder',
  68. 'name' => 'GoDaddy Email Marketing',
  69. 'class' => 'FLBuilderServiceGoDaddyEmailMarketing',
  70. ),
  71. 'hatchbuck' => array(
  72. 'type' => 'autoresponder',
  73. 'name' => 'Hatchbuck',
  74. 'class' => 'FLBuilderServiceHatchbuck',
  75. ),
  76. 'icontact' => array(
  77. 'type' => 'autoresponder',
  78. 'name' => 'iContact',
  79. 'class' => 'FLBuilderServiceIContact',
  80. ),
  81. 'icontact-pro' => array(
  82. 'type' => 'autoresponder',
  83. 'name' => 'iContact Pro',
  84. 'class' => 'FLBuilderServiceIContactPro',
  85. ),
  86. 'infusionsoft' => array(
  87. 'type' => 'autoresponder',
  88. 'name' => 'Infusionsoft',
  89. 'class' => 'FLBuilderServiceInfusionsoft',
  90. ),
  91. 'madmimi' => array(
  92. 'type' => 'autoresponder',
  93. 'name' => 'Mad Mimi',
  94. 'class' => 'FLBuilderServiceMadMimi',
  95. ),
  96. 'mailchimp' => array(
  97. 'type' => 'autoresponder',
  98. 'name' => 'MailChimp',
  99. 'class' => 'FLBuilderServiceMailChimp',
  100. ),
  101. 'mailerlite' => array(
  102. 'type' => 'autoresponder',
  103. 'name' => 'MailerLite',
  104. 'class' => 'FLBuilderServiceMailerLite',
  105. ),
  106. 'mailpoet' => array(
  107. 'type' => 'autoresponder',
  108. 'name' => 'MailPoet',
  109. 'class' => 'FLBuilderServiceMailPoet',
  110. ),
  111. 'mailrelay' => array(
  112. 'type' => 'autoresponder',
  113. 'name' => 'Mailrelay',
  114. 'class' => 'FLBuilderServiceMailrelay',
  115. ),
  116. 'mautic' => array(
  117. 'type' => 'autoresponder',
  118. 'name' => 'Mautic',
  119. 'class' => 'FLBuilderServiceMautic',
  120. ),
  121. 'ontraport' => array(
  122. 'type' => 'autoresponder',
  123. 'name' => 'Ontraport',
  124. 'class' => 'FLBuilderServiceOntraport',
  125. 'namespace' => true,
  126. ),
  127. 'sendinblue' => array(
  128. 'type' => 'autoresponder',
  129. 'name' => 'SendinBlue',
  130. 'class' => 'FLBuilderServiceSendinBlue',
  131. ),
  132. 'sendy' => array(
  133. 'type' => 'autoresponder',
  134. 'name' => 'Sendy',
  135. 'class' => 'FLBuilderServiceSendy',
  136. 'namespace' => true,
  137. ),
  138. );
  139. /**
  140. * Get an array of services data of a certain type such as "autoresponder".
  141. * If no type is specified, all services will be returned.
  142. *
  143. * @since 1.5.4
  144. * @param string $type The type of service data to return.
  145. * @return array An array of services and related data.
  146. */
  147. static public function get_services_data( $type = null ) {
  148. $services = array();
  149. // Return all services.
  150. if ( ! $type ) {
  151. $services = self::$services_data;
  152. } else {
  153. foreach ( self::$services_data as $key => $service ) {
  154. if ( $service['type'] == $type ) {
  155. $services[ $key ] = $service;
  156. }
  157. }
  158. }
  159. // Remove services that use namespaces if we're not on a supported PHP version.
  160. foreach ( $services as $key => $service ) {
  161. if ( isset( $service['namespace'] ) && ! version_compare( phpversion(), '5.3', '>=' ) ) {
  162. unset( $services[ $key ] );
  163. }
  164. }
  165. // Remove services that don't meet the requirements.
  166. if ( isset( $services['mailpoet'] )
  167. && ! class_exists( 'WYSIJA' )
  168. && ( ! defined( 'MAILPOET_INITIALIZED' ) || ( defined( 'MAILPOET_INITIALIZED' ) && false === MAILPOET_INITIALIZED ) )
  169. ) {
  170. unset( $services['mailpoet'] );
  171. }
  172. return $services;
  173. }
  174. /**
  175. * Get an instance of a service helper class.
  176. *
  177. * @since 1.5.4
  178. * @param string $type The type of service.
  179. * @return object
  180. */
  181. static public function get_service_instance( $service ) {
  182. $services = self::get_services_data();
  183. $data = $services[ $service ];
  184. // Make sure the base class is loaded.
  185. if ( ! class_exists( 'FLBuilderService' ) ) {
  186. require_once FL_BUILDER_DIR . 'classes/class-fl-builder-service.php';
  187. }
  188. // Make sure the service class is loaded.
  189. if ( ! class_exists( $data['class'] ) ) {
  190. require_once FL_BUILDER_DIR . 'classes/class-fl-builder-service-' . $service . '.php';
  191. }
  192. return new $data['class']();
  193. }
  194. /**
  195. * Save the API connection of a service and retrieve account settings markup.
  196. *
  197. * Called via the connect_service frontend AJAX action.
  198. *
  199. * @since 1.5.4
  200. * @return array The response array.
  201. */
  202. static public function connect_service() {
  203. $saved_services = FLBuilderModel::get_services();
  204. $post_data = FLBuilderModel::get_post_data();
  205. $response = array(
  206. 'error' => false,
  207. 'html' => '',
  208. );
  209. // Validate the service data.
  210. if ( ! isset( $post_data['service'] ) || empty( $post_data['service'] ) ) {
  211. $response['error'] = _x( 'Error: Missing service type.', 'Third party service such as MailChimp.', 'fl-builder' );
  212. } elseif ( ! isset( $post_data['fields'] ) || 0 === count( $post_data['fields'] ) ) {
  213. $response['error'] = _x( 'Error: Missing service data.', 'Connection data such as an API key.', 'fl-builder' );
  214. } elseif ( ! isset( $post_data['fields']['service_account'] ) || empty( $post_data['fields']['service_account'] ) ) {
  215. $response['error'] = _x( 'Error: Missing account name.', 'Account name for a third party service such as MailChimp.', 'fl-builder' );
  216. }
  217. // Get the service data.
  218. $service = $post_data['service'];
  219. $service_account = $post_data['fields']['service_account'];
  220. // Does this account already exist?
  221. if ( isset( $saved_services[ $service ][ $service_account ] ) ) {
  222. $response['error'] = _x( 'Error: An account with that name already exists.', 'Account name for a third party service such as MailChimp.', 'fl-builder' );
  223. }
  224. // Try to connect to the service.
  225. if ( ! $response['error'] ) {
  226. $instance = self::get_service_instance( $service );
  227. $connection = $instance->connect( $post_data['fields'] );
  228. if ( $connection['error'] ) {
  229. $response['error'] = $connection['error'];
  230. } else {
  231. FLBuilderModel::update_services(
  232. $service,
  233. $service_account,
  234. $connection['data']
  235. );
  236. $response['html'] = self::render_account_settings( $service, $service_account );
  237. }
  238. }
  239. // Return the response.
  240. return $response;
  241. }
  242. /**
  243. * Render the connection settings or account settings for a service.
  244. *
  245. * Called via the render_service_settings frontend AJAX action.
  246. *
  247. * @since 1.5.4
  248. * @return array The response array.
  249. */
  250. static public function render_settings() {
  251. $post_data = FLBuilderModel::get_post_data();
  252. $saved_services = FLBuilderModel::get_services();
  253. $module = FLBuilderModel::get_module( $post_data['node_id'] );
  254. $settings = $module->settings;
  255. $service = $post_data['service'];
  256. $response = array(
  257. 'error' => false,
  258. 'html' => '',
  259. );
  260. // Render the settings to connect a new account.
  261. if ( isset( $post_data['add_new'] ) || ! isset( $saved_services[ $service ] ) ) {
  262. $response['html'] = self::render_connect_settings( $service );
  263. } else {
  264. $account = isset( $settings->service_account ) ? $settings->service_account : '';
  265. $response['html'] = self::render_account_settings( $service, $account );
  266. }
  267. // Return the response.
  268. return $response;
  269. }
  270. /**
  271. * Render the settings to connect to a new account.
  272. *
  273. * @since 1.5.4
  274. * @return string The settings markup.
  275. */
  276. static public function render_connect_settings( $service ) {
  277. ob_start();
  278. FLBuilder::render_settings_field( 'service_account', array(
  279. 'row_class' => 'fl-builder-service-connect-row',
  280. 'class' => 'fl-builder-service-connect-input',
  281. 'type' => 'text',
  282. 'label' => __( 'Account Name', 'fl-builder' ),
  283. 'help' => __( 'Used to identify this connection within the accounts list and can be anything you like.', 'fl-builder' ),
  284. 'preview' => array(
  285. 'type' => 'none',
  286. ),
  287. ));
  288. $instance = self::get_service_instance( $service );
  289. echo $instance->render_connect_settings();
  290. FLBuilder::render_settings_field( 'service_connect_button', array(
  291. 'row_class' => 'fl-builder-service-connect-row',
  292. 'class' => 'fl-builder-service-connect-button',
  293. 'type' => 'button',
  294. 'label' => __( 'Connect', 'fl-builder' ),
  295. ));
  296. return ob_get_clean();
  297. }
  298. /**
  299. * Render the account settings for a saved connection.
  300. *
  301. * @since 1.5.4
  302. * @param string $service The service id such as "mailchimp".
  303. * @param string $active The name of the active account, if any.
  304. * @return string The account settings markup.
  305. */
  306. static public function render_account_settings( $service, $active = '' ) {
  307. ob_start();
  308. $saved_services = FLBuilderModel::get_services();
  309. $settings = new stdClass();
  310. $settings->service_account = $active;
  311. $options = array(
  312. '' => __( 'Choose...', 'fl-builder' ),
  313. );
  314. // Build the account select options.
  315. foreach ( $saved_services[ $service ] as $account => $data ) {
  316. $options[ $account ] = $account;
  317. }
  318. $options['add_new_account'] = __( 'Add Account...', 'fl-builder' );
  319. // Render the account select.
  320. FLBuilder::render_settings_field( 'service_account', array(
  321. 'row_class' => 'fl-builder-service-account-row',
  322. 'class' => 'fl-builder-service-account-select',
  323. 'type' => 'select',
  324. 'label' => __( 'Account', 'fl-builder' ),
  325. 'options' => $options,
  326. 'preview' => array(
  327. 'type' => 'none',
  328. ),
  329. ), $settings);
  330. // Render additional service fields if we have a saved account.
  331. if ( ! empty( $active ) && isset( $saved_services[ $service ][ $active ] ) ) {
  332. $post_data = FLBuilderModel::get_post_data();
  333. $module = FLBuilderModel::get_module( $post_data['node_id'] );
  334. $instance = self::get_service_instance( $service );
  335. $response = $instance->render_fields( $active, $module->settings );
  336. if ( ! $response['error'] ) {
  337. echo $response['html'];
  338. }
  339. }
  340. return ob_get_clean();
  341. }
  342. /**
  343. * Render the markup for service specific fields.
  344. *
  345. * Called via the render_service_fields frontend AJAX action.
  346. *
  347. * @since 1.5.4
  348. * @return array The response array.
  349. */
  350. static public function render_fields() {
  351. $post_data = FLBuilderModel::get_post_data();
  352. $module = FLBuilderModel::get_module( $post_data['node_id'] );
  353. $instance = self::get_service_instance( $post_data['service'] );
  354. $response = $instance->render_fields( $post_data['account'], $module->settings );
  355. return $response;
  356. }
  357. /**
  358. * Delete a saved account from the database.
  359. *
  360. * Called via the delete_service_account frontend AJAX action.
  361. *
  362. * @since 1.5.4
  363. * @return void
  364. */
  365. static public function delete_account() {
  366. $post_data = FLBuilderModel::get_post_data();
  367. if ( ! isset( $post_data['service'] ) || ! isset( $post_data['account'] ) ) {
  368. return;
  369. }
  370. FLBuilderModel::delete_service_account( $post_data['service'], $post_data['account'] );
  371. }
  372. }