Commit inicial - WordPress Análisis de Precios Unitarios

- WordPress core y plugins
- Tema Twenty Twenty-Four configurado
- Plugin allow-unfiltered-html.php simplificado
- .gitignore configurado para excluir wp-config.php y uploads

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
root
2025-11-03 21:04:30 -06:00
commit a22573bf0b
24068 changed files with 4993111 additions and 0 deletions

View File

@@ -0,0 +1,82 @@
<?php
/**
* Class BWFCRM_API_Create_API_Keys
*/
namespace BWFAN\Rest_API;
use WP_Error;
use WP_HTTP_Response;
use WP_REST_Response;
class Create_API_Keys extends \BWFCRM_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::CREATABLE;
$this->route = 'create-api-key/(?P<user_id>[\\d]+)';
}
/**
* @return WP_Error|WP_HTTP_Response|WP_REST_Response
* processing the api request
*/
public function process_api_call() {
$user_id = $this->get_sanitized_arg( 'user_id', 'text_field' );
$permission = $this->get_sanitized_arg( 'permission', 'text_field' );
$description = $this->get_sanitized_arg( 'description', 'text_field' );
$status = $this->get_sanitized_arg( 'status', 'text_field' );
$user_data = get_user_by( 'ID', $user_id );
if ( empty( $user_id ) || empty( $user_data ) ) {
return $this->error_response( __( 'Required Parameter Missing : User ID not provided or user with this id does not exist', 'wp-marketing-automations-pro' ), null, 400 );
}
if ( empty( $permission ) ) {
return $this->error_response( __( 'Required Parameter Missing : No permission provided', 'wp-marketing-automations-pro' ), null, 400 );
}
if ( strlen( $description ) > 200 ) {
return $this->error_response( __( 'Description must be no longer than 200 characters', 'wp-marketing-automations-pro' ), null, 400 );
}
// creating array of data
$data = [
'uid' => $user_id,
'api_key' => md5( $user_id . time() ),
'created_by' => get_current_user_id() ? get_current_user_id() : 0,
'created_at' => current_time( 'mysql', 1 ),
'permission' => $permission,
];
// storing description in the array if provided
if ( ! empty( $description ) ) {
$data['description'] = $description;
}
// storing status in the array if provided : 1 - active | 2 - revoked, default - 1
if ( ! empty( $status ) ) {
$data['status'] = $status;
}
$insert = \BWFAN_Rest_API_DB_Model::insert( $data );
if ( is_wp_error( $insert ) ) {
return $this->error_response( __( 'API key not created', 'wp-marketing-automations-pro' ), $insert, 500 );
}
return $this->success_response( $data, __( 'API key created successfully', 'wp-marketing-automations-pro' ) );
}
}
\BWFCRM_API_Loader::register( 'BWFAN\Rest_API\Create_API_Keys' );

View File

@@ -0,0 +1,62 @@
<?php
/**
* Class BWFCRM_API_Delete_API_Keys
*/
namespace BWFAN\Rest_API;
use WP_Error;
use WP_HTTP_Response;
use WP_REST_Response;
class Delete_API_Keys extends \BWFCRM_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::DELETABLE;
$this->route = '/delete-api-key/(?P<api_key_id>[\\d]+)';
}
/**
* @return WP_Error|WP_HTTP_Response|WP_REST_Response
* processing api request
*/
public function process_api_call() {
$api_key_id = $this->get_sanitized_arg( 'api_key_id', 'text_field' );
if ( empty( $api_key_id ) ) {
return $this->error_response( __( 'Required parameter missing : No API key ID provided', 'wp-marketing-automations-pro' ), null, 400 );
}
$data = [
'id' => $api_key_id,
];
// checking for api key existence by api key id
$api_key_exists = \BWFAN_Rest_API_DB_Model::fetch( array(), array( 'id' => $api_key_id ) );
if ( empty( $api_key_exists ) ) {
return $this->error_response( __( 'API key does not exist with ID #' . $api_key_id, 'wp-marketing-automations-pro' ), null, 400 );
}
$delete = \BWFAN_Rest_API_DB_Model::delete( $data );
if ( is_wp_error( $delete ) ) {
return $this->error_response( __( 'API key could not be deleted', 'wp-marketing-automations-pro' ), $delete, 500 );
}
return $this->success_response( $data, __( 'API key deleted successfully', 'wp-marketing-automations-pro' ) );
}
}
\BWFCRM_API_Loader::register( 'BWFAN\Rest_API\Delete_API_Keys' );

View File

@@ -0,0 +1,56 @@
<?php
/**
* Class BWFCRM_API_List_API_Keys_By_User_Id
*/
namespace BWFAN\Rest_API;
use WP_Error;
use WP_HTTP_Response;
use WP_REST_Response;
class List_API_Keys_By_User_Id extends \BWFCRM_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::READABLE;
$this->route = 'get-api-keys-by-userid/(?P<user_id>[\\d]+)';
}
/**
* @return WP_Error|WP_HTTP_Response|WP_REST_Response
* processing the api request
*/
public function process_api_call() {
$limit = ! empty( $this->args['limit'] ) ? absint( $this->args['limit'] ) : 0;
$offset = ! empty( $this->args['offset'] ) ? absint( $this->args['offset'] ) : 0;
$user_id = $this->get_sanitized_arg( 'user_id', 'text_field' );
// checking for user existence
$user_data = get_user_by( 'ID', $user_id );
if ( empty( $user_id ) || empty( $user_data ) ) {
return $this->error_response( __( 'Required parameter missing : No user id provided or user with this id does not exist', 'wp-marketing-automations-pro' ), null, 400 );
}
$data = array( 'uid' => $user_id );
$fetch = \BWFAN_Rest_API_DB_Model::fetch( array(), $data, $limit, $offset );
if ( is_wp_error( $fetch ) ) {
return $this->error_response( __( 'API key listing failed', 'wp-marketing-automations-pro' ), $fetch, 500 );
}
return $this->success_response( $fetch, __( 'API key listed successfully', 'wp-marketing-automations-pro' ) );
}
}
\BWFCRM_API_Loader::register( 'BWFAN\Rest_API\List_API_Keys_By_User_Id' );

View File

