Skip to content

Commit c42916b

Browse files
dev: track first import creation/run
1 parent f21113d commit c42916b

File tree

5 files changed

+305
-55
lines changed

5 files changed

+305
-55
lines changed

includes/abstract/feedzy-rss-feeds-admin-abstract.php

Lines changed: 81 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,45 @@ public function get_usage_data( $data ) {
8888
// imports.
8989
$imports = array();
9090
$license = 'free';
91+
92+
$imports = array(
93+
// how many active imports are created.
94+
'publish' => count(
95+
get_posts(
96+
array(
97+
'post_type' => 'feedzy_imports',
98+
'post_status' => 'publish',
99+
'numberposts' => 99,
100+
'fields' => 'ids',
101+
)
102+
)
103+
),
104+
// how many draft imports are created.
105+
'draft' => count(
106+
get_posts(
107+
array(
108+
'post_type' => 'feedzy_imports',
109+
'post_status' => 'draft',
110+
'numberposts' => 99,
111+
'fields' => 'ids',
112+
)
113+
)
114+
),
115+
// how many posts were imported by the imports.
116+
'imported' => count(
117+
get_posts(
118+
array(
119+
'post_type' => 'post',
120+
'post_status' => array( 'publish', 'private', 'draft', 'trash' ),
121+
'numberposts' => 2999, //phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_numberposts
122+
'fields' => 'ids',
123+
'meta_key' => 'feedzy', //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
124+
'meta_value' => 1, //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
125+
)
126+
)
127+
),
128+
);
129+
91130
if ( feedzy_is_pro() ) {
92131
$license = 'pro';
93132
if ( apply_filters( 'feedzy_is_license_of_type', false, 'agency' ) ) {
@@ -107,70 +146,57 @@ public function get_usage_data( $data ) {
107146
}
108147
}
109148

110-
$imports = apply_filters(
111-
'feedzy_usage_data',
112-
array(
113-
// how many active imports are created.
114-
'publish' => count(
115-
get_posts(
116-
array(
117-
'post_type' => 'feedzy_imports',
118-
'post_status' => 'publish',
119-
'numberposts' => 99,
120-
'fields' => 'ids',
121-
)
122-
)
123-
),
124-
// how many draft imports are created
125-
'draft' => count(
126-
get_posts(
127-
array(
128-
'post_type' => 'feedzy_imports',
129-
'post_status' => 'draft',
130-
'numberposts' => 99,
131-
'fields' => 'ids',
132-
)
133-
)
134-
),
135-
// how many posts were imported by the imports
136-
'imported' => count(
137-
get_posts(
138-
array(
139-
'post_type' => 'post',
140-
'post_status' => array( 'publish', 'private', 'draft', 'trash' ),
141-
'numberposts' => 2999, //phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_numberposts
142-
'fields' => 'ids',
143-
'meta_key' => 'feedzy', //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
144-
'meta_value' => 1, //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
145-
)
146-
)
147-
),
148-
// integrations
149-
'integrations' => $integrations,
150-
)
151-
);
149+
$imports['integrations'] = $integrations;
152150
}
153151

154-
$settings = apply_filters( 'feedzy_get_settings', null );
155-
$config = array();
156-
if ( $settings ) {
157-
$proxy = isset( $settings['proxy'] ) && is_array( $settings['proxy'] ) && ! empty( $settings['proxy'] ) ? array_filter( $settings['proxy'] ) : array();
158-
if ( ! empty( $proxy ) ) {
159-
$config[] = 'proxy';
160-
}
161-
}
152+
$imports = wp_parse_args( Feedzy_Rss_Feeds_Usage::get_instance()->get_usage_stats(), $imports );
153+
$imports = apply_filters( 'feedzy_usage_data', $imports );
162154

163155
// how many posts contain the shortcode
164156
global $wpdb;
165157
$shortcodes = $wpdb->get_var( "SELECT count(*) FROM {$wpdb->prefix}posts WHERE post_status IN ('publish', 'private') AND post_content LIKE '%[feedzy-rss %'" ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
158+
166159
$data = array(
167-
'categories' => $categories,
168-
'imports' => $imports,
169-
'shortcodes' => $shortcodes,
170-
'license' => $license,
171-
'config' => $config,
160+
'categories' => $categories,
161+
'imports' => $imports,
162+
'shortcodes' => $shortcodes,
163+
'license' => $license,
172164
);
173165

166+
$settings = apply_filters( 'feedzy_get_settings', null );
167+
168+
if ( ! is_array( $settings ) || empty( $settings ) ) {
169+
return $data;
170+
}
171+
172+
$general_settings = array();
173+
$config = array();
174+
175+
$proxy = isset( $settings['proxy'] ) && is_array( $settings['proxy'] ) && ! empty( $settings['proxy'] ) ? array_filter( $settings['proxy'] ) : array();
176+
if ( ! empty( $proxy ) ) {
177+
$config[] = 'proxy';
178+
}
179+
180+
if ( isset( $settings['header'], $settings['header']['user-agent'] ) && ! empty( $settings['header']['user-agent'] ) ) {
181+
$config[] = 'custom-user-agent';
182+
}
183+
184+
if ( ! empty( $config ) ) {
185+
$data['config'] = $config;
186+
}
187+
188+
if ( is_array( $settings['general'] ) && ! empty( $settings['general'] ) ) {
189+
foreach ( $settings['general'] as $key => $value ) {
190+
if ( ! empty( $value ) ) {
191+
$general_settings[ $key ] = $value;
192+
}
193+
}
194+
195+
if ( ! empty( $general_settings ) ) {
196+
$data['general'] = $general_settings;
197+
}
198+
}
199+
174200
return $data;
175201
}
176202

includes/admin/feedzy-rss-feeds-admin.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,12 @@ public function save_feedzy_post_type_meta( $post_id, $post ) {
769769
$feedzy_category_feed = preg_replace( '/\s*,\s*|\R/', ',', $feedzy_category_feed );
770770
$category_meta['feedzy_category_feed'] = $feedzy_category_feed;
771771
}
772+
773+
if ( empty( get_post_meta( $post_id, 'usage_counted', true ) ) ) {
774+
Feedzy_Rss_Feeds_Usage::get_instance()->track_import_creation();
775+
update_post_meta( $post_id, 'usage_counted', 'yes' );
776+
}
777+
772778
if ( $post->post_type === 'revision' ) {
773779
return true;
774780
} else {

includes/admin/feedzy-rss-feeds-import.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,8 @@ public function run_cron( $max = 100, $job_id = 0 ) {
13871387
* @access private
13881388
*/
13891389
private function run_job( $job, $max ) {
1390+
Feedzy_Rss_Feeds_Usage::get_instance()->track_rss_import();
1391+
13901392
global $themeisle_log_event;
13911393
$source = get_post_meta( $job->ID, 'source', true );
13921394
$inc_key = get_post_meta( $job->ID, 'inc_key', true );
@@ -3352,6 +3354,7 @@ private function wizard_import_feed() {
33523354
'post_status' => 'publish',
33533355
)
33543356
);
3357+
Feedzy_Rss_Feeds_Usage::get_instance()->track_import_creation();
33553358
}
33563359

33573360
if ( ! is_wp_error( $job_id ) ) {
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
<?php
2+
/**
3+
* Track the usage of the plugin.
4+
*
5+
* @link https://themeisle.com
6+
* @since 5.0.7
7+
*
8+
* @package feedzy-rss-feeds
9+
* @subpackage feedzy-rss-feeds/includes/admin
10+
*/
11+
12+
/**
13+
* Track the usage of the plugin.
14+
*
15+
* @package feedzy-rss-feeds
16+
* @subpackage feedzy-rss-feeds/includes/admin
17+
* @author Themeisle <[email protected]>
18+
*/
19+
class Feedzy_Rss_Feeds_Usage {
20+
21+
/**
22+
* Option name in wp_options table.
23+
*/
24+
const OPTION_NAME = 'feedzy_usage';
25+
26+
/**
27+
* The single instance of the class.
28+
*
29+
* @var Feedzy_Rss_Feeds_Usage|null
30+
*/
31+
private static $instance = null;
32+
33+
/**
34+
* Default usage data structure.
35+
*
36+
* @var array<string, string|int>
37+
*/
38+
private $default_data = array(
39+
'first_import_run_datetime' => null,
40+
'imports_runs' => 0,
41+
'first_import_created_datetime' => '',
42+
'can_track_first_usage' => false,
43+
);
44+
45+
/**
46+
* Private constructor to prevent direct instantiation.
47+
*/
48+
private function __construct() {
49+
$this->init();
50+
}
51+
52+
/**
53+
* Prevent cloning of the instance.
54+
*/
55+
public function __clone() {}
56+
57+
/**
58+
* Prevent unserialization of the instance.
59+
*/
60+
public function __wakeup() {}
61+
62+
/**
63+
* Get the single instance of the class.
64+
*
65+
* @return Feedzy_Rss_Feeds_Usage
66+
*/
67+
public static function get_instance() {
68+
if (null === self::$instance) {
69+
self::$instance = new self();
70+
}
71+
return self::$instance;
72+
}
73+
74+
/**
75+
* Initialize the usage tracking.
76+
* Creates the option if it doesn't exist.
77+
*/
78+
private function init() {
79+
if ( false === get_option(self::OPTION_NAME) ) {
80+
add_option(self::OPTION_NAME, $this->default_data);
81+
}
82+
}
83+
84+
/**
85+
* Get all usage data.
86+
*
87+
* @return array<string, string|int> Usage data array.
88+
*/
89+
public function get_usage_data() {
90+
$data = get_option( self::OPTION_NAME, array() );
91+
return wp_parse_args( $data, $this->default_data );
92+
}
93+
94+
/**
95+
* Update usage data.
96+
*
97+
* @param array<string, string|int> $new_data Data to update.
98+
* @return bool True if the option was updated, false otherwise.
99+
*/
100+
public function update_usage_data( $new_data ) {
101+
$current_data = $this->get_usage_data();
102+
$updated_data = array_merge( $current_data, $new_data );
103+
return update_option(self::OPTION_NAME, $updated_data);
104+
}
105+
106+
/**
107+
* Track RSS feed import.
108+
* Sets first import timestamp if it's the first import, always increments counter.
109+
*
110+
* @return void
111+
*/
112+
public function track_rss_import() {
113+
$data = $this->get_usage_data();
114+
115+
if ( PHP_INT_MAX <= $data['imports_runs'] ) {
116+
return;
117+
}
118+
119+
$update_data = array();
120+
121+
$update_data['imports_runs'] = $data['imports_runs'] + 1;
122+
123+
if ( $data['can_track_first_usage'] && empty( $data['first_import_run_datetime'] ) ) {
124+
$update_data['first_import_run_datetime'] = current_time('mysql');
125+
}
126+
127+
$this->update_usage_data( $update_data );
128+
}
129+
130+
/**
131+
* Track settings page creation.
132+
* Sets first settings page timestamp if it's the first page, always increments counter.
133+
*
134+
* @return void
135+
*/
136+
public function track_import_creation() {
137+
$data = $this->get_usage_data();
138+
139+
if ( $data['can_track_first_usage'] && ! empty( $data['first_import_created_datetime'] ) ) {
140+
return;
141+
}
142+
143+
$this->update_usage_data( array( 'first_import_created_datetime' => current_time('mysql') ) );
144+
}
145+
146+
/**
147+
* Delete the usage data option.
148+
* Useful for plugin uninstall.
149+
*
150+
* @return bool True if the option was deleted, false otherwise.
151+
*/
152+
public function delete_usage_data() {
153+
return delete_option(self::OPTION_NAME);
154+
}
155+
156+
/**
157+
* Get usage statistics in a formatted array.
158+
*
159+
* @return array<string, string|int> Formatted usage statistics.
160+
*/
161+
public function get_usage_stats() {
162+
$data = $this->get_usage_data();
163+
164+
$stats = array(
165+
'import_runs' => $data['imports_runs'],
166+
);
167+
168+
if ( ! $data['can_track_first_usage'] ) {
169+
return $data;
170+
}
171+
172+
$stats['first_import_run_datetime'] = ! empty( $data['first_import_run_datetime'] ) ? $data['first_import_run_datetime'] : 'Never';
173+
$stats['first_import_created_datetime'] = ! empty( $data['first_import_created_datetime'] ) ? $data['first_import_created_datetime'] : 'Never';
174+
175+
// Calculate time between first import run and first import created if applicable.
176+
if (
177+
! empty( $data['first_import_run_datetime'] ) &&
178+
! empty( $data['first_import_created_datetime'] )
179+
) {
180+
$import_time = strtotime( $data['first_import_run_datetime'] );
181+
$settings_time = strtotime( $data['first_import_created_datetime'] );
182+
if (
183+
( false !== $import_time && false !== $settings_time ) &&
184+
$settings_time <= $import_time
185+
) {
186+
$stats['time_between_first_import_created_and_run'] = $import_time - $settings_time;
187+
}
188+
}
189+
190+
return $stats;
191+
}
192+
193+
/**
194+
* Check if the user is new to track the first usage.
195+
*
196+
* @return bool
197+
*/
198+
public function is_new_user() {
199+
$install_time = get_option( 'feedzy_rss_feeds_install', false );
200+
201+
if ( ! is_numeric( $install_time ) ) {
202+
return true;
203+
}
204+
205+
return DAY_IN_SECONDS >= ( time() - $install_time );
206+
}
207+
}

0 commit comments

Comments
 (0)