Skip to content

Commit

Permalink
feat: initial draft for event reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
Soare-Robert-Daniel committed Apr 19, 2024
1 parent 510d320 commit bf4256a
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 52 deletions.
10 changes: 5 additions & 5 deletions includes/abstract/feedzy-rss-feeds-admin-abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -1374,11 +1374,11 @@ public function get_feed_url( $feeds ) {
* @since 3.0.12
* @access public
*
* @param array $feed_items The feed items array.
* @param array $sc The short code attributes.
* @param object $feed The feed object.
* @param string $feed_url The feed URL source/s.
* @param array $sizes Sizes array.
* @param array $feed_items The feed items array.
* @param array $sc The short code attributes.
* @param \SimplePie $feed The feed object.
* @param string $feed_url The feed URL source/s.
* @param array $sizes Sizes array.
*
* @return array
*/
Expand Down
123 changes: 76 additions & 47 deletions includes/admin/feedzy-rss-feeds-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -1101,10 +1101,25 @@ private function get_taxonomies() {
private function run_now() {
check_ajax_referer( FEEDZY_BASEFILE, 'security' );

$job = get_post( filter_input( INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT ) );
$count = $this->run_job( $job, 100 );
$count = 0;
$msg = '';

try {
$job = get_post( filter_input( INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT ) );
$count = $this->run_job( $job, 100 );
$msg = $count > 0 ? __( 'Successfully run!', 'feedzy-rss-feeds' ) : __( 'Nothing imported!', 'feedzy-rss-feeds' );
} catch ( \Throwable $e ) {
$msg = __( 'An error occurred while running the job.', 'feedzy-rss-feeds' );

if ( defined( 'TI_EVENT_REPORTER_ENABLED' ) ) {
do_action( 'themeisle_capture_exception', $e );
}

if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( '[Feedzy Run Now] Error: ' . $e->getMessage() );
}
}

$msg = $count > 0 ? __( 'Successfully run!', 'feedzy-rss-feeds' ) : __( 'Nothing imported!', 'feedzy-rss-feeds' );
$msg .= ' (' . __( 'Refresh this page for the updated status', 'feedzy-rss-feeds' ) . ')';

wp_send_json_success( array( 'msg' => $msg, 'import_success' => $count > 0 ) );
Expand Down Expand Up @@ -1196,10 +1211,14 @@ public function run_cron( $max = 100 ) {
$this->run_job( $job, $max );
}
do_action( 'feedzy_run_cron_extra', $job );
} catch ( Exception $e ) {
} catch ( \Throwable $e ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( '[Feedzy Run Cron][Post title: ' . ( ! empty( $job->post_title ) ? $job->post_title : '' ) . '] Error: ' . $e->getMessage() );
}

if ( defined( 'TI_EVENT_REPORTER_ENABLED' ) ) {
do_action( 'themeisle_capture_exception', $e );
}
}
}
}
Expand Down Expand Up @@ -2901,58 +2920,68 @@ public function feedzy_import_trim_tags( $content = '' ) {
private function wizard_import_feed() {
check_ajax_referer( FEEDZY_BASEFILE, 'security' );

$post_type = ! empty( $_POST['post_type'] ) ? filter_input( INPUT_POST, 'post_type', FILTER_UNSAFE_RAW ) : '';
$wizard_data = get_option( 'feedzy_wizard_data', array() );
$wizard_data = ! empty( $wizard_data ) ? $wizard_data : array();
$wizard_data['post_type'] = $post_type;
try {
$post_type = ! empty( $_POST['post_type'] ) ? filter_input( INPUT_POST, 'post_type', FILTER_UNSAFE_RAW ) : '';
$wizard_data = get_option( 'feedzy_wizard_data', array() );
$wizard_data = ! empty( $wizard_data ) ? $wizard_data : array();
$wizard_data['post_type'] = $post_type;

$post_title = __( 'Setup Wizard', 'feedzy-rss-feeds' );
$job_id = post_exists( $post_title, '', '', 'feedzy_imports' );
$post_title = __( 'Setup Wizard', 'feedzy-rss-feeds' );
$job_id = post_exists( $post_title, '', '', 'feedzy_imports' );

$response = array(
'status' => 0,
);
$response = array(
'status' => 0,
);

// Delete previous meta data.
if ( $job_id ) {
$meta = get_post_meta( $job_id );
foreach ( $meta as $key => $value ) {
delete_post_meta( $job_id, $key );
// Delete previous meta data.
if ( $job_id ) {
$meta = get_post_meta( $job_id );
foreach ( $meta as $key => $value ) {
delete_post_meta( $job_id, $key );
}
}
}

// Create new import job.
if ( ! $job_id ) {
$job_id = wp_insert_post(
array(
'post_title' => $post_title,
'post_type' => 'feedzy_imports',
'post_status' => 'publish',
)
);
}
// Create new import job.
if ( ! $job_id ) {
$job_id = wp_insert_post(
array(
'post_title' => $post_title,
'post_type' => 'feedzy_imports',
'post_status' => 'publish',
)
);
}

if ( ! is_wp_error( $job_id ) ) {
update_post_meta( $job_id, 'source', $wizard_data['feed'] );
update_post_meta( $job_id, 'import_post_title', '[#item_title]' );
update_post_meta( $job_id, 'import_post_date', '[#item_date]' );
update_post_meta( $job_id, 'import_post_content', '[#item_content]' );
update_post_meta( $job_id, 'import_post_content', '[#item_content]' );
update_post_meta( $job_id, 'import_post_type', $post_type );
update_post_meta( $job_id, 'import_post_status', 'publish' );
update_post_meta( $job_id, 'import_post_featured_img', '[#item_image]' );
if ( ! is_wp_error( $job_id ) ) {
update_post_meta( $job_id, 'source', $wizard_data['feed'] );
update_post_meta( $job_id, 'import_post_title', '[#item_title]' );
update_post_meta( $job_id, 'import_post_date', '[#item_date]' );
update_post_meta( $job_id, 'import_post_content', '[#item_content]' );
update_post_meta( $job_id, 'import_post_content', '[#item_content]' );
update_post_meta( $job_id, 'import_post_type', $post_type );
update_post_meta( $job_id, 'import_post_status', 'publish' );
update_post_meta( $job_id, 'import_post_featured_img', '[#item_image]' );

// Update wizard data.
update_option( 'feedzy_wizard_data', $wizard_data );
// Update wizard data.
update_option( 'feedzy_wizard_data', $wizard_data );

$job = get_post( $job_id );
$count = $this->run_job( $job, 10 );
do_action( 'feedzy_run_cron_extra', $job );
$response = array(
'status' => $count > 0,
);
$job = get_post( $job_id );
$count = $this->run_job( $job, 10 );
do_action( 'feedzy_run_cron_extra', $job );
$response = array(
'status' => $count > 0,
);
}
wp_send_json( $response );
} catch ( \Throwable $e ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( '[Feedzy Wizard Import] Error: ' . $e->getMessage() );
}

if ( defined( 'TI_EVENT_REPORTER_ENABLED' ) ) {
do_action( 'themeisle_capture_exception', $e );
}
}
wp_send_json( $response );
}

/**
Expand Down
26 changes: 26 additions & 0 deletions includes/feedzy-rss-feeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,32 @@ function () {
$offer->load_banner();
}

// Start Event Reporter if telemetry is enabled.
if ( ! defined( 'TI_UNIT_TESTING' ) ) {
$telemetry_enabled = get_option( 'feedzy_rss_feeds_logger_flag', false );
if ( $telemetry_enabled ) {
try {
\Sentry\init( array( 'dsn' => '' ) ); // TODO: Add proxy DSN.
if ( ! defined( 'TI_EVENT_REPORTER_ENABLED' ) ) {
define( 'TI_EVENT_REPORTER_ENABLED', true );
add_action(
'themeisle_capture_exception', function ( $exception ) {
\Sentry\captureException( $exception );
}
);
add_action(
'themeisle_capture_message', function ( $message ) {
\Sentry\captureMessage( $message );
}
);
}
} catch ( Throwable $e ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( 'Event Reporter failed to initialize: ' . $e->getMessage() );
}
}
}
}
}

/**
Expand Down

0 comments on commit bf4256a

Please sign in to comment.