Skip to content

Commit

Permalink
Merge pull request #985 from Codeinwp/feat/webhooks
Browse files Browse the repository at this point in the history
Add Webhooks integration
  • Loading branch information
selul authored Jul 12, 2024
2 parents 1232f1e + c5dd746 commit 47b59a3
Show file tree
Hide file tree
Showing 18 changed files with 1,122 additions and 31 deletions.
38 changes: 36 additions & 2 deletions assets/css/rop_core.css
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,33 @@ a.active {
border-left-color: #fff;
}

#rop_core .btn.btn-webhook {
border-color: #0071a6;
color: #fff;
background: #FFBC42;
}

#rop_core .btn.btn-webhook:focus {
box-shadow: 0 0 0 0.1rem rgba(0, 123, 181, 0.2);
}

#rop_core .btn.btn-webhook:is( :focus, :hover ) {
border-color: #006a9c;
color: #fff;
background: #0074ab;
}

#rop_core .btn.btn-webhook:is( :active, .active ) {
border-color: #005882;
color: #fff;
background: #006391;
}

#rop_core .btn.btn-webhook.loading::after {
border-bottom-color: #fff;
border-left-color: #fff;
}

#rop_core .btn.btn-sm {
height: 1.2rem;
padding: 0.05rem 0.3rem;
Expand Down Expand Up @@ -2849,14 +2876,17 @@ a.active {
cursor: default;
}

#rop_core .modal:target .modal-container,
#rop_core .modal.active .modal-container {
#rop_core :is( .modal:target, .modal.active ) .modal-container {
z-index: 99999;
max-width: 640px;
-webkit-animation: slide-down 0.2s ease 1;
animation: slide-down 0.2s ease 1;
}

#rop_core :is( .modal:target, .modal.active ) .modal-container:has(.webhook-headers) {
max-width: 960px;
}

#rop_core .modal.modal-sm .modal-container {
max-width: 320px;
}
Expand Down Expand Up @@ -4168,6 +4198,10 @@ a.active {
background-color: #bd081c;
}

.icon_box.webhook {
background-color: #FFBC42;;
}

#rop_core .avatar .avatar-icon {
border-radius: 50%;
background: #333;
Expand Down
4 changes: 2 additions & 2 deletions includes/admin/class-rop-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1268,8 +1268,8 @@ public function check_cron_status() {
* @access public
*/
public function rop_wp_cron_notice() {
//TODO - we need to rework this as the constant is not saying that cron is not working only that the default scheduling is, the user can still use server cron instead.
return;
// TODO - we need to rework this as the constant is not saying that cron is not working only that the default scheduling is, the user can still use server cron instead.
return;
if ( ! defined( 'DISABLE_WP_CRON' ) ) {
return;
}
Expand Down
27 changes: 27 additions & 0 deletions includes/admin/class-rop-global-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ class Rop_Global_Settings {
'active' => false,
'name' => 'Vk',
),
'webhook' => array(
'active' => false,
'name' => 'Webhook',
'two_step_sign_in' => true,
),
);

/**
Expand Down Expand Up @@ -327,6 +332,28 @@ class Rop_Global_Settings {
'utm_campaign_medium' => 'social',
'utm_campaign_name' => 'ReviveOldPost',
),
'webhook' => array(
'wpml_language' => '',
'post_content' => 'post_title',
'custom_meta_field' => '',
'maximum_length' => '1000',
'custom_text' => '',
'custom_text_pos' => 'beginning',
'include_link' => true,
'url_from_meta' => false,
'url_meta_key' => '',
'short_url' => false,
'short_url_service' => 'is.gd',
'hashtags' => 'no-hashtags',
'hashtags_length' => '200',
'hashtags_common' => '',
'hashtags_custom' => '',
'hashtags_randomize' => false,
'shortner_credentials' => array(),
'image' => false,
'utm_campaign_medium' => 'social',
'utm_campaign_name' => 'ReviveOldPost',
),
);

/**
Expand Down
104 changes: 102 additions & 2 deletions includes/admin/class-rop-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -896,8 +896,11 @@ function ( $value ) {
}
}
if ( ${$data['service'] . '_services'} ) {
/* @noinspection PhpUndefinedMethodInspection */
$url = ${$data['service'] . '_services'}->sign_in_url( $data );
if ( method_exists( ${$data['service'] . '_services'}, 'sign_in_url' ) ) {
$url = ${$data['service'] . '_services'}->sign_in_url( $data );
} else {
$url = '';
}
}
} catch ( Exception $exception ) {
// Service can't be built. Not found or otherwise. Maybe log this.
Expand Down Expand Up @@ -1320,4 +1323,101 @@ private function add_account_vk( $data ) {

return $this->response->to_array();
}

/**
* API method called to add Webhook account.
*
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) As it is called dynamically.
*
* @since 9.1.0
* @access private
*
* @param array $data Webhook account data.
*
* @return array
*/
private function add_account_webhook( $data ) {
$services = array();
$webhook_service = new Rop_Webhook_Service();
$model = new Rop_Services_Model();
$db = new Rop_Db_Upgrade();

if ( ! $webhook_service->add_webhook( $data ) ) {
$this->response->set_code( '422' )
->set_data( array() );

return $this->response->to_array();
}

$services[ $webhook_service->get_service_id() ] = $webhook_service->get_service();
$active_accounts = $webhook_service->get_service_active_accounts();

if ( ! empty( $services ) ) {
$model->add_authenticated_service( $services );
}

if ( ! empty( $active_accounts ) ) {
$db->migrate_schedule( $active_accounts );
$db->migrate_post_formats( $active_accounts );
} else {
$this->response->set_code( '500' )
->set_data( array() );

return $this->response->to_array();
}

$this->response->set_code( '200' )
->set_message( 'OK' )
->set_data( array() );

return $this->response->to_array();
}

/**
* API method called to edit Webhook account.
*
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) As it is called dynamically.
*
* @since 9.1.0
* @access private
*
* @param array $data Webhook account data.
*
* @return array
*/
private function edit_account_webhook( $data ) {
$webhook_service = new Rop_Webhook_Service();
$model = new Rop_Services_Model();

if ( ! $webhook_service->add_webhook( $data ) ) {
$this->response->set_code( '422' )
->set_data( array() );

return $this->response->to_array();
}

$service_id = ! empty( $data['service_id'] ) ? $data['service_id'] : '';
$authenticated_services = $model->get_authenticated_services();

if ( ! isset( $authenticated_services[ $service_id ] ) ) {
$this->response->set_code( '422' )
->set_data( array() );

return $this->response->to_array();
}

$authenticated_services[ $service_id ] = array_merge( $authenticated_services[ $service_id ], $webhook_service->get_service() );

$model->update_authenticated_services( $authenticated_services );

if ( ! empty( $data['active'] ) && ! empty( $data['full_id'] ) ) {
$model->add_active_accounts( array( $data['full_id'] ) );
}

$this->response->set_code( '200' )
->set_message( 'OK' )
->set_data( array() );

return $this->response->to_array();
}
}
2 changes: 1 addition & 1 deletion includes/admin/models/class-rop-scheduler-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ function ( $value ) use ( &$prev ) {
* @since 8.0.0
* @access public
*/
public function generate_upcoming_events( $base = 0, $account_id, $limit = 0 ) {
public function generate_upcoming_events( $base = 0, $account_id = null, $limit = 0 ) {

$schedule = $this->get_schedule( $account_id );
if ( empty( $schedule ) ) {
Expand Down
Loading

0 comments on commit 47b59a3

Please sign in to comment.