t be built. * * @since 8.0.0 * @access private * * @param array $data Data passed from the AJAX call. * * @return array */ private function get_service_sign_in_url( $data ) { $url = ''; $factory = new Rop_Services_Factory(); try { ${$data['service'] . '_services'} = $factory->build( $data['service'] ); if ( empty( $data['credentials'] ) ) { $authenticated_services = new Rop_Services_Model(); $service = $authenticated_services->get_authenticated_services( $data['service'] ); if ( ! empty( $service ) ) { $service = array_filter( $service, function ( $value ) { return ! empty( $value['public_credentials'] ); } ); $service = reset( $service ); if ( ! empty( $service['public_credentials'] ) ) { $data['credentials'] = array_combine( array_keys( $service['public_credentials'] ), wp_list_pluck( $service['public_credentials'], 'value' ) ); } } } if ( ${$data['service'] . '_services'} ) { 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. $log = new Rop_Logger(); $log->alert_error( 'The service "' . $data['service'] . '" can NOT be built or was not found' . $exception->getMessage() ); $url = ''; } return array( 'url' => $url ); } /** * API method called to retrieve the logs. * * @SuppressWarnings(PHPMD.UnusedPrivateMethod) As it is called dynamically. * * @since 8.0.0 * @access private * @return string */ private function get_log( $data ) { $log = new Rop_Logger(); if ( isset( $data['force'] ) ) { $log->clear_user_logs(); } $this->response->set_code( '200' ) ->set_data( $log->get_logs() ); return $this->response->to_array(); } /** * This will disable facebook domain check toast message. * * @param mixed $data The data. * * @return array */ private function fb_exception_toast( $data ) { update_option( 'rop_facebook_domain_toast', 'no' ); $this->response->set_code( '200' ) ->set_message( 'Facebook domain check toast new status is closed' ) ->set_data( array( 'display' => false ) ); return $this->response->to_array(); } /** * API method called to retrieve the logs for toast. * * @SuppressWarnings(PHPMD.UnusedPrivateMethod) As it is called dynamically. * * @since 8.4.2 * @access private * @return string */ private function get_toast( $data ) { $log = new Rop_Logger(); $this->response->set_code( '200' ) ->set_message( 'OK' ) ->set_data( $log->get_logs() ); $logs_response = $this->response->to_array(); $logs_data = $logs_response['data']; if ( ! empty( $logs_data ) ) { $custom_response = 0; // Is it a status alert? $is_status_logs_alert = $log->is_status_error_necessary( $logs_response ); // true | false // The logs will contain latest entry as first element first. reset( $logs_data ); // reset pointer to first element $latest_log_entry = current( $logs_data ); // fetch the latest log entry $logs_response['data'] = array(); // reset data // Making sure it contains the important attributes. if ( isset( $latest_log_entry['message'] ) && isset( $latest_log_entry['type'] ) ) { // fetch log entry data; $channel = $latest_log_entry['channel']; $type = $latest_log_entry['type']; $message = $latest_log_entry['message']; $time = (int) $latest_log_entry['time']; if ( 'error' === $type ) { // Not displaying anything if there's no issue $get_last_err_timestamp = (int) get_option( 'rop_toast', 0 ); // get the last error timestamp if ( $get_last_err_timestamp !== $time ) { // If the time does not match, then proceed further. // Check to see if the error needs to be "translated" $latest_log_entry['message'] = $log->translate_messages( $message ); $logs_response['data'][] = $latest_log_entry; // Add the timestamp of the error into DB to now show this alert multiple times. update_option( 'rop_toast', $time, 'no' ); $custom_response ++; } } } // We need to inform the user as there are many errors in the log // This will change the status to "Error (check logs)" if ( true === $is_status_logs_alert ) { $custom_response ++; $logs_response['data'][] = array( 'type' => 'status_error', 'message' => '', 'channel' => 'rop_logs', 'time' => Rop_Scheduler_Model::get_current_time(), ); } if ( ! empty( $custom_response ) ) { // return the current error return $logs_response; } } $logs_response['data'] = array(); return $logs_response; } /** * API method called to add Facebook pages via app. * * @SuppressWarnings(PHPMD.UnusedPrivateMethod) As it is called dynamically. * * @since ... * @access private * * @param array $data Facebook page data. * * @return array */ private function add_account_fb( $data ) { $services = array(); $active_accounts = array(); $facebook_service = new Rop_Facebook_Service(); $model = new Rop_Services_Model(); $db = new Rop_Db_Upgrade(); $facebook_service->add_account_with_app( $data ); $services[ $facebook_service->get_service_id() ] = $facebook_service->get_service(); $active_accounts = array_merge( $active_accounts, $facebook_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() ); $rop_facebook_via_rs_app_option = 'rop_facebook_via_rs_app'; if ( ! get_option( $rop_facebook_via_rs_app_option ) ) { add_option( $rop_facebook_via_rs_app_option, 'true', ' ', 'no' ); } else { update_option( $rop_facebook_via_rs_app_option, 'true' ); } return $this->response->to_array(); } /** * API method called to add Twitter pages via app. * * @SuppressWarnings(PHPMD.UnusedPrivateMethod) As it is called dynamically. * * @since 8.4.0 * @access private * * @param array $data Twitter account data. * * @return array */ private function add_account_tw( $data ) { $services = array(); $active_accounts = array(); $twitter_service = new Rop_Twitter_Service(); $model = new Rop_Services_Model(); $db = new Rop_Db_Upgrade(); if ( ! empty( $data['pages'] ) && ! empty( $data['pages']['credentials']['rop_auth_token'] ) ) { $twitter_service->add_account_from_rop_server( $data ); } else { $twitter_service->add_account_with_app( $data ); } $services[ $twitter_service->get_service_id() ] = $twitter_service->get_service(); $active_accounts = array_merge( $active_accounts, $twitter_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() ); $rop_twitter_via_rs_app_option = 'rop_twitter_via_rs_app'; if ( ! get_option( $rop_twitter_via_rs_app_option ) ) { add_option( $rop_twitter_via_rs_app_option, 'true', ' ', 'no' ); } else { update_option( $rop_twitter_via_rs_app_option, 'true' ); } return $this->response->to_array(); } /** * API method called to add Linkedin pages via app. * * @SuppressWarnings(PHPMD.UnusedPrivateMethod) As it is called dynamically. * * @since 8.5.0 * @access private * * @param array $data LinkedIn accounts data. * * @return array */ private function add_account_li( $data ) { $services = array(); $active_accounts = array(); $linkedin_service = new Rop_Linkedin_Service(); $model = new Rop_Services_Model(); $db = new Rop_Db_Upgrade(); $linkedin_service->add_account_with_app( $data ); $services[ $linkedin_service->get_service_id() ] = $linkedin_service->get_service(); $active_accounts = array_merge( $active_accounts, $linkedin_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() ); $rop_linkedin_via_rs_app_option = 'rop_linkedin_via_rs_app'; if ( ! get_option( $rop_linkedin_via_rs_app_option ) ) { add_option( $rop_linkedin_via_rs_app_option, 'true', ' ', 'no' ); } else { update_option( $rop_linkedin_via_rs_app_option, 'true' ); } return $this->response->to_array(); } /** * API method called to add Tumblr Blogs via app. * * @SuppressWarnings(PHPMD.UnusedPrivateMethod) As it is called dynamically. * * @since 8.5.7 * @access private * * @param array $data Tumblr accounts data. * * @return array */ private function add_account_tumblr( $data ) { $services = array(); $active_accounts = array(); $tumblr_service = new Rop_Tumblr_Service(); $model = new Rop_Services_Model(); $db = new Rop_Db_Upgrade(); $tumblr_service->add_account_with_app( $data ); $services[ $tumblr_service->get_service_id() ] = $tumblr_service->get_service(); $active_accounts = array_merge( $active_accounts, $tumblr_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() ); $rop_tumblr_via_rs_app_option = 'rop_tumblr_via_rs_app'; if ( ! get_option( $rop_tumblr_via_rs_app_option ) ) { add_option( $rop_tumblr_via_rs_app_option, 'true', ' ', 'no' ); } else { update_option( $rop_tumblr_via_rs_app_option, 'true' ); } return $this->response->to_array(); } /** * API method called to add Google My Business locations via app. * * @SuppressWarnings(PHPMD.UnusedPrivateMethod) As it is called dynamically. * * @since 8.5.9 * @access private * * @param array $data Google My Business accounts data. * * @return array */ private function add_account_gmb( $data ) { $services = array(); $active_accounts = array(); $gmb_service = new Rop_Gmb_Service(); $model = new Rop_Services_Model(); $db = new Rop_Db_Upgrade(); $gmb_service->add_account_with_app( $data ); $services[ $gmb_service->get_service_id() ] = $gmb_service->get_service(); $active_accounts = array_merge( $active_accounts, $gmb_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 add VK via app. * * @SuppressWarnings(PHPMD.UnusedPrivateMethod) As it is called dynamically. * * @since 8.6.1 * @access private * * @param array $data Vk accounts data. * * @return array */ private function add_account_vk( $data ) { $services = array(); $active_accounts = array(); $vk_service = new Rop_Vk_Service(); $model = new Rop_Services_Model(); $db = new Rop_Db_Upgrade(); $vk_service->add_account_with_app( $data ); $services[ $vk_service->get_service_id() ] = $vk_service->get_service(); $active_accounts = array_merge( $active_accounts, $vk_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 to call the license processor. * * @SuppressWarnings(PHPMD.UnusedPrivateMethod) As it is called dynamically. * * @since 9.1.0 * * @param array $data Data passed from the AJAX call. * * @return array */ private function set_license( $data ) { if ( ! current_user_can( 'manage_options' ) ) { $this->response ->set_code( '403' ) ->set_message( 'Forbidden' ) ->set_data( array( 'success' => false, 'message' => Rop_I18n::get_labels( 'general.no_permission' ) ) ); return $this->response->to_array(); } // NOTE: The license processor requires the license key, even if we want to deactivate the license. if ( empty( $data['license_key'] ) ) { $general_settings = new Rop_Global_Settings; $license_data = $general_settings->get_license_data(); if ( ! empty( $license_data ) && isset( $license_data->key ) ) { $data['license_key'] = $license_data->key; } } $response = apply_filters( 'themeisle_sdk_license_process_rop', $data['license_key'], $data['action'] ); if ( is_wp_error( $response ) ) { return $this->response ->set_data( array( 'success' => false, 'message' => 'activate' === $data['action'] ? Rop_I18n::get_labels( 'general.validation_failed' ) : Rop_I18n::get_labels( 'general.could_not_change_license' ) ) ) ->to_array(); } return $this->response ->set_code( '200' ) ->set_data( array( 'success' => true ) ) ->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(); } /** * API method called to add Telegram via app. * * @SuppressWarnings(PHPMD.UnusedPrivateMethod) As it is called dynamically. * * @since 9.1.3 * @access private * * @param array $data Telegram accounts data. * * @return array */ private function add_account_telegram( $data ) { $services = array(); $webhook_service = new Rop_Telegram_Service(); $model = new Rop_Services_Model(); $db = new Rop_Db_Upgrade(); if ( ! $webhook_service->add_account_with_app( $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 add mastodon via app. * * @SuppressWarnings(PHPMD.UnusedPrivateMethod) As it is called dynamically. * * @since 8.6.1 * @access private * * @param array $data Mastodon accounts data. * * @return array */ private function add_account_mastodon( $data ) { $services = array(); $active_accounts = array(); $md_service = new Rop_Mastodon_Service(); $model = new Rop_Services_Model(); $db = new Rop_Db_Upgrade(); $md_service->add_account_with_app( $data ); $services[ $md_service->get_service_id() ] = $md_service->get_service(); $active_accounts = array_merge( $active_accounts, $md_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(); } }