@@ -0,0 +1,62 @@
<?php
/**
* Class BWFCRM_API_List_API_Keys
*/
namespace BWFAN\Rest_API;
class List_API_Keys extends \BWFCRM_API_Base {
public static $ins;
public $total_count = 0;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::READABLE;
$this->route = '/get-api-keys';
}
/**
* @return \WP_Error|\WP_HTTP_Response|\WP_REST_Response
* prcessing the api request
*/
public function process_api_call() {
$limit = ! empty( $this->args['limit'] ) ? absint( $this->args['limit'] ) : 30;
$offset = ! empty( $this->args['offset'] ) ? absint( $this->args['offset'] ) : 0;
$search = ! empty( $this->args['search'] ) ? $this->args['search'] : '';
$fetch = \BWFAN_Rest_API_DB_Model::fetch( array(), array(), $limit, $offset, $search, true );
if ( is_wp_error( $fetch ) ) {
return $this->error_response( __( 'API key could not be listed', 'wp-marketing-automations-pro' ), $fetch, 500 );
}
$this->total_count = isset( $fetch['total'] ) ? intval( $fetch['total'] ) : 0;
$data = isset( $fetch['data'] ) ? $fetch['data'] : [];
// formatted returned data
$data = array_map( function ( $data ) {
if ( isset( $data['api_key'] ) ) {
$data['api_key'] = 'xxxx' . substr( $data['api_key'], - 6 );
}
return $data;
}, $data );
return $this->success_response( $data, __( 'API key listed successful', 'wp-marketing-automations-pro' ) );
}
public function get_result_total_count() {
return $this->total_count;
}
}
\BWFCRM_API_Loader::register( 'BWFAN\Rest_API\List_API_Keys' );

View File

@@ -0,0 +1,86 @@
<?php
/**
* Class BWFCRM_API_Update_API_Keys
*/
namespace BWFAN\Rest_API;
use WP_Error;
use WP_HTTP_Response;
use WP_REST_Response;
class Update_API_Keys extends \BWFCRM_API_Base {
public static $ins;
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::CREATABLE;
$this->route = '/update-api-key/(?P<api_key_id>[\\d]+)';
}
/**
* @return WP_Error|WP_HTTP_Response|WP_REST_Response
* processing the api request
*/
public function process_api_call() {
$api_key_id = $this->get_sanitized_arg( 'api_key_id', 'text_field' );
$permission = $this->get_sanitized_arg( 'permission', 'text_field' );
$description = $this->get_sanitized_arg( 'description', 'text_field' );
$status = $this->get_sanitized_arg( 'status', 'text_field' );
$update_api_key = $this->get_sanitized_arg( 'update_api_key', 'bool' );
if ( empty( $api_key_id ) ) {
return $this->error_response( __( 'Required parameter missing : No api key ID provided', 'wp-marketing-automations-pro' ), null, 400 );
}
if ( strlen( $description ) > 200 ) {
return $this->error_response( __( 'Description must be no longer than 200 characters', 'wp-marketing-automations-pro' ), null, 400 );
}
// checking for api key existence
$api_key_exists = \BWFAN_Rest_API_DB_Model::fetch( array(), array( 'id' => $api_key_id ) );
if ( empty( $api_key_exists ) ) {
return $this->error_response( __( 'API key does not exist with ID #' . $api_key_id, 'wp-marketing-automations-pro' ), null, 400 );
}
$where = array( 'id' => $api_key_id );
$data = [
'created_at' => current_time( 'mysql', 1 ),
];
// generate new api key, if opted to change the api key
if ( ! empty( $update_api_key ) && true === $update_api_key ) {
$data['api_key'] = md5( $api_key_exists[0]['uid'] . time() );
}
if ( ! empty( $permission ) ) {
$data['permission'] = $permission;
}
if ( ! empty( $description ) ) {
$data['description'] = $description;
}
if ( ! empty( $status ) ) {
$data['status'] = $status;
}
$update = \BWFAN_Rest_API_DB_Model::update( $data, $where );
if ( is_wp_error( $update ) ) {
return $this->error_response( __( 'API key update failed', 'wp-marketing-automations-pro' ), $update, 500 );
}
return $this->success_response( $data, __( 'API key updated successfully', 'wp-marketing-automations-pro' ) );
}
}
\BWFCRM_API_Loader::register( 'BWFAN\Rest_API\Update_API_Keys' );

View File

@@ -0,0 +1,96 @@
<?php
/**
* Class BWFAN_Rest_API_Add_Contact
*/
namespace BWFAN\Rest_API;
class Add_Contact extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::CREATABLE;
$this->route = '/contact/add';
$this->required = [ 'email' ];
$this->validate = [ 'contact_already_exists' ];
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* @param \BWFCRM_Contact $contact
*
* @return mixed
*/
public function process_api_call( $contact = '' ) {
$result = array();
$email = $this->get_sanitized_arg( 'email', 'email' );
if ( ! is_email( $email ) ) {
$this->error_response( null, 422, 'email_invalid' );
}
$fields = isset( $this->args['fields'] ) && ! empty( $this->args['fields'] ) ? $this->args['fields'] : array();
$stop_automation = isset( $this->args['stop_automation'] ) ? $this->args['stop_automation'] : false;
// creating array of data
$params = array(
'f_name' => isset( $this->args['f_name'] ) ? $this->get_sanitized_arg( 'f_name', 'text_field', $this->args['f_name'] ) : '',
'l_name' => isset( $this->args['l_name'] ) ? $this->get_sanitized_arg( 'l_name', 'text_field', $this->args['l_name'] ) : '',
'country' => isset( $this->args['country'] ) ? $this->get_sanitized_arg( 'country', 'text_field', $this->args['country'] ) : '',
'state' => isset( $this->args['state'] ) ? $this->get_sanitized_arg( 'state', 'text_field', $this->args['state'] ) : '',
'source' => isset( $this->args['source'] ) ? $this->get_sanitized_arg( 'source', 'text_field', $this->args['source'] ) : 'public_api',
'contact_no' => isset( $this->args['contact_no'] ) ? $this->get_sanitized_arg( 'contact_no', 'text_field', $this->args['contact_no'] ) : '',
);
if ( ! empty( $fields ) ) {
// check and remove the fields which does not exists
$field_data = \BWFAN_Rest_API_Common::remove_invalid_fields( $fields );
// store fields which does not exists
if ( ! empty( $field_data['field_not_exists'] ) ) {
$result['field_not_exists'] = $field_data['field_not_exists'];
}
$params = array_replace( $params, $field_data['fields'] );
}
$contact = \BWFAN_Rest_API_Common::create_contact( $email, $params, true );
// setting tags if provided
if ( isset( $this->args['tags'] ) ) {
$tag_ids = is_array( $this->args['tags'] ) ? $this->args['tags'] : array( $this->args['tags'] );
$contact->set_tags_v2( $tag_ids, $stop_automation );
}
// setting lists if provided
if ( isset( $this->args['lists'] ) ) {
$list_ids = is_array( $this->args['lists'] ) ? $this->args['lists'] : array( $this->args['lists'] );
$contact->set_lists_v2( $list_ids, $stop_automation );
}
// saving contact
$contact->save();
// return if not able to create or some error occurred
if ( ! $contact->is_contact_exists() ) {
$this->error_response( '', 422, 'unknown_error' );
}
// validating status if provided and updating it only when update contact is successful
if ( isset( $this->args['status'] ) ) {
\BWFAN_Rest_API_Common::change_contact_status( $contact, $this->args['status'] );
}
return $this->success_response( [ 'contact' => \BWFAN_Rest_API_Common::set_contact_status( $contact ) ], 'Contact created successfully' );
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Add_Contact' );

View File

@@ -0,0 +1,63 @@
<?php
/**
* Class BWFAN_Rest_API_Assign_List
*/
namespace BWFAN\Rest_API;
class Assign_List extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::EDITABLE;
$this->route = '/contact/list-assign/(?P<id>[\\d]+)';
$this->required = [ 'id', 'lists' ];
$this->validate = [ 'contact_not_exists' ];
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* @param \BWFCRM_Contact $contact
*
* @return mixed
*/
public function process_api_call( $contact = '' ) {
$lists = isset( $this->args['lists'] ) ? $this->args['lists'] : array();
$stop_automation = isset( $this->args['stop_automation'] ) ? (bool) $this->args['stop_automation'] : false;
if ( ! is_array( $lists ) ) {
$this->error_response( null, 422, 'unprocessable_entity' );
}
$list_data = \BWFCRM_Lists::get_lists( $lists );
if ( empty( $list_data ) ) {
$this->error_response( 'List(s) not found', 422, 'unprocessable_entity' );
}
$old_lists = $contact->get_lists();
$list_ids = array_column( $list_data, 'ID' );
$assign_lists = $contact->set_lists_v2( $list_ids, $stop_automation );
$contact->save();
if ( ! is_array( $assign_lists ) ) {
$this->error_response( 'No list(s) to update', 422, 'unprocessable_entity' );
}
if ( is_wp_error( $assign_lists ) ) {
$this->error_response( $assign_lists->get_error_messages(), 422, 'unknown_error' );
}
$newly_assigned_lists = array_diff( $assign_lists, $old_lists );
$list_results = \BWFCRM_Lists::get_lists( $newly_assigned_lists );
return $this->success_response( [ 'lists' => $list_results ], __( 'List(s) assigned successfully', 'wp-marketing-automations-pro' ) );
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Assign_List' );

View File

@@ -0,0 +1,62 @@
<?php
/**
* Class BWFAN_Rest_API_Assign_Tag
*/
namespace BWFAN\Rest_API;
class Assign_Tag extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::EDITABLE;
$this->route = '/contact/tag-assign/(?P<id>[\\d]+)';
$this->required = [ 'id', 'tags' ];
$this->validate = [ 'contact_not_exists' ];
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* @param \BWFCRM_Contact $contact
*
* @return mixed|void
*/
public function process_api_call( $contact = '' ) {
$tags = isset( $this->args['tags'] ) ? $this->args['tags'] : array();
$stop_automation = isset( $this->args['stop_automation'] ) ? $this->args['stop_automation'] : false;
if ( ! is_array( $tags ) ) {
$this->error_response( null, 422, 'unprocessable_entity' );
}
$tag_data = \BWFCRM_Tag::get_tags( $tags );
if ( empty( $tag_data ) ) {
$this->error_response( 'Tag(s) not found', 422, 'unprocessable_entity' );
}
$tag_ids = array_column( $tag_data, 'ID' );
$old_tags = $contact->get_tags();
$assign_tags = $contact->set_tags_v2( $tag_ids, $stop_automation );
$contact->save();
if ( ! is_array( $assign_tags ) ) {
$this->error_response( 'No tag(s) to update', 422, 'unprocessable_entity' );
}
if ( is_wp_error( $assign_tags ) ) {
$this->error_response( $assign_tags->get_error_messages(), 422, 'unknown_error' );
}
$newly_assigned_tags = array_diff( $assign_tags, $old_tags );
$tag_results = \BWFCRM_Tag::get_tags( $newly_assigned_tags );
return $this->success_response( [ 'tags' => $tag_results ], __( 'Tag(s) assigned successfully', 'wp-marketing-automations-pro' ) );
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Assign_Tag' );

View File

@@ -0,0 +1,52 @@
<?php
/**
* Class BWFAN_Rest_API_Change_Status
*/
namespace BWFAN\Rest_API;
class Change_Status extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::EDITABLE;
$this->route = '/contact/change-status/(?P<id>[\\d]+)';
$this->required = [ 'id', 'status' ];
$this->validate = [ 'contact_not_exists' ];
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* @param \BWFCRM_Contact $contact
*
* @return mixed|void
*/
public function process_api_call( $contact = '' ) {
$status = $this->get_sanitized_arg( 'status', 'text_field' );
$status = ! empty( $status ) ? strtolower( $status ) : $status;
$allowed_statuses = [ 'unverified', 'subscribed', 'bounced', 'unsubscribed', 'softbounced', 'complaint' ];
/** Validate the status */
if ( ! in_array( $status, $allowed_statuses ) ) {
$this->error_response( sprintf( __( 'Invalid status. Allowed values are: %s', 'wp-marketing-automations-pro' ), implode( ', ', $allowed_statuses ) ), 422, 'invalid_status' );
}
$result = \BWFAN_Rest_API_Common::change_contact_status( $contact, $status );
if ( ! $result ) {
$this->error_response( '', 422, 'unknown_error' );
}
return $this->success_response( [ 'contact' => \BWFAN_Rest_API_Common::get_formatted_contact_data( $contact ) ], __( "Status changed successfully", "autonami-automations-pro" ) );
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Change_Status' );

View File

@@ -0,0 +1,39 @@
<?php
/**
* Class BWFAN_Rest_API_Delete_Contact
*/
namespace BWFAN\Rest_API;
class Delete_Contact extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::DELETABLE;
$this->route = '/contact/(?P<id>[\\d]+)';
$this->required = [ 'id' ];
$this->validate = [ 'contact_not_exists' ];
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function process_api_call() {
$id = $this->get_sanitized_arg( 'id', 'text_field' );
$delete_contact = \BWFCRM_Model_Contact::delete_contact( $id );
if ( ! $delete_contact ) {
$this->error_response( '', 422, 'unknown_error' );
}
return $this->success_response( [], __( 'Contact deleted successfully', 'wp-marketing-automations-pro' ) );
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Delete_Contact' );

View File

@@ -0,0 +1,44 @@
<?php
/**
* Class BWFAN_Rest_API_Get_Contact_By_Id_Or_Email
*/
namespace BWFAN\Rest_API;
class Get_Contact_By_Id_Or_Email extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::READABLE;
$this->route = '/contact';
$this->validate = [ 'contact_not_exists' ];
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* /**
* @param \BWFCRM_Contact $contact
*
* @return mixed
*/
public function process_api_call( $contact = '' ) {
$id = $this->get_sanitized_arg( 'id', 'text_field' );
$email = $this->get_sanitized_arg( 'email', 'email' );
$search_by = empty( $id ) ? $email : $id;
// either id or email should be present
if ( empty( $search_by ) ) {
$this->error_response( null, 400, 'required_fields_missing' );
}
return $this->success_response( [ 'contact' => \BWFAN_Rest_API_Common::get_formatted_contact_data( $contact ) ], __( 'Contact fetched successfully', 'wp-marketing-automations-pro' ) ); }
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Get_Contact_By_Id_Or_Email' );

View File

@@ -0,0 +1,154 @@
<?php
/**
* Class BWFAN_Rest_API_Get_Contacts
*/
namespace BWFAN\Rest_API;
class Get_Contacts extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::READABLE;
$this->route = '/contacts';
add_filter( 'bwfan_contact_sql_final_where_query', [ $this, 'bwfan_public_api_contact_where_sql' ], 10, 1 );
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function process_api_call() {
$email = $this->get_sanitized_arg( 'email', 'email' );
$limit = isset( $this->args['limit'] ) ? $this->args['limit'] : 30;
$offset = isset( $this->args['offset'] ) ? $this->args['offset'] : 0;
$sort_order = isset( $this->args['sort_order'] ) ? $this->args['sort_order'] : 'DESC';
$sort_field = isset( $this->args['sort_field'] ) ? $this->args['sort_field'] : 'creation_date';
$status = isset( $this->args['status'] ) ? $this->args['status'] : '';
// setting filters statically as only 'status' is being used to filter the contact
$filters = array();
if ( ! empty( $status ) ) {
//formatting the filters
$filters['c'][] = array(
'key' => 'status',
'rule' => 'is',
'type' => \BWFCRM_Filters::$TYPE_NUMBER_EXACT,
'value' => $status
);
}
// if not provided, then taking the defaults
$additional_info = array( 'order_by' => $sort_field, 'order' => $sort_order, 'grab_totals' => true );
$contact_data = \BWFCRM_Model_Contact::get_contacts( $email, $limit, $offset, $filters, $additional_info );
if ( empty( $contact_data ) ) {
$this->error_response( '', 422, 'data_not_found' );
}
/** Modify the status if contact email/phone is in unsubscribe table */
$unsubscribed = self::get_unsubscribed_contacts( $contact_data );
if ( ! empty( $unsubscribed ) ) {
$processed_contacts = array_map( function ( $contact ) use ( $unsubscribed ) {
$email = $contact['email'] ?? '';
$contact_no = $contact['contact_no'] ?? '';
if ( in_array( $email, $unsubscribed, true ) || in_array( $contact_no, $unsubscribed, true ) ) {
$contact['status'] = '3';
}
return $contact;
}, $contact_data['contacts'] );
$contact_data['contacts'] = $processed_contacts;
}
return $this->success_response( [ 'contact' => $contact_data ], __( 'Contact(s) listed successfully', 'wp-marketing-automations-pro' ) );
}
/**
* Get all un-subscriber
*
* @param $contact_data
*
* @return array
*/
public static function get_unsubscribed_contacts( $contact_data ) {
if ( ! isset( $contact_data['contacts'] ) || empty( $contact_data['contacts'] ) ) {
return [];
}
$emails = array_column( $contact_data['contacts'], 'email' );
$contact_nos = array_column( $contact_data['contacts'], 'contact_no' );
$recipients = array_filter( array_unique( array_merge( $emails, $contact_nos ) ) );
if ( empty( $recipients ) ) {
return [];
}
global $wpdb;
$placeholders = implode( ',', array_fill( 0, count( $recipients ), '%s' ) );
$query = " SELECT recipient FROM {$wpdb->prefix}bwfan_message_unsubscribe WHERE recipient IN ($placeholders)";
return $wpdb->get_col( $wpdb->prepare( $query, $recipients ) );//phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL
}
/**
* @param $filter_where
* appending apis filters to contacts where query
*
* @return mixed|string
*/
public function bwfan_public_api_contact_where_sql( $filter_where ) {
$from = isset( $this->args['from'] ) ? date( 'Y-m-d', strtotime( $this->args['from'] ) ) : '';
$to = isset( $this->args['to'] ) ? date( 'Y-m-d', strtotime( $this->args['to'] ) ) : '';
$updated_from = isset( $this->args['updated_from'] ) ? date( 'Y-m-d', strtotime( $this->args['updated_from'] ) ) : '';
$updated_to = isset( $this->args['updated_to'] ) ? date( 'Y-m-d', strtotime( $this->args['updated_to'] ) ) : '';
$tags = isset( $this->args['tag'] ) ? $this->args['tag'] : array();
$lists = isset( $this->args['list'] ) ? $this->args['list'] : array();
if ( ! empty( $from ) ) {
$filter_where .= "AND (c.creation_date >='$from')";
}
if ( ! empty( $to ) ) {
$filter_where .= "AND (c.creation_date <='$to')";
}
if ( ! empty( $updated_from ) ) {
$filter_where .= "AND (c.last_modified >'$updated_from')";
}
if ( ! empty( $updated_to ) ) {
$filter_where .= "AND (c.last_modified <'$updated_to')";
}
// formatting tags with 'like' and 'or' operator
if ( ! empty( $tags ) ) {
$tags = array_map( function ( $tag ) {
return "c.tags LIKE '%\"$tag\"%'";
}, $tags );
$tags = implode( ' OR ', $tags );
$filter_where .= "AND ($tags)";
}
// formatting list with 'like' and 'or' operator
if ( ! empty( $lists ) ) {
$lists = array_map( function ( $list ) {
return "c.lists LIKE '%\"$list\"%'";
}, $lists );
$lists = implode( ' OR ', $lists );
$filter_where .= "AND ($lists)";
}
return $filter_where;
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Get_Contacts' );

View File

@@ -0,0 +1,55 @@
<?php
/**
* Class BWFAN_Rest_API_Unassign_List
*/
namespace BWFAN\Rest_API;
class Unassign_List extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::EDITABLE;
$this->route = '/contact/list-unassign/(?P<id>[\\d]+)';
$this->required = [ 'id', 'listId' ];
$this->validate = [ 'contact_not_exists' ];
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* @param \BWFCRM_Contact $contact
*
* @return mixed
*/
public function process_api_call( $contact = '' ) {
$lists = isset( $this->args['listId'] ) ? $this->args['listId'] : array();
if ( ! is_array( $lists ) ) {
$this->error_response( '', 422, 'unprocessable_entity' );
}
$unassign_lists = $contact->remove_lists( $lists );
$contact->save();
if ( empty( $unassign_lists ) ) {
$this->error_response( 'No list to unassign', 422, 'unprocessable_entity' );
}
if ( is_wp_error( $unassign_lists ) ) {
$this->error_response( $unassign_lists->get_error_messages(), 422, 'unknown_error' );
}
$unassign_lists = \BWFCRM_Lists::get_lists( $unassign_lists );
return $this->success_response( [ 'lists' => $unassign_lists ], __( 'List(s) unassigned successfully', 'wp-marketing-automations-pro' ) );
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Unassign_List' );

View File

@@ -0,0 +1,55 @@
<?php
/**
* Class BWFAN_Rest_API_Unassign_Tag
*/
namespace BWFAN\Rest_API;
class Unassign_Tag extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::EDITABLE;
$this->route = '/contact/tag-unassign/(?P<id>[\\d]+)';
$this->required = [ 'id', 'tagId' ];
$this->validate = [ 'contact_not_exists' ];
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* @param \BWFCRM_Contact $contact
*
* @return mixed
*/
public function process_api_call( $contact = '' ) {
$tags = isset( $this->args['tagId'] ) ? $this->args['tagId'] : array();
if ( ! is_array( $tags ) ) {
$this->error_response( '', 422, 'unprocessable_entity' );
}
$tags_data = $contact->remove_tags( $tags );
$contact->save();
if ( empty( $tags_data ) ) {
$this->error_response( 'No tag to unassign', 422, 'unprocessable_entity' );
}
if ( is_wp_error( $tags_data ) ) {
$this->error_response( $tags_data->get_error_messages(), 422, 'unknown_error' );
}
$unassign_tags = \BWFCRM_Tag::get_tags( $tags_data );
return $this->success_response( [ 'tags' => $unassign_tags ], __( 'Tag(s) unassigned successfully', 'wp-marketing-automations-pro' ) );
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Unassign_Tag' );

View File

@@ -0,0 +1,114 @@
<?php
/**
* Class BWFAN_Rest_API_Update_Contact
*/
namespace BWFAN\Rest_API;
class Update_Contact extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::EDITABLE;
$this->route = '/contact/update/(?P<id>[\\d]+)';
$this->required = [ 'id' ];
$this->validate = [ 'contact_not_exists' ];
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* @param \BWFCRM_Contact $contact
*
* @return mixed
*/
public function process_api_call( $contact = '' ) {
$id = $this->get_sanitized_arg( 'id', 'text_field' );
/** creating array of data */
$params = array(
'id' => $id,
'f_name' => isset( $this->args['f_name'] ) ? $this->get_sanitized_arg( 'f_name', 'text_field', $this->args['f_name'] ) : '',
'l_name' => isset( $this->args['l_name'] ) ? $this->get_sanitized_arg( 'l_name', 'text_field', $this->args['l_name'] ) : '',
'country' => isset( $this->args['country'] ) ? $this->get_sanitized_arg( 'country', 'text_field', $this->args['country'] ) : '',
'state' => isset( $this->args['state'] ) ? $this->get_sanitized_arg( 'state', 'text_field', $this->args['state'] ) : '',
'source' => isset( $this->args['source'] ) ? $this->get_sanitized_arg( 'source', 'text_field', $this->args['source'] ) : '',
'contact_no' => isset( $this->args['contact_no'] ) ? $this->get_sanitized_arg( 'contact_no', 'text_field', $this->args['contact_no'] ) : '',
);
/** Validating email if provided */
if ( isset( $this->args['email'] ) ) {
$email = $this->get_sanitized_arg( 'email', 'email' );
if ( ! is_email( $email ) ) {
$this->error_response( null, 422, 'email_invalid' );
}
/** Checking if contact already exist */
if ( ! empty( \BWFAN_Rest_API_Common::is_contact_exists( $email ) ) ) {
$this->error_response( null, 422, 'already_exists' );
}
$params['email'] = $email;
}
/** Setting fields if provided */
$result = array();
$fields = isset( $this->args['fields'] ) && ! empty( $this->args['fields'] ) ? $this->args['fields'] : array();
if ( ! empty( $fields ) ) {
$field_data = \BWFAN_Rest_API_Common::remove_invalid_fields( $fields );
if ( ! empty( $field_data['field_not_exists'] ) ) {
$result['field_not_exists'] = $field_data['field_not_exists'];
}
if ( ! empty( $field_data['fields'] ) ) {
$contact->set_fields( $field_data['fields'] );
}
}
/** Removing empty keys */
$params = array_filter( $params );
/** Setting tags if provided */
if ( isset( $this->args['tags'] ) && ! empty( $this->args['tags'] ) ) {
$tags = is_array( $this->args['tags'] ) ? $this->args['tags'] : array( $this->args['tags'] );
$tag_data = \BWFCRM_Tag::get_tags( $tags );
$tag_ids = array_column( $tag_data, 'ID' );
$contact->set_tags_v2( $tag_ids );
}
/** Setting lists if provided */
if ( isset( $this->args['lists'] ) && ! empty( $this->args['tags'] ) ) {
$lists = is_array( $this->args['lists'] ) ? $this->args['lists'] : array( $this->args['lists'] );
$lists_data = \BWFCRM_Lists::get_lists( $lists );
$list_ids = array_column( $lists_data, 'ID' );
$contact->set_lists_v2( $list_ids );
}
/** Updating contact by contact id */
$update_contact = $contact->update( $params );
if ( is_wp_error( $update_contact ) || ! $update_contact ) {
$this->error_response( '', 422, 'unknown_error' );
}
/** Validating status if provided and updating it only when update contact is successful */
if ( isset( $this->args['status'] ) ) {
\BWFAN_Rest_API_Common::change_contact_status( $contact, $this->args['status'] );
}
/** Re-fetching contact obj after updating */
$contact = new \BWFCRM_Contact( $contact->get_id() );
return $this->success_response( [ 'contact' => \BWFAN_Rest_API_Common::get_formatted_contact_data( $contact ) ], __( 'Contact updated successfully', 'wp-marketing-automations-pro' ) );
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Update_Contact' );

View File

@@ -0,0 +1,60 @@
<?php
/**
* Class BWFAN_Rest_API_Update_Email
*/
namespace BWFAN\Rest_API;
class Update_Email extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::EDITABLE;
$this->route = '/contact/update-email/(?P<id>[\\d]+)';
$this->required = [ 'id', 'email' ];
$this->validate = [ 'contact_not_exists' ];
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* @param \BWFCRM_Contact $contact
*
* @return mixed
*/
public function process_api_call( $contact = '' ) {
$id = $this->get_sanitized_arg( 'id', 'text_field' );
$email = $this->get_sanitized_arg( 'email', 'email' );
if ( ! is_email( $email ) ) {
$this->error_response( null, 422, ' email_invalid' );
}
// checking if contact already exits
if ( ! empty( \BWFAN_Rest_API_Common::is_contact_exists( $email ) ) ) {
$this->error_response( null, 422, 'already_exists' );
}
// updating email by contact id
$update_contact = $contact->set_data( array( 'email' => $email ) );
if ( is_wp_error( $update_contact ) ) {
$this->error_response( $update_contact->get_error_messages(), 422, 'unknown_error' );
}
$contact->save();
/** Re-fetching contact obj after updating */
$contact = new \BWFCRM_Contact( $contact->get_id() );
return $this->success_response( [ 'contact' => \BWFAN_Rest_API_Common::get_formatted_contact_data( $contact ) ], __( 'Email updated successfully', 'wp-marketing-automations-pro' ) );
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Update_Email' );

View File

@@ -0,0 +1,61 @@
<?php
/**
* Class BWFAN_Rest_API_Add_Field
*/
namespace BWFAN\Rest_API;
class Add_Field extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::CREATABLE;
$this->route = '/field/add';
$this->required = [ 'field_name', 'type' ];
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function process_api_call() {
// collecting data
$field_name = $this->get_sanitized_arg( 'field_name', 'text_field' );
// types : 1 - text | 2 - number | 3 - textarea | 4 - select | 5 - radio | 6 - checkbox | 7 - date
$type = $this->get_sanitized_arg( 'type', 'text_field' );
$group_id = $this->get_sanitized_arg( 'group_id', 'text_field' );
$mode = isset( $this->args['mode'] ) ? absint( $this->args['mode'] ) : 1;
$vmode = isset( $this->args['vmode'] ) ? absint( $this->args['vmode'] ) : 1;
$search = isset( $this->args['search'] ) ? absint( $this->args['search'] ) : 1;
// checking for group existence
$group_id = ! empty( $group_id ) && is_numeric( $group_id ) ? $group_id : 0;
$group = \BWFCRM_Group::get_groupby_id( $group_id );
if ( $group_id > 0 && empty( $group ) ) {
$this->error_response( null, 422, 'unprocessable_entity' );
}
$options = isset( $this->args['options'] ) && ! empty( $options ) && is_array( $options ) ? $options : [];
$placeholder = isset( $this->args['placeholder'] ) ? $this->args['placeholder'] : '';
$field = \BWFCRM_Fields::add_field( $field_name, $type, $options, $placeholder, $mode, $vmode, $search, $group_id );
if ( is_wp_error( $field ) ) {
$this->error_response( '', 422, 'already_exists' );
}
if ( empty( $field ) ) {
$this->error_response( '', 422, 'unknown_error' );
}
return $this->success_response( array( 'fields' => $field ), __( 'Field created successfully', 'wp-marketing-automations-pro' ) );
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Add_Field' );

View File

@@ -0,0 +1,48 @@
<?php
/**
* Class BWFAN_Rest_API_Delete_Field
*/
namespace BWFAN\Rest_API;
class Delete_Field extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::DELETABLE;
$this->route = '/field/(?P<id>[\\d]+)';
$this->required = [ 'id' ];
$this->validate = [ 'field_not_exists' ];
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function process_api_call() {
$field_id = $this->get_sanitized_arg( 'id', 'text_field' );
// fetching field detail before deleting to send it in api response
$deleting_field = \BWFAN_Model_Fields::get_field_by_id( $field_id );
$delete_field = \BWFCRM_Fields::delete_field( absint( $field_id ) );
if ( false === $delete_field ) {
$this->error_response( '', 422, 'unknown_error' );
}
$field = [
"ID" => $field_id,
"name" => $deleting_field['name']
];
return $this->success_response( [ 'fields' => $field ], __( 'Field deleted successfully', 'wp-marketing-automations-pro' ) );
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Delete_Field' );

View File

@@ -0,0 +1,40 @@
<?php
/**
* Class BWFAN_Rest_API_Get_All_Fields
*/
namespace BWFAN\Rest_API;
class Get_All_Fields extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::READABLE;
$this->route = '/fields';
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function process_api_call() {
// Explicitly set type to 1 for contact fields if not specified
$type = ! empty( $this->args['type'] ) ? $this->args['type'] : null;
$fields = \BWFCRM_Fields::get_custom_fields( '', 1, null, false, null, $type );
if ( empty( $fields ) ) {
$this->error_response( null, '404', 'data_not_found' );
}
return $this->success_response( array( 'fields' => $fields ), __( 'Fields listed successfully', 'wp-marketing-automations-pro' ) );
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Get_All_Fields' );

View File

@@ -0,0 +1,65 @@
<?php
/**
* Class BWFAN_Rest_API_Update_Field
*/
namespace BWFAN\Rest_API;
class Update_Field extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::EDITABLE;
$this->route = '/field/update/(?P<id>[\\d]+)';
$this->required = [ 'id', 'slug' ];
$this->validate = [ 'field_not_exists' ];
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function process_api_call() {
// collecting the data
$field_id = $this->get_sanitized_arg( 'id', 'text_field' );
$field_name = $this->get_sanitized_arg( 'field_name', 'text_field' );
$type = $this->get_sanitized_arg( 'type', 'text_field' );
$slug = $this->get_sanitized_arg( 'slug', 'text_field' );
$options = isset( $this->args['options'] ) ? $this->args['options'] : array();
$placeholder = $this->get_sanitized_arg( 'placeholder', 'text_field' );
$mode = $this->get_sanitized_arg( 'mode', 'text_field' );
$vmode = $this->get_sanitized_arg( 'vmode', 'text_field' );
$search = $this->get_sanitized_arg( 'search', 'text_field' );
$group_id = $this->get_sanitized_arg( 'group_id', 'text_field' );
$group_id = ! empty( $group_id ) && is_numeric( $group_id ) ? $group_id : false;
// checking for group existence if group id provided
$group = \BWFCRM_Group::get_groupby_id( $group_id );
if ( false !== $group_id && $group_id > 0 && empty( $group ) ) {
$this->error_response( null, 422, 'unprocessable_entity' );
}
$update_field = \BWFCRM_Fields::update_field( $field_id, $group_id, $field_name, $type, $options, $placeholder, $slug, $mode, $vmode, $search );
if ( $update_field['status'] === 404 ) {
$this->error_response( null, 422, 'already_exists' );
}
// fetching field detail before deleting to send it in api response
$updated_field = \BWFAN_Model_Fields::get_field_by_id( $field_id );
$field = [
"ID" => $field_id,
"name" => $updated_field['name']
];
return $this->success_response( [ 'fields' => $field ], __( 'Field updated successfully', 'wp-marketing-automations-pro' ) );
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Update_Field' );

View File

@@ -0,0 +1,66 @@
<?php
/**
* Class BWFAN_Rest_API_Add_List
*/
namespace BWFAN\Rest_API;
class Add_List extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::CREATABLE;
$this->route = '/list/add';
$this->required = [ 'lists' ];
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function process_api_call() {
// list names can be an array when creating more than one list or
// can be single text when creating only one list
$lists = isset( $this->args['lists'] ) ? $this->args['lists'] : array();
// adding id to list names
// array(array(id,value),array(id,value),...)
$list_data = array();
if ( is_array( $lists ) ) {
$list_data = array_map( function ( $list ) {
return array( 'id' => 0, 'value' => $list );
}, $lists );
} else {
$list_data['id'] = 0;
$list_data['value'] = $lists;
$list_data = array( $list_data );
}
$lists = \BWFCRM_Term::get_or_create_terms( $list_data, \BWFCRM_Term_Type::$LIST, true, true );
if ( is_wp_error( $lists ) ) {
$this->error_response( null, 422, 'unknown_error' );
}
// return if lists not created or not existing
if ( ! isset( $lists['existing'] ) || ! isset( $lists['created'] ) ) {
$this->error_response( null, 422, 'unknown_error' );
}
$created_lists = \BWFCRM_Term::get_collection_array( $lists['created'] );
// if not created, then provided list already exists
if ( empty( $created_lists ) ) {
$this->error_response( null, 422, 'already_exists' );
}
return $this->success_response( [ 'lists' => $created_lists ], __( 'New list created successfully.', 'wp-marketing-automations-pro' ) );
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Add_List' );

View File

@@ -0,0 +1,49 @@
<?php
/**
* Class BWFAN_Rest_API_Delete_List
*/
namespace BWFAN\Rest_API;
class Delete_List extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::DELETABLE;
$this->route = '/list/(?P<id>[\\d]+)';
$this->required = [ 'id' ];
$this->validate = [ 'list_not_exists' ];
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* @param \BWFCRM_Lists $list
*
* @return mixed
*/
public function process_api_call( $list = '' ) {
$list_id = $this->get_sanitized_arg( 'id', 'text_field' );
// storing list details before deleting to pass to api response
$deleted_list = [ 'ID' => $list_id, 'name' => $list->get_name() ];
$delete_list = \BWFCRM_Lists::delete_list( absint( $list_id ) );
if ( false === $delete_list ) {
$this->error_response( null, 422, 'unknown_error' );
}
return $this->success_response( [ 'lists' => $deleted_list ], __( 'List deleted successfully', 'wp-marketing-automations-pro' ) );
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Delete_List' );

View File

@@ -0,0 +1,46 @@
<?php
/**
* Class BWFAN_Rest_API_Get_All_Lists
*/
namespace BWFAN\Rest_API;
class Get_All_Lists extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::READABLE;
$this->route = '/lists';
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function process_api_call() {
$limit = ! empty( $this->args['limit'] ) ? absint( $this->args['limit'] ) : 30;
$offset = ! empty( $this->args['offset'] ) ? absint( $this->args['offset'] ) : 0;
$lists = \BWFCRM_Lists::get_lists( array(), '', $offset, $limit );
if ( empty( $lists ) ) {
$this->error_response( null, 404, 'data_not_found' );
}
// creating array of id and name to return in the api response
if ( is_array( $lists ) && ! empty( $lists ) ) {
$lists = array_map( function ( $list ) {
return array( 'ID' => $list['ID'], 'name' => $list['name'] );
}, $lists );
}
return $this->success_response( [ 'lists' => $lists ], 'Lists fetched successfully' );
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Get_All_Lists' );

View File

@@ -0,0 +1,65 @@
<?php
/**
* Class BWFAN_Rest_API_Update_List
*/
namespace BWFAN\Rest_API;
class Update_List extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::EDITABLE;
$this->route = '/list/update/(?P<id>[\\d]+)';
$this->required = [ 'id', 'list' ];
$this->validate = [ 'list_not_exists' ];
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* @param \BWFCRM_Lists $list
*
* @return mixed
*/
public function process_api_call( $list = '' ) {
$list_id = $this->get_sanitized_arg( 'id', 'key' );
$list_name = $this->get_sanitized_arg( 'list', 'text_field' );
$current_name = $list->get_name();
if ( $list_name === $current_name ) {
$updated_list = [ 'ID' => $list_id, 'name' => $list_name ];
return $this->success_response( [ 'lists' => $updated_list ], __( 'List updated successfully', 'wp-marketing-automations-pro' ) );
}
/** Check if list is already exists */
$already_exists = \BWFCRM_Lists::get_terms( \BWFCRM_Term_Type::$LIST, [], $list_name, 0, 0, ARRAY_A, 'exact' );
if ( ! empty( $already_exists ) ) {
$response = __( "List already exists with name: " . $list_name, 'wp-marketing-automations-pro' );
$this->error_response( $response, 422, 'unknown_error' );
}
// setting and update lists
$list->set_name( $list_name );
$updated = $list->save();
if ( empty( $updated ) || is_wp_error( $updated ) ) {
$this->error_response( null, 422, 'unknown_error' );
}
$updated_list = [ 'ID' => $list_id, 'name' => $list->get_name() ];
return $this->success_response( [ 'lists' => $updated_list ], __( 'List updated successfully', 'wp-marketing-automations-pro' ) );
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Update_List' );

View File

@@ -0,0 +1,66 @@
<?php
/**
* Class BWFAN_Rest_API_Add_Tags
*/
namespace BWFAN\Rest_API;
class Add_Tags extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::CREATABLE;
$this->route = '/tag/add';
$this->required = [ 'tags' ];
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function process_api_call() {
// tags can be an array of tag name or a string name
$tags = isset( $this->args['tags'] ) ? $this->args['tags'] : array();
// associating id with tag name
$tag_data = array();
if ( is_array( $tags ) ) {
$tag_data = array_map( function ( $tag ) {
return array( 'id' => 0, 'value' => $tag );
}, $tags );
} else {
$tag_data['id'] = 0;
$tag_data['value'] = $tags;
$tag_data = array( $tag_data );
}
$tags = \BWFCRM_Term::get_or_create_terms( $tag_data, \BWFCRM_Term_Type::$TAG, true, true );
if ( is_wp_error( $tags ) ) {
$this->error_response( null, 422, 'unknown_error' );
}
// return if tags not created or not existing
if ( ! isset( $tags['existing'] ) || ! isset( $tags['created'] ) ) {
$this->error_response( null, 422, 'unknown_error' );
}
//$existing_tags = \BWFCRM_Term::get_collection_array( $tags['existing'] );
$created_tags = \BWFCRM_Term::get_collection_array( $tags['created'] );
//$all_tags = array_merge( $created_tags, $existing_tags );
// return if tags not created, already existing
if ( empty( $created_tags ) ) {
$this->error_response( null, 422, 'already_exists' );
}
return $this->success_response( [ 'tags' => $created_tags ], __( 'Tag(s) created successfully', 'wp-marketing-automations-pro' ) );
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Add_Tags' );

View File

@@ -0,0 +1,48 @@
<?php
/**
* Class BWFAN_Rest_API_Delete_Tags
*/
namespace BWFAN\Rest_API;
class Delete_Tags extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::DELETABLE;
$this->route = '/tag/(?P<id>[\\d]+)';
$this->required = [ 'id' ];
$this->validate = [ 'tag_not_exists' ];
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* @param \BWFCRM_Tag $tag
*
* @return mixed
*/
public function process_api_call( $tag = '' ) {
$tag_id = $this->get_sanitized_arg( 'id', 'text_field' );
// storing tag details before deleting to pass to api response
$deleted_tag = [ 'ID' => $tag_id, 'name' => $tag->get_name() ];
$delete_tag = \BWFCRM_Tag::delete_tag( absint( $tag_id ) );
if ( false === $delete_tag ) {
$this->error_response( null, 422, 'unknown_error' );
}
return $this->success_response( [ 'tags' => $deleted_tag ], __( 'Tag deleted successfully', 'wp-marketing-automations-pro' ) );
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Delete_Tags' );

View File

@@ -0,0 +1,50 @@
<?php
/**
* Class BWFAN_Rest_API_Get_All_Tags
*/
namespace BWFAN\Rest_API;
class Get_All_Tags extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::READABLE;
$this->route = '/tags';
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
public function process_api_call() {
$limit = ! empty( $this->args['limit'] ) ? $this->args['limit'] : 30;
$offset = ! empty( $this->args['offset'] ) ? $this->args['offset'] : 0;
$tags = \BWFCRM_Tag::get_tags( array(), '', $offset, $limit );
if ( is_wp_error( $tags ) ) {
$this->error_response( null, 422, 'unknown_error' );
}
if ( empty( $tags ) ) {
$this->error_response( null, 404, 'data_not_found' );
}
// creating array of only id and name to return to api response
if ( is_array( $tags ) ) {
$tags = array_map( function ( $tag ) {
return array( 'ID' => $tag['ID'], 'name' => $tag['name'] );
}, $tags );
}
return $this->success_response( [ 'tags' => $tags ], __( 'Tag(s) Listed successfully', 'wp-marketing-automations-pro' ) );
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Get_All_Tags' );

View File

@@ -0,0 +1,64 @@
<?php
/**
* Class BWFAN_Rest_API_Update_Tags
*/
namespace BWFAN\Rest_API;
class Update_Tags extends \BWFAN_Rest_API_Base {
public static $ins;
public function __construct() {
parent::__construct();
$this->method = \WP_REST_Server::EDITABLE;
$this->route = '/tag/update/(?P<id>[\\d]+)';
$this->required = [ 'id', 'tag' ];
$this->validate = [ 'tag_not_exists' ];
}
public static function get_instance() {
if ( null === self::$ins ) {
self::$ins = new self();
}
return self::$ins;
}
/**
* @param \BWFCRM_Tag $tag
*
* @return mixed
*/
public function process_api_call( $tag = '' ) {
$tag_id = $this->get_sanitized_arg( 'id', 'text_field' );
$tag_name = $this->get_sanitized_arg( 'tag', 'text_field' );
$current_name = $tag->get_name();
if ( $tag_name === $current_name ) {
$updated_tag = [ 'ID' => $tag_id, 'name' => $tag_name ];
return $this->success_response( [ 'tags' => $updated_tag ], __( 'Tag updated successfully', 'wp-marketing-automations-pro' ) );
}
/** Check if list is already exists */
$already_exists = \BWFCRM_Tag::get_terms( \BWFCRM_Term_Type::$TAG, [], $tag_name, 0, 0, ARRAY_A, 'exact' );
if ( ! empty( $already_exists ) ) {
$response = __( "Tag already exists with name: " . $tag_name, 'wp-marketing-automations-pro' );
$this->error_response( $response, 422, 'unknown_error' );
}
$tag->set_name( $tag_name );
$update_tag = $tag->save();
if ( empty( $update_tag ) ) {
$this->error_response( null, 422, 'unknown_error' );
}
$updated_tag = [ 'ID' => $tag_id, 'name' => $tag->get_name() ];
return $this->success_response( [ 'tags' => $updated_tag ], __( 'Tag updated successfully', 'wp-marketing-automations-pro' ) );
}
}
\BWFAN_Rest_API_Loader::register( 'BWFAN\Rest_API\Update_Tags' );