WIP integration for com-form
This commit is contained in:
15
includes/class-partnerexpo-core-integrations.php
Normal file
15
includes/class-partnerexpo-core-integrations.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
class Partnerexpo_Core_Integrations {
|
||||
public static function add_hooks($loader) {
|
||||
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/mc-integrations/com-form.php';
|
||||
|
||||
if ( ! $loader ) {
|
||||
Logger::log('error', 'Loader not found, skipping integration hooks');
|
||||
return;
|
||||
}
|
||||
|
||||
$loader->add_action( 'comment_post', Partnerexpo_Core_Integration_Comment_Form::class, 'queue_up', 10, 3 );
|
||||
$loader->add_action( 'process_comment_data', Partnerexpo_Core_Integration_Comment_Form::class, 'run_process', 10, 1 );
|
||||
}
|
||||
}
|
||||
@@ -117,11 +117,11 @@ class Partnerexpo_Core_Loader {
|
||||
public function run() {
|
||||
|
||||
foreach ( $this->filters as $hook ) {
|
||||
add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
|
||||
add_filter( $hook['hook'], [ $hook['component'], $hook['callback'] ], $hook['priority'], $hook['accepted_args'] );
|
||||
}
|
||||
|
||||
foreach ( $this->actions as $hook ) {
|
||||
add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
|
||||
add_action( $hook['hook'], [ $hook['component'], $hook['callback'] ], $hook['priority'], $hook['accepted_args'] );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -124,15 +124,31 @@ class Partnerexpo_Core {
|
||||
/**
|
||||
* Used to have relevance-based search results.
|
||||
*/
|
||||
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/dependency/class-query-w-relevance.php';
|
||||
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/utils/class-query-w-relevance.php';
|
||||
|
||||
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/utils/class-logger.php';
|
||||
|
||||
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-partnerexpo-core-integrations.php';
|
||||
|
||||
/**
|
||||
* The class responsible for registering custom user fields.
|
||||
*/
|
||||
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-partnerexpo-core-user-fields.php';
|
||||
|
||||
|
||||
$this->loader = new Partnerexpo_Core_Loader();
|
||||
|
||||
$this->loader->add_action( 'init', $this, 'setup' );
|
||||
}
|
||||
|
||||
public function setup() {
|
||||
Logger::init("PartnerEXPO Core", __("Megjegyezve, elrejtés", 'partnerexpo-core'), 'partnerexpo-core');
|
||||
|
||||
|
||||
Partnerexpo_Core_Integrations::add_hooks( $this->loader );
|
||||
|
||||
$user_fields = new Partnerexpo_Core_User_Fields( $this->loader );
|
||||
|
||||
$this->loader->run();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -151,7 +167,6 @@ class Partnerexpo_Core {
|
||||
$this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Register all of the hooks related to the admin area functionality
|
||||
* of the plugin.
|
||||
|
||||
@@ -1,97 +0,0 @@
|
||||
<?php
|
||||
defined('ABSPATH') or die;
|
||||
|
||||
/**
|
||||
* Centralized logging and admin notices for the MiniCRM integration.
|
||||
*/
|
||||
class Minicrm_Integration_Pb_Logger {
|
||||
|
||||
/**
|
||||
* Log an error and optionally raise an admin notice.
|
||||
*/
|
||||
public static function log($message, array $context = [], $raise_notice = true) {
|
||||
if ( $raise_notice ) {
|
||||
update_option(
|
||||
'minicrm_integration_last_error',
|
||||
[
|
||||
'message' => $message,
|
||||
'context' => $context,
|
||||
'time' => self::now_utc_plus_one()
|
||||
],
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
if ( class_exists( '\WC_Logger' ) ) {
|
||||
$logger = wc_get_logger();
|
||||
$logger->error($message, array_merge(['source' => 'minicrm-integration'], $context));
|
||||
return;
|
||||
}
|
||||
|
||||
$context_str = '';
|
||||
if ( ! empty($context) ) {
|
||||
$context_str = ' | ' . wp_json_encode($context);
|
||||
}
|
||||
error_log('[minicrm-integration] ' . $message . $context_str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Current time in UTC+1 (Y-m-d H:i:s) using wp_date().
|
||||
*/
|
||||
private static function now_utc_plus_one() {
|
||||
return wp_date( 'Y-m-d H:i:s', time() + HOUR_IN_SECONDS, new DateTimeZone( 'UTC' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Show latest MiniCRM sync error in the admin UI so someone can act on it.
|
||||
*/
|
||||
public static function render_admin_notice() {
|
||||
if ( ! current_user_can( 'manage_woocommerce' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$error = get_option( 'minicrm_integration_last_error' );
|
||||
|
||||
if ( ! $error || empty( $error['message'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$message = esc_html( $error['message'] );
|
||||
$time = ! empty( $error['time'] ) ? esc_html( $error['time'] ) : '';
|
||||
$context = '';
|
||||
if ( ! empty( $error['context'] ) ) {
|
||||
$context = '<pre style="white-space:pre-wrap;word-break:break-word;margin-top:4px;">' . esc_html( wp_json_encode( $error['context'], JSON_PARTIAL_OUTPUT_ON_ERROR ) ) . '</pre>';
|
||||
}
|
||||
|
||||
$dismiss_url = wp_nonce_url(
|
||||
add_query_arg( 'minicrm_clear_error', '1' ),
|
||||
'minicrm_clear_error',
|
||||
'minicrm_nonce'
|
||||
);
|
||||
|
||||
printf(
|
||||
'<div class="notice notice-error"><p><strong>MiniCRM integráció hiba:</strong> %s%s%s</p><p><a href="%s" class="button">Megjegyezve, elrejtés</a></p></div>',
|
||||
$message,
|
||||
$time ? ' <em>(' . $time . ')</em>' : '',
|
||||
$context,
|
||||
esc_url( $dismiss_url )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear error notice when user dismisses via link.
|
||||
*/
|
||||
public static function handle_admin_notice_dismiss() {
|
||||
if ( ! isset( $_GET['minicrm_clear_error'], $_GET['minicrm_nonce'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! wp_verify_nonce( $_GET['minicrm_nonce'], 'minicrm_clear_error' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
delete_option( 'minicrm_integration_last_error' );
|
||||
wp_safe_redirect( remove_query_arg( [ 'minicrm_clear_error', 'minicrm_nonce' ] ) );
|
||||
exit;
|
||||
}
|
||||
}
|
||||
27
includes/mc-integrations/com-form.php
Normal file
27
includes/mc-integrations/com-form.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Sends in the data from the comment form to MiniCRM
|
||||
*
|
||||
* @link https://github.com/Duskell
|
||||
* @since 1.1.11
|
||||
*
|
||||
* @package Partnerexpo_Core
|
||||
* @subpackage Partnerexpo_Core/includes/mc-integrations
|
||||
*/
|
||||
|
||||
if ( ! defined( 'WPINC' ) ) {
|
||||
die;
|
||||
}
|
||||
|
||||
class Partnerexpo_Core_Integration_Comment_Form {
|
||||
public static function queue_up($comment_id, $comment_approved, $commentdata) {
|
||||
as_schedule_single_action( time() + 60, 'process_comment_data', [ $comment_id ] );
|
||||
}
|
||||
|
||||
public static function run_process($comment_id) {
|
||||
Logger::log('data', $comment_id);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
135
includes/utils/class-logger.php
Normal file
135
includes/utils/class-logger.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
defined('ABSPATH') or die;
|
||||
|
||||
/**
|
||||
* Centralized logging and admin notices.
|
||||
*/
|
||||
class Logger {
|
||||
static $source = "plugin";
|
||||
static $clear_text = "Dismiss";
|
||||
static $namespace = "logger";
|
||||
|
||||
// Prevents hooks from being registered multiple times
|
||||
private static $initialized = false;
|
||||
|
||||
const MAX_ERRORS = 5;
|
||||
|
||||
public static function init($source, $clear_text, $namespace) {
|
||||
self::$source = $source;
|
||||
self::$clear_text = $clear_text;
|
||||
self::$namespace = $namespace;
|
||||
|
||||
if ( ! self::$initialized ) {
|
||||
add_action( 'admin_notices', [ self::class, 'render_admin_notice' ] );
|
||||
add_action( 'admin_init', [ self::class, 'handle_admin_notice_dismiss' ] );
|
||||
self::$initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an error and optionally raise an admin notice.
|
||||
*/
|
||||
public static function log($message, array $context = [], $raise_notice = true) {
|
||||
if ( $raise_notice ) {
|
||||
$errors = get_option( self::$namespace . '_errors', [] );
|
||||
if ( ! is_array( $errors ) ) {
|
||||
$errors = [];
|
||||
}
|
||||
|
||||
$errors[] = [
|
||||
'message' => $message,
|
||||
'context' => $context,
|
||||
'time' => wp_date( 'Y-m-d H:i:s' )
|
||||
];
|
||||
|
||||
if ( count( $errors ) > self::MAX_ERRORS ) {
|
||||
$errors = array_slice( $errors, -self::MAX_ERRORS );
|
||||
}
|
||||
|
||||
update_option( self::$namespace . '_errors', $errors, false );
|
||||
}
|
||||
|
||||
$context_str = '';
|
||||
if ( ! empty($context) ) {
|
||||
$context_str = ' | ' . wp_json_encode($context);
|
||||
}
|
||||
error_log('['. self::$namespace .'] ' . $message . $context_str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show latest errors in the admin UI.
|
||||
*/
|
||||
public static function render_admin_notice() {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$errors = get_option( self::$namespace . '_errors' );
|
||||
|
||||
if ( empty( $errors ) || ! is_array( $errors ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$nonce_action = self::$namespace . '_clear_error';
|
||||
$nonce_name = self::$namespace . '_error_nonce';
|
||||
|
||||
$dismiss_url = wp_nonce_url(
|
||||
add_query_arg( $nonce_action, '1' ),
|
||||
$nonce_action,
|
||||
$nonce_name
|
||||
);
|
||||
|
||||
echo '<div class="notice notice-error">';
|
||||
printf( '<p><strong>%s:</strong></p>', esc_html( self::$source ) );
|
||||
echo '<ul style="list-style-type: disc; margin-left: 20px;">';
|
||||
|
||||
foreach ( $errors as $error ) {
|
||||
$message = esc_html( $error['message'] ?? 'Unknown error' );
|
||||
$time = ! empty( $error['time'] ) ? esc_html( $error['time'] ) : '';
|
||||
$context = '';
|
||||
|
||||
if ( ! empty( $error['context'] ) ) {
|
||||
$context = '<pre style="white-space:pre-wrap;word-break:break-word;margin-top:4px;background:rgba(0,0,0,0.05);padding:8px;">' .
|
||||
esc_html( wp_json_encode( $error['context'], JSON_PARTIAL_OUTPUT_ON_ERROR ) ) .
|
||||
'</pre>';
|
||||
}
|
||||
|
||||
printf(
|
||||
'<li>%s%s%s</li>',
|
||||
$message,
|
||||
$time ? ' <em>(' . $time . ')</em>' : '',
|
||||
$context
|
||||
);
|
||||
}
|
||||
|
||||
echo '</ul>';
|
||||
printf(
|
||||
'<p><a href="%s" class="button">%s</a></p>',
|
||||
esc_url( $dismiss_url ),
|
||||
esc_html( self::$clear_text )
|
||||
);
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear error notice when user dismisses via link.
|
||||
*/
|
||||
public static function handle_admin_notice_dismiss() {
|
||||
$nonce_action = self::$namespace . '_clear_error';
|
||||
$nonce_name = self::$namespace . '_error_nonce';
|
||||
|
||||
if ( ! isset( $_GET[$nonce_action], $_GET[$nonce_name] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! wp_verify_nonce( $_GET[$nonce_name], $nonce_action ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Delete the entire array of errors
|
||||
delete_option( self::$namespace . '_errors' );
|
||||
|
||||
wp_safe_redirect( remove_query_arg( [ $nonce_action, $nonce_name ] ) );
|
||||
exit;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user