ThirstyAffiliates Affiliate Link Manager - Version 3.5

Version Description

  • Improvement: Gutenberg compatibility: TA Buttons are not showing up anymore on Gutenberg classic editor
  • Improvement: Gutenberg compatibility: TA buttons in wp editors present in metaboxes are not working properly.
Download this release

Release Info

Developer jkohlbach
Plugin Icon 128x128 ThirstyAffiliates Affiliate Link Manager
Version 3.5
Comparing to
See all releases

Code changes from version 3.4.0 to 3.5

Files changed (154) hide show
  1. .editorconfig +6 -0
  2. .htaccess +2 -2
  3. Abstracts/Abstract_Main_Plugin_Class.php +112 -112
  4. Abstracts/index.php +0 -0
  5. Helpers/Helper_Functions.php +591 -591
  6. Helpers/Plugin_Constants.php +164 -164
  7. Helpers/index.php +0 -0
  8. Interfaces/Activatable_Interface.php +0 -0
  9. Interfaces/Deactivatable_Interface.php +22 -22
  10. Interfaces/Initiable_Interface.php +0 -0
  11. Interfaces/Model_Interface.php +0 -0
  12. Interfaces/index.php +0 -0
  13. Models/Affiliate_Link.php +768 -768
  14. Models/Affiliate_Link_Attachment.php +0 -0
  15. Models/Affiliate_Links_CPT.php +1014 -1014
  16. Models/Bootstrap.php +517 -517
  17. Models/Guided_Tour.php +389 -389
  18. Models/Link_Fixer.php +289 -289
  19. Models/Link_Picker.php +658 -658
  20. Models/Marketing.php +0 -0
  21. Models/Migration.php +0 -0
  22. Models/REST_API.php +354 -354
  23. Models/Rewrites_Redirection.php +440 -440
  24. Models/Script_Loader.php +0 -0
  25. Models/Settings.php +2073 -2073
  26. Models/Shortcodes.php +301 -301
  27. Models/Stats_Reporting.php +1077 -1077
  28. Models/index.php +0 -0
  29. css/admin/index.php +0 -0
  30. css/admin/ta-affiliate-link-list.css +29 -29
  31. css/admin/ta-editor.css +46 -46
  32. css/admin/ta-guided-tour.css +11 -11
  33. css/admin/ta-reports.css +255 -255
  34. css/admin/ta-settings.css +113 -113
  35. css/admin/tinymce/editor.css +8 -8
  36. css/index.php +0 -0
  37. css/lib/jquery-tiptip/jquery-tiptip.css +109 -109
  38. css/lib/select2/select2.css +484 -484
  39. css/lib/select2/select2.min.css +1 -1
  40. images/admin-review-notice-logo.png +0 -0
  41. images/index.php +0 -0
  42. images/sidebar.jpg +0 -0
  43. images/spinner-2x.gif +0 -0
  44. images/spinner.gif +0 -0
  45. index.php +0 -0
  46. js/app/advance_link_picker/.babelrc +3 -0
  47. js/app/advance_link_picker/.eslintrc.json +30 -0
  48. js/app/advance_link_picker/dist/advance-link-picker.css +0 -0
  49. js/app/advance_link_picker/dist/advance-link-picker.js +1 -1
  50. js/app/advance_link_picker/package-lock.json +9586 -9586
  51. js/app/advance_link_picker/package.json +28 -0
  52. js/app/advance_link_picker/src/advance-link-picker-search.js +237 -0
  53. js/app/advance_link_picker/src/assets/styles/index.scss +15 -0
  54. js/app/advance_link_picker/src/assets/styles/results-panel.scss +174 -0
  55. js/app/advance_link_picker/src/assets/styles/search-panel.scss +26 -0
  56. js/app/advance_link_picker/src/index.js +12 -0
  57. js/app/advance_link_picker/src/insert-affiliate-link-normal.js +285 -0
  58. js/app/advance_link_picker/webpack.config.js +33 -0
  59. js/app/affiliate_link_page/.babelrc +3 -0
  60. js/app/affiliate_link_page/.eslintrc.json +30 -0
  61. js/app/affiliate_link_page/dist/affiliate-link-page.css +0 -0
  62. js/app/affiliate_link_page/dist/affiliate-link-page.js +23 -23
  63. js/app/affiliate_link_page/package-lock.json +6830 -6830
  64. js/app/affiliate_link_page/package.json +27 -0
  65. js/app/affiliate_link_page/src/affiliate-link-attachment.js +315 -0
  66. js/app/affiliate_link_page/src/affiliate-link-urls.js +43 -0
  67. js/app/affiliate_link_page/src/assets/styles/affiliate-link-attachment.scss +110 -0
  68. js/app/affiliate_link_page/src/assets/styles/affiliate-link-urls.scss +20 -0
  69. js/app/affiliate_link_page/src/assets/styles/index.scss +70 -0
  70. js/app/affiliate_link_page/src/assets/styles/link-inserted-scanner.scss +82 -0
  71. js/app/affiliate_link_page/src/index.js +16 -0
  72. js/app/affiliate_link_page/src/link-category-show-in-slug.js +135 -0
  73. js/app/affiliate_link_page/src/link-insert-scanner.js +51 -0
  74. js/app/affiliate_link_page/webpack.config.js +33 -0
  75. js/app/edit_shortcode/.babelrc +3 -0
  76. js/app/edit_shortcode/.eslintrc.json +29 -0
  77. js/app/edit_shortcode/dist/edit-shortcode.css +0 -0
  78. js/app/edit_shortcode/dist/edit-shortcode.js +0 -0
  79. js/app/edit_shortcode/package.json +27 -0
  80. js/app/edit_shortcode/src/assets/styles/index.scss +95 -0
  81. js/app/edit_shortcode/src/edit-shortcode.js +85 -0
  82. js/app/edit_shortcode/src/index.js +10 -0
  83. js/app/edit_shortcode/webpack.config.js +33 -0
  84. js/app/import_export/.babelrc +3 -0
  85. js/app/import_export/.eslintrc.json +30 -0
  86. js/app/import_export/dist/import-export.css +0 -0
  87. js/app/import_export/dist/import-export.js +24 -24
  88. js/app/import_export/package.json +29 -0
  89. js/app/import_export/src/assets/styles/export.scss +19 -0
  90. js/app/import_export/src/assets/styles/import.scss +24 -0
  91. js/app/import_export/src/assets/styles/index.scss +6 -0
  92. js/app/import_export/src/export.js +23 -0
  93. js/app/import_export/src/import.js +65 -0
  94. js/app/import_export/src/index.js +18 -0
  95. js/app/import_export/webpack.config.js +33 -0
  96. js/app/migration/.babelrc +3 -0
  97. js/app/migration/.eslintrc.json +30 -0
  98. js/app/migration/dist/migration.css +0 -0
  99. js/app/migration/dist/migration.js +24 -24
  100. js/app/migration/package.json +28 -0
  101. js/app/migration/src/assets/styles/index.scss +37 -0
  102. js/app/migration/src/index.js +66 -0
  103. js/app/migration/webpack.config.js +33 -0
  104. js/app/quick_add_affiliate_link/.babelrc +3 -0
  105. js/app/quick_add_affiliate_link/.eslintrc.json +30 -0
  106. js/app/quick_add_affiliate_link/dist/quick-add-affiliate-link.css +0 -0
  107. js/app/quick_add_affiliate_link/dist/quick-add-affiliate-link.js +24 -24
  108. js/app/quick_add_affiliate_link/package.json +27 -0
  109. js/app/quick_add_affiliate_link/src/assets/styles/form-style.scss +99 -0
  110. js/app/quick_add_affiliate_link/src/assets/styles/index.scss +22 -0
  111. js/app/quick_add_affiliate_link/src/index.js +10 -0
  112. js/app/quick_add_affiliate_link/src/process-add-affiliate-link.js +205 -0
  113. js/app/quick_add_affiliate_link/webpack.config.js +33 -0
  114. js/app/ta-editor.js +198 -164
  115. js/app/ta-guided-tour.js +210 -210
  116. js/app/ta-reports.js +381 -381
  117. js/app/ta-review-request.js +38 -38
  118. js/app/ta-settings.js +137 -137
  119. js/app/ta.js +245 -245
  120. js/index.php +0 -0
  121. js/lib/chosen/chosen-sprite.png +0 -0
  122. js/lib/chosen/chosen-sprite@2x.png +0 -0
  123. js/lib/chosen/chosen.jquery.js +1321 -1321
  124. js/lib/chosen/chosen.jquery.min.js +1 -1
  125. js/lib/chosen/chosen.min.css +2 -2
  126. js/lib/flot/jquery.flot.min.js +7 -7
  127. js/lib/flot/jquery.flot.time.min.js +6 -6
  128. js/lib/jquery-tiptip/jquery.tipTip.js +190 -190
  129. js/lib/jquery-tiptip/jquery.tipTip.min.js +0 -0
  130. js/lib/selectize/selectize.default.css +394 -394
  131. js/lib/selectize/selectize.min.js +2 -2
  132. js/lib/thirstymce/editor-plugin.js +598 -545
  133. js/lib/thirstymce/img/aff-new.gif +0 -0
  134. js/lib/thirstymce/img/aff.gif +0 -0
  135. js/lib/thirstymce/widget-editor.js +134 -134
  136. languages/index.php +0 -0
  137. languages/thirstyaffiliates.pot +1321 -1320
  138. logs/{.hgkeep → .gitkeep} +0 -0
  139. readme.txt +517 -513
  140. thirstyaffiliates.php +378 -378
  141. uninstall.php +0 -0
  142. views/cpt/view-attach-images-metabox-external-image.php +8 -8
  143. views/cpt/view-attach-images-metabox-single-image.php +8 -8
  144. views/cpt/view-attach-images-metabox.php +63 -63
  145. views/cpt/view-inserted-link-scanner-metabox.php +24 -24
  146. views/cpt/view-link-options-metabox.php +98 -98
  147. views/cpt/view-save-affiliate-link-metabox.php +15 -15
  148. views/cpt/view-urls-metabox.php +48 -48
  149. views/index.php +0 -0
  150. views/linkpicker/advance-link-picker.php +72 -72
  151. views/linkpicker/edit-shortcode.php +81 -81
  152. views/linkpicker/quick-add-affiliate-link.php +113 -113
  153. views/reports/link-performance-report.php +90 -90
  154. wpml-config.xml +8 -8
.editorconfig ADDED
@@ -0,0 +1,6 @@
1
+ # top-most EditorConfig file
2
+ root = true
3
+
4
+ [*]
5
+ indent_style = space
6
+ indent_size = 4
.htaccess CHANGED
@@ -1,2 +1,2 @@
1
- Options -Indexes
2
-
1
+ Options -Indexes
2
+
Abstracts/Abstract_Main_Plugin_Class.php CHANGED
@@ -1,112 +1,112 @@
1
- <?php
2
- namespace ThirstyAffiliates\Abstracts;
3
-
4
- use ThirstyAffiliates\Interfaces\Model_Interface;
5
-
6
- if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
7
-
8
- /**
9
- * Abstract class that the main plugin class needs to extend.
10
- *
11
- * @since 3.0.0
12
- */
13
- abstract class Abstract_Main_Plugin_Class {
14
-
15
- /*
16
- |--------------------------------------------------------------------------
17
- | Class Properties
18
- |--------------------------------------------------------------------------
19
- */
20
-
21
- /**
22
- * Property that houses an array of all the "regular models" of the plugin.
23
- *
24
- * @since 3.0.0
25
- * @access protected
26
- * @var array
27
- */
28
- protected $__all_models = array();
29
-
30
- /**
31
- * Property that houses an array of all "public regular models" of the plugin.
32
- * Public models can be accessed and utilized by external entities via the main plugin class.
33
- *
34
- * @since 3.0.0
35
- * @access public
36
- * @var array
37
- */
38
- public $models = array();
39
-
40
- /**
41
- * Property that houses an array of all "public helper classes" of the plugin.
42
- *
43
- * @since 3.0.0
44
- * @access public
45
- * @var array
46
- */
47
- public $helpers = array();
48
-
49
-
50
-
51
-
52
- /*
53
- |--------------------------------------------------------------------------
54
- | Class Methods
55
- |--------------------------------------------------------------------------
56
- */
57
-
58
- /**
59
- * Add a "regular model" to the main plugin class "all models" array.
60
- *
61
- * @since 3.0.0
62
- * @access public
63
- *
64
- * @param Model_Interface $model Regular model.
65
- */
66
- public function add_to_all_plugin_models( Model_Interface $model ) {
67
-
68
- $class_reflection = new \ReflectionClass( $model );
69
- $class_name = $class_reflection->getShortName();
70
-
71
- if ( !array_key_exists( $class_name , $this->__all_models ) )
72
- $this->__all_models[ $class_name ] = $model;
73
-
74
- }
75
-
76
- /**
77
- * Add a "regular model" to the main plugin class "public models" array.
78
- *
79
- * @since 3.0.0
80
- * @access public
81
- *
82
- * @param Model_Interface $model Regular model.
83
- */
84
- public function add_to_public_models( Model_Interface $model ) {
85
-
86
- $class_reflection = new \ReflectionClass( $model );
87
- $class_name = $class_reflection->getShortName();
88
-
89
- if ( !array_key_exists( $class_name , $this->models ) )
90
- $this->models[ $class_name ] = $model;
91
-
92
- }
93
-
94
- /**
95
- * Add a "helper class instance" to the main plugin class "public helpers" array.
96
- *
97
- * @since 3.0.0
98
- * @access public
99
- *
100
- * @param object $helper Helper class instance.
101
- */
102
- public function add_to_public_helpers( $helper ) {
103
-
104
- $class_reflection = new \ReflectionClass( $helper );
105
- $class_name = $class_reflection->getShortName();
106
-
107
- if ( !array_key_exists( $class_name , $this->helpers ) )
108
- $this->helpers[ $class_name ] = $helper;
109
-
110
- }
111
-
112
- }
1
+ <?php
2
+ namespace ThirstyAffiliates\Abstracts;
3
+
4
+ use ThirstyAffiliates\Interfaces\Model_Interface;
5
+
6
+ if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
7
+
8
+ /**
9
+ * Abstract class that the main plugin class needs to extend.
10
+ *
11
+ * @since 3.0.0
12
+ */
13
+ abstract class Abstract_Main_Plugin_Class {
14
+
15
+ /*
16
+ |--------------------------------------------------------------------------
17
+ | Class Properties
18
+ |--------------------------------------------------------------------------
19
+ */
20
+
21
+ /**
22
+ * Property that houses an array of all the "regular models" of the plugin.
23
+ *
24
+ * @since 3.0.0
25
+ * @access protected
26
+ * @var array
27
+ */
28
+ protected $__all_models = array();
29
+
30
+ /**
31
+ * Property that houses an array of all "public regular models" of the plugin.
32
+ * Public models can be accessed and utilized by external entities via the main plugin class.
33
+ *
34
+ * @since 3.0.0
35
+ * @access public
36
+ * @var array
37
+ */
38
+ public $models = array();
39
+
40
+ /**
41
+ * Property that houses an array of all "public helper classes" of the plugin.
42
+ *
43
+ * @since 3.0.0
44
+ * @access public
45
+ * @var array
46
+ */
47
+ public $helpers = array();
48
+
49
+
50
+
51
+
52
+ /*
53
+ |--------------------------------------------------------------------------
54
+ | Class Methods
55
+ |--------------------------------------------------------------------------
56
+ */
57
+
58
+ /**
59
+ * Add a "regular model" to the main plugin class "all models" array.
60
+ *
61
+ * @since 3.0.0
62
+ * @access public
63
+ *
64
+ * @param Model_Interface $model Regular model.
65
+ */
66
+ public function add_to_all_plugin_models( Model_Interface $model ) {
67
+
68
+ $class_reflection = new \ReflectionClass( $model );
69
+ $class_name = $class_reflection->getShortName();
70
+
71
+ if ( !array_key_exists( $class_name , $this->__all_models ) )
72
+ $this->__all_models[ $class_name ] = $model;
73
+
74
+ }
75
+
76
+ /**
77
+ * Add a "regular model" to the main plugin class "public models" array.
78
+ *
79
+ * @since 3.0.0
80
+ * @access public
81
+ *
82
+ * @param Model_Interface $model Regular model.
83
+ */
84
+ public function add_to_public_models( Model_Interface $model ) {
85
+
86
+ $class_reflection = new \ReflectionClass( $model );
87
+ $class_name = $class_reflection->getShortName();
88
+
89
+ if ( !array_key_exists( $class_name , $this->models ) )
90
+ $this->models[ $class_name ] = $model;
91
+
92
+ }
93
+
94
+ /**
95
+ * Add a "helper class instance" to the main plugin class "public helpers" array.
96
+ *
97
+ * @since 3.0.0
98
+ * @access public
99
+ *
100
+ * @param object $helper Helper class instance.
101
+ */
102
+ public function add_to_public_helpers( $helper ) {
103
+
104
+ $class_reflection = new \ReflectionClass( $helper );
105
+ $class_name = $class_reflection->getShortName();
106
+
107
+ if ( !array_key_exists( $class_name , $this->helpers ) )
108
+ $this->helpers[ $class_name ] = $helper;
109
+
110
+ }
111
+
112
+ }
Abstracts/index.php CHANGED
File without changes
Helpers/Helper_Functions.php CHANGED
@@ -1,591 +1,591 @@
1
- <?php
2
- namespace ThirstyAffiliates\Helpers;
3
-
4
- use ThirstyAffiliates\Abstracts\Abstract_Main_Plugin_Class;
5
-
6
- use ThirstyAffiliates\Models\Affiliate_Link;
7
-
8
- if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
9
-
10
- /**
11
- * Model that houses all the helper functions of the plugin.
12
- *
13
- * 3.0.0
14
- */
15
- class Helper_Functions {
16
-
17
- /*
18
- |--------------------------------------------------------------------------
19
- | Class Properties
20
- |--------------------------------------------------------------------------
21
- */
22
-
23
- /**
24
- * Property that holds the single main instance of Helper_Functions.
25
- *
26
- * @since 3.0.0
27
- * @access private
28
- * @var Helper_Functions
29
- */
30
- private static $_instance;
31
-
32
- /**
33
- * Model that houses all the plugin constants.
34
- *
35
- * @since 3.0.0
36
- * @access private
37
- * @var Plugin_Constants
38
- */
39
- private $_constants;
40
-
41
- /**
42
- * Property that houses all the saved settings.
43
- *
44
- * @since 3.0.0
45
- * @access private
46
- */
47
- private $_settings = array();
48
-
49
-
50
-
51
-
52
- /*
53
- |--------------------------------------------------------------------------
54
- | Class Methods
55
- |--------------------------------------------------------------------------
56
- */
57
-
58
- /**
59
- * Class constructor.
60
- *
61
- * @since 3.0.0
62
- * @access public
63
- *
64
- * @param Abstract_Main_Plugin_Class $main_plugin Main plugin object.
65
- * @param Plugin_Constants $constants Plugin constants object.
66
- */
67
- public function __construct( Abstract_Main_Plugin_Class $main_plugin , Plugin_Constants $constants ) {
68
-
69
- $this->_constants = $constants;
70
-
71
- $main_plugin->add_to_public_helpers( $this );
72
-
73
- }
74
-
75
- /**
76
- * Ensure that only one instance of this class is loaded or can be loaded ( Singleton Pattern ).
77
- *
78
- * @since 3.0.0
79
- * @access public
80
- *
81
- * @param Abstract_Main_Plugin_Class $main_plugin Main plugin object.
82
- * @param Plugin_Constants $constants Plugin constants object.
83
- * @return Helper_Functions
84
- */
85
- public static function get_instance( Abstract_Main_Plugin_Class $main_plugin , Plugin_Constants $constants ) {
86
-
87
- if ( !self::$_instance instanceof self )
88
- self::$_instance = new self( $main_plugin , $constants );
89
-
90
- return self::$_instance;
91
-
92
- }
93
-
94
-
95
-
96
-
97
- /*
98
- |--------------------------------------------------------------------------
99
- | Helper Functions
100
- |--------------------------------------------------------------------------
101
- */
102
-
103
- /**
104
- * Write data to plugin log file.
105
- *
106
- * @since 3.0.0
107
- * @access public
108
- *
109
- * @param mixed Data to log.
110
- */
111
- public function write_debug_log( $log ) {
112
-
113
- error_log( "\n[" . current_time( 'mysql' ) . "]\n" . $log . "\n--------------------------------------------------\n" , 3 , $this->_constants->LOGS_ROOT_PATH() . 'debug.log' );
114
-
115
- }
116
-
117
- /**
118
- * Check if current user is authorized to manage the plugin on the backend.
119
- *
120
- * @since 3.0.0
121
- * @access public
122
- *
123
- * @param WP_User $user WP_User object.
124
- * @return boolean True if authorized, False otherwise.
125
- */
126
- public function current_user_authorized( $user = null ) {
127
-
128
- // Array of roles allowed to access/utilize the plugin
129
- $admin_roles = apply_filters( 'ucfw_admin_roles' , array( 'administrator' ) );
130
-
131
- if ( is_null( $user ) )
132
- $user = wp_get_current_user();
133
-
134
- if ( $user->ID )
135
- return count( array_intersect( ( array ) $user->roles , $admin_roles ) ) ? true : false;
136
- else
137
- return false;
138
-
139
- }
140
-
141
- /**
142
- * Returns the timezone string for a site, even if it's set to a UTC offset
143
- *
144
- * Adapted from http://www.php.net/manual/en/function.timezone-name-from-abbr.php#89155
145
- *
146
- * Reference:
147
- * http://www.skyverge.com/blog/down-the-rabbit-hole-wordpress-and-timezones/
148
- *
149
- * @since 3.0.0
150
- * @access public
151
- *
152
- * @return string Valid PHP timezone string
153
- */
154
- public function get_site_current_timezone() {
155
-
156
- // if site timezone string exists, return it
157
- if ( $timezone = get_option( 'timezone_string' ) )
158
- return $timezone;
159
-
160
- // get UTC offset, if it isn't set then return UTC
161
- if ( 0 === ( $utc_offset = get_option( 'gmt_offset', 0 ) ) )
162
- return 'UTC';
163
-
164
- return $this->convert_utc_offset_to_timezone( $utc_offset );
165
-
166
- }
167
-
168
- /**
169
- * Conver UTC offset to timezone.
170
- *
171
- * @since 1.2.0
172
- * @access public
173
- *
174
- * @param float|int|string $utc_offset UTC offset.
175
- * @return string valid PHP timezone string
176
- */
177
- public function convert_utc_offset_to_timezone( $utc_offset ) {
178
-
179
- // adjust UTC offset from hours to seconds
180
- $utc_offset *= 3600;
181
-
182
- // attempt to guess the timezone string from the UTC offset
183
- if ( $timezone = timezone_name_from_abbr( '' , $utc_offset , 0 ) )
184
- return $timezone;
185
-
186
- // last try, guess timezone string manually
187
- $is_dst = date( 'I' );
188
-
189
- foreach ( timezone_abbreviations_list() as $abbr )
190
- foreach ( $abbr as $city )
191
- if ( $city[ 'dst' ] == $is_dst && $city[ 'offset' ] == $utc_offset )
192
- return $city[ 'timezone_id' ];
193
-
194
- // fallback to UTC
195
- return 'UTC';
196
-
197
- }
198
-
199
- /**
200
- * Get all user roles.
201
- *
202
- * @since 3.0.0
203
- * @access public
204
- *
205
- * @global WP_Roles $wp_roles Core class used to implement a user roles API.
206
- *
207
- * @return array Array of all site registered user roles. User role key as the key and value is user role text.
208
- */
209
- public function get_all_user_roles() {
210
-
211
- global $wp_roles;
212
- return $wp_roles->get_names();
213
-
214
- }
215
-
216
- /**
217
- * Check validity of a save post action.
218
- *
219
- * @since 3.0.0
220
- * @access private
221
- *
222
- * @param int $post_id Id of the coupon post.
223
- * @param string $post_type Post type to check.
224
- * @return bool True if valid save post action, False otherwise.
225
- */
226
- public function check_if_valid_save_post_action( $post_id , $post_type ) {
227
-
228
- if ( get_post_type() != $post_type || empty( $_POST ) || wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) || !current_user_can( 'edit_page' , $post_id ) )
229
- return false;
230
- else
231
- return true;
232
-
233
- }
234
-
235
- /**
236
- * Get user IP address.
237
- *
238
- * @since 3.0.0
239
- * @since 3.3.2 Added condition to disable IP address collection (for GDRP compliance).
240
- * @access public
241
- *
242
- * @return string User's IP address.
243
- */
244
- public function get_user_ip_address() {
245
-
246
- if ( get_option( 'ta_disable_ip_address_collection' ) === 'yes' )
247
- return;
248
-
249
- if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) )
250
- $ip = $_SERVER['HTTP_CLIENT_IP'];
251
- elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
252
- $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
253
- else
254
- $ip = $_SERVER['REMOTE_ADDR'];
255
-
256
- return apply_filters( 'ta_get_user_ip_address', $ip );
257
- }
258
-
259
- /**
260
- * Get the thirstylink slug set on the settings.
261
- *
262
- * @since 3.0.0
263
- * @access public
264
- *
265
- * @return string $link_prefix Thirstyling link prefix.
266
- */
267
- public function get_thirstylink_link_prefix() {
268
-
269
- $link_prefix = get_option( 'ta_link_prefix' , 'recommends' );
270
-
271
- if ( $link_prefix === 'custom' )
272
- $link_prefix = get_option( 'ta_link_prefix_custom' , 'recommends' );
273
-
274
- return $link_prefix ? $link_prefix : 'recommends';
275
- }
276
-
277
- /**
278
- * Get the affiliate link post default category slug.
279
- *
280
- * @since 3.0.0
281
- * @access public
282
- *
283
- * @param int $link_id Affiliate Link ID.
284
- * @param array $terms Affiliate link categories.
285
- * @return string Affiliate link default category slug.
286
- */
287
- public function get_default_category_slug( $link_id , $terms = array() ) {
288
-
289
- if ( ! is_array( $terms ) || empty( $terms ) )
290
- $terms = get_the_terms( $link_id , Plugin_Constants::AFFILIATE_LINKS_TAX );
291
-
292
- if ( is_wp_error( $terms ) || empty( $terms ) )
293
- return;
294
-
295
- $link_cat_obj = array_shift( $terms );
296
-
297
- return $link_cat_obj->slug;
298
- }
299
-
300
- /**
301
- * Search affiliate links query
302
- *
303
- * @since 3.0.0
304
- * @access public
305
- *
306
- * @param string $keyword Search keyword.
307
- * @param int $paged WP_Query paged value.
308
- * @param string $category Affiliate link category to search.
309
- * @param array $exclude List of posts to be excluded.
310
- * @return array List of affiliate link IDs.
311
- */
312
- public function search_affiliate_links_query( $keyword = '' , $paged = 1 , $category = '' , $exclude = array() ) {
313
-
314
- $args = array(
315
- 'post_type' => Plugin_Constants::AFFILIATE_LINKS_CPT,
316
- 'post_status' => 'publish',
317
- 's' => $keyword,
318
- 'fields' => 'ids',
319
- 'paged' => $paged,
320
- 'post__not_in' => $exclude
321
- );
322
-
323
- if ( $category ) {
324
-
325
- $args[ 'tax_query' ] = array(
326
- array(
327
- 'taxonomy' => Plugin_Constants::AFFILIATE_LINKS_TAX,
328
- 'field' => 'slug',
329
- 'terms' => $category
330
- )
331
- );
332
- }
333
-
334
- $query = new \WP_Query( $args );
335
-
336
- return $query->posts;
337
- }
338
-
339
- /**
340
- * Check if affiliate link needs to be uncloaked.
341
- *
342
- * @deprecated 3.2.0
343
- *
344
- * @since 3.0.0
345
- * @access public
346
- *
347
- * @param Affiliate_Link $thirstylink Thirsty affiliate link object.
348
- * @return boolean Sets to true when affiliate link needs to be uncloaked.
349
- */
350
- public function is_uncloak_link( $thirstylink ) {
351
-
352
- return $thirstylink->is( 'uncloak_link' );
353
- }
354
-
355
- /**
356
- * Error log with a trace.
357
- *
358
- * @since 3.0.0
359
- * @access public
360
- */
361
- public function ta_error_log( $msg ) {
362
-
363
- $trace = debug_backtrace();
364
- $caller = array_shift( $trace );
365
-
366
- error_log( $msg . ' | Trace: ' . $caller[ 'file' ] . ' on line ' . $caller[ 'line' ] );
367
-
368
- }
369
-
370
- /**
371
- * Utility function that determines if a plugin is active or not.
372
- *
373
- * @since 3.0.0
374
- * @access public
375
- *
376
- * @param string $plugin_basename Plugin base name. Ex. woocommerce/woocommerce.php
377
- * @return boolean True if active, false otherwise.
378
- */
379
- public function is_plugin_active( $plugin_basename ) {
380
-
381
- // Makes sure the plugin is defined before trying to use it
382
- if ( !function_exists( 'is_plugin_active' ) )
383
- include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
384
-
385
- return is_plugin_active( $plugin_basename );
386
-
387
- }
388
-
389
- /**
390
- * Send email.
391
- *
392
- * @since 3.0.0
393
- * @access public
394
- *
395
- * @param array $recipients Array of recipients emails.
396
- * @param string $subject Email subject.
397
- * @param string $message Email message.
398
- * @param array $headers Array of email headers.
399
- * @param array $attachments Array of email attachments.
400
- * @return boolean True if email sending is triggered, note it does not mean that the email was received, it just denotes that the email sending is triggered. False if email sending is not triggered.
401
- */
402
- public function send_email( $recipients , $subject , $message , $headers = array() , $attachments = array() ) {
403
-
404
- $from_name = apply_filters( 'ta_email_from_name' , get_bloginfo( 'name' ) );
405
- $from_email = apply_filters( 'ta_email_from_email' , get_option( 'admin_email' ) );
406
-
407
- $headers[] = 'From: ' . $from_name . ' <' . $from_email . '>';
408
- $headers[] = 'Content-Type: text/html; charset=' . get_option( 'blog_charset' );
409
-
410
- return wp_mail( $recipients , $subject , $message , $headers , $attachments );
411
-
412
- }
413
-
414
- /**
415
- * Get Affiliate_Link data object.
416
- *
417
- * @since 3.0.0
418
- * @access public
419
- *
420
- * @param int $id Affiliate_Link post ID.
421
- * @return Affiliate_Link Affiliate Link object.
422
- */
423
- public function get_affiliate_link( $id = 0 ) {
424
-
425
- return new Affiliate_Link( $id );
426
- }
427
-
428
- /**
429
- * Retrieve all categories as an option array list.
430
- *
431
- * @since 3.0.0
432
- * @since 3.4.0 Add support for slug options.
433
- * @access public
434
- *
435
- * @return array List of category options.
436
- */
437
- public function get_all_category_as_options( $use_slug = false ) {
438
-
439
- $options = array();
440
-
441
- $categories = get_terms( array(
442
- 'taxonomy' => Plugin_Constants::AFFILIATE_LINKS_TAX,
443
- 'hide_empty' => false,
444
- ) );
445
-
446
- if ( ! is_wp_error( $categories ) ) {
447
-
448
- foreach( $categories as $category ) {
449
- $key = $use_slug ? $category->slug : $category->term_id;
450
- $options[ $key ] = $category->name;
451
- }
452
-
453
- } else {
454
-
455
- // TODO: Handle error
456
-
457
- }
458
-
459
- return $options;
460
- }
461
-
462
- /**
463
- * Set default term when affiliate link is saved.
464
- *
465
- * @since 3.2.0
466
- * @access public
467
- *
468
- * @param int $post_id Affiliate link post ID.
469
- */
470
- public function save_default_affiliate_link_category( $post_id ) {
471
-
472
- $default_category = Plugin_Constants::DEFAULT_LINK_CATEGORY;
473
- $taxonomy_slug = Plugin_Constants::AFFILIATE_LINKS_TAX;
474
-
475
- if ( get_option( 'ta_disable_cat_auto_select' ) == 'yes' || get_the_terms( $post_id , $taxonomy_slug ) )
476
- return;
477
-
478
- // create the default term if it doesn't exist
479
- if ( ! term_exists( $default_category , $taxonomy_slug ) )
480
- wp_insert_term( $default_category , $taxonomy_slug );
481
-
482
- $default_term = get_term_by( 'name' , $default_category , $taxonomy_slug );
483
-
484
- wp_set_post_terms( $post_id , $default_term->term_id , $taxonomy_slug );
485
- }
486
-
487
- /**
488
- * This function is an alias for WP get_option(), but will return the default value if option value is empty or invalid.
489
- *
490
- * @since 3.2.0
491
- * @access public
492
- *
493
- * @param string $option_name Name of the option of value to fetch.
494
- * @param mixed $default_value Defaut option value.
495
- * @return mixed Option value.
496
- */
497
- public function get_option( $option_name , $default_value = '' ) {
498
-
499
- $option_value = get_option( $option_name , $default_value );
500
-
501
- return ( gettype( $option_value ) === gettype( $default_value ) && $option_value ) ? $option_value : $default_value;
502
- }
503
-
504
- /**
505
- * Get blocked bots from settings or default value.
506
- *
507
- * @since 3.3.2
508
- * @access public
509
- *
510
- * @return array List of blocked bots.
511
- */
512
- public function get_blocked_bots() {
513
-
514
- $bots_string = $this->get_option( 'ta_blocked_bots' , Plugin_Constants::DEFAULT_BLOCKED_BOTS );
515
- return str_replace( ',' , '|' , $bots_string );
516
- }
517
-
518
- /**
519
- * Check if useragent is bot.
520
- *
521
- * @since 3.3.3
522
- * @access public
523
- *
524
- * @return bool True if detected as bot, otherwise false.
525
- */
526
- public function is_user_agent_bot() {
527
-
528
- $user_agent = isset( $_SERVER[ 'HTTP_USER_AGENT' ] ) ? strtolower( $_SERVER[ 'HTTP_USER_AGENT' ] ) : '';
529
- $bots = apply_filters( 'ta_useragent_bots_phrase_list' , $this->get_blocked_bots() );
530
- $pattern = '/' . $bots . '/i';
531
-
532
- return preg_match( $pattern , $user_agent );
533
- }
534
-
535
- /**
536
- * Get screen ID.
537
- *
538
- * @since 3.3.3
539
- * @access public
540
- */
541
- public function get_screen_id( $object_id ) {
542
-
543
- $screen_id = null;
544
-
545
- if ( isset( $_GET[ 'post_type' ] ) && $_GET[ 'post_type' ] == Plugin_Constants::AFFILIATE_LINKS_CPT ) {
546
-
547
- if ( isset( $_GET[ 'taxonomy' ] ) )
548
- $screen_id = 'edit-' . $_GET[ 'taxonomy' ];
549
- elseif ( isset( $_GET[ 'page' ] ) )
550
- $screen_id = 'thirstylink_page_' . $_GET[ 'page' ];
551
- else
552
- $screen_id = 'edit-thirstylink';
553
-
554
- } elseif ( $object_id )
555
- $screen_id = 'thirstylink';
556
-
557
- return apply_filters( 'ta_get_screen_id' , $screen_id );
558
- }
559
-
560
- /**
561
- * Get visistor's browser and device.
562
- *
563
- * @since 3.4.0
564
- * @access public
565
- *
566
- * @return string Browser and device.
567
- */
568
- public function get_visitor_browser_device() {
569
-
570
- if ( get_option( 'ta_disable_browser_device_collection' ) === 'yes' || ! ini_get( "browscap" ) )
571
- return;
572
-
573
- $browser = get_browser( null );
574
- return is_object( $browser ) ? $browser->browser . '|' . $browser->platform . '|' . $browser->device_type : '';
575
- }
576
-
577
- /**
578
- * Check if a page builder is active or not. Currently supports: Beaver Builder and Elementor.
579
- *
580
- * @since 3.4.0
581
- * @access public
582
- */
583
- public function is_page_builder_active() {
584
-
585
- $is_bb_active = class_exists( 'FLBuilderModel' ) && \FLBuilderModel::is_builder_active();
586
- $elementor = class_exists( 'Elementor\Editor' ) && isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'elementor';
587
-
588
- return $is_bb_active || $elementor;
589
- }
590
-
591
- }
1
+ <?php
2
+ namespace ThirstyAffiliates\Helpers;
3
+
4
+ use ThirstyAffiliates\Abstracts\Abstract_Main_Plugin_Class;
5
+
6
+ use ThirstyAffiliates\Models\Affiliate_Link;
7
+
8
+ if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
9
+
10
+ /**
11
+ * Model that houses all the helper functions of the plugin.
12
+ *
13
+ * 3.0.0
14
+ */
15
+ class Helper_Functions {
16
+
17
+ /*
18
+ |--------------------------------------------------------------------------
19
+ | Class Properties
20
+ |--------------------------------------------------------------------------
21
+ */
22
+
23
+ /**
24
+ * Property that holds the single main instance of Helper_Functions.
25
+ *
26
+ * @since 3.0.0
27
+ * @access private
28
+ * @var Helper_Functions
29
+ */
30
+ private static $_instance;
31
+
32
+ /**
33
+ * Model that houses all the plugin constants.
34
+ *
35
+ * @since 3.0.0
36
+ * @access private
37
+ * @var Plugin_Constants
38
+ */
39
+ private $_constants;
40
+
41
+ /**
42
+ * Property that houses all the saved settings.
43
+ *
44
+ * @since 3.0.0
45
+ * @access private
46
+ */
47
+ private $_settings = array();
48
+
49
+
50
+
51
+
52
+ /*
53
+ |--------------------------------------------------------------------------
54
+ | Class Methods
55
+ |--------------------------------------------------------------------------
56
+ */
57
+
58
+ /**
59
+ * Class constructor.
60
+ *
61
+ * @since 3.0.0
62
+ * @access public
63
+ *
64
+ * @param Abstract_Main_Plugin_Class $main_plugin Main plugin object.
65
+ * @param Plugin_Constants $constants Plugin constants object.
66
+ */
67
+ public function __construct( Abstract_Main_Plugin_Class $main_plugin , Plugin_Constants $constants ) {
68
+
69
+ $this->_constants = $constants;
70
+
71
+ $main_plugin->add_to_public_helpers( $this );
72
+
73
+ }
74
+
75
+ /**
76
+ * Ensure that only one instance of this class is loaded or can be loaded ( Singleton Pattern ).
77
+ *
78
+ * @since 3.0.0
79
+ * @access public
80
+ *
81
+ * @param Abstract_Main_Plugin_Class $main_plugin Main plugin object.
82
+ * @param Plugin_Constants $constants Plugin constants object.
83
+ * @return Helper_Functions
84
+ */
85
+ public static function get_instance( Abstract_Main_Plugin_Class $main_plugin , Plugin_Constants $constants ) {
86
+
87
+ if ( !self::$_instance instanceof self )
88
+ self::$_instance = new self( $main_plugin , $constants );
89
+
90
+ return self::$_instance;
91
+
92
+ }
93
+
94
+
95
+
96
+
97
+ /*
98
+ |--------------------------------------------------------------------------
99
+ | Helper Functions
100
+ |--------------------------------------------------------------------------
101
+ */
102
+
103
+ /**
104
+ * Write data to plugin log file.
105
+ *
106
+ * @since 3.0.0
107
+ * @access public
108
+ *
109
+ * @param mixed Data to log.
110
+ */
111
+ public function write_debug_log( $log ) {
112
+
113
+ error_log( "\n[" . current_time( 'mysql' ) . "]\n" . $log . "\n--------------------------------------------------\n" , 3 , $this->_constants->LOGS_ROOT_PATH() . 'debug.log' );
114
+
115
+ }
116
+
117
+ /**
118
+ * Check if current user is authorized to manage the plugin on the backend.
119
+ *
120
+ * @since 3.0.0
121
+ * @access public
122
+ *
123
+ * @param WP_User $user WP_User object.
124
+ * @return boolean True if authorized, False otherwise.
125
+ */
126
+ public function current_user_authorized( $user = null ) {
127
+
128
+ // Array of roles allowed to access/utilize the plugin
129
+ $admin_roles = apply_filters( 'ucfw_admin_roles' , array( 'administrator' ) );
130
+
131
+ if ( is_null( $user ) )
132
+ $user = wp_get_current_user();
133
+
134
+ if ( $user->ID )
135
+ return count( array_intersect( ( array ) $user->roles , $admin_roles ) ) ? true : false;
136
+ else
137
+ return false;
138
+
139
+ }
140
+
141
+ /**
142
+ * Returns the timezone string for a site, even if it's set to a UTC offset
143
+ *
144
+ * Adapted from http://www.php.net/manual/en/function.timezone-name-from-abbr.php#89155
145
+ *
146
+ * Reference:
147
+ * http://www.skyverge.com/blog/down-the-rabbit-hole-wordpress-and-timezones/
148
+ *
149
+ * @since 3.0.0
150
+ * @access public
151
+ *
152
+ * @return string Valid PHP timezone string
153
+ */
154
+ public function get_site_current_timezone() {
155
+
156
+ // if site timezone string exists, return it
157
+ if ( $timezone = get_option( 'timezone_string' ) )
158
+ return $timezone;
159
+
160
+ // get UTC offset, if it isn't set then return UTC
161
+ if ( 0 === ( $utc_offset = get_option( 'gmt_offset', 0 ) ) )
162
+ return 'UTC';
163
+
164
+ return $this->convert_utc_offset_to_timezone( $utc_offset );
165
+
166
+ }
167
+
168
+ /**
169
+ * Conver UTC offset to timezone.
170
+ *
171
+ * @since 1.2.0
172
+ * @access public
173
+ *
174
+ * @param float|int|string $utc_offset UTC offset.
175
+ * @return string valid PHP timezone string
176
+ */
177
+ public function convert_utc_offset_to_timezone( $utc_offset ) {
178
+
179
+ // adjust UTC offset from hours to seconds
180
+ $utc_offset *= 3600;
181
+
182
+ // attempt to guess the timezone string from the UTC offset
183
+ if ( $timezone = timezone_name_from_abbr( '' , $utc_offset , 0 ) )
184
+ return $timezone;
185
+
186
+ // last try, guess timezone string manually
187
+ $is_dst = date( 'I' );
188
+
189
+ foreach ( timezone_abbreviations_list() as $abbr )
190
+ foreach ( $abbr as $city )
191
+ if ( $city[ 'dst' ] == $is_dst && $city[ 'offset' ] == $utc_offset )
192
+ return $city[ 'timezone_id' ];
193
+
194
+ // fallback to UTC
195
+ return 'UTC';
196
+
197
+ }
198
+
199
+ /**
200
+ * Get all user roles.
201
+ *
202
+ * @since 3.0.0
203
+ * @access public
204
+ *
205
+ * @global WP_Roles $wp_roles Core class used to implement a user roles API.
206
+ *
207
+ * @return array Array of all site registered user roles. User role key as the key and value is user role text.
208
+ */
209
+ public function get_all_user_roles() {
210
+
211
+ global $wp_roles;
212
+ return $wp_roles->get_names();
213
+
214
+ }
215
+
216
+ /**
217
+ * Check validity of a save post action.
218
+ *
219
+ * @since 3.0.0
220
+ * @access private
221
+ *
222
+ * @param int $post_id Id of the coupon post.
223
+ * @param string $post_type Post type to check.
224
+ * @return bool True if valid save post action, False otherwise.
225
+ */
226
+ public function check_if_valid_save_post_action( $post_id , $post_type ) {
227
+
228
+ if ( get_post_type() != $post_type || empty( $_POST ) || wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) || !current_user_can( 'edit_page' , $post_id ) )
229
+ return false;
230
+ else
231
+ return true;
232
+
233
+ }
234
+
235
+ /**
236
+ * Get user IP address.
237
+ *
238
+ * @since 3.0.0
239
+ * @since 3.3.2 Added condition to disable IP address collection (for GDRP compliance).
240
+ * @access public
241
+ *
242
+ * @return string User's IP address.
243
+ */
244
+ public function get_user_ip_address() {
245
+
246
+ if ( get_option( 'ta_disable_ip_address_collection' ) === 'yes' )
247
+ return;
248
+
249
+ if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) )
250
+ $ip = $_SERVER['HTTP_CLIENT_IP'];
251
+ elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
252
+ $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
253
+ else
254
+ $ip = $_SERVER['REMOTE_ADDR'];
255
+
256
+ return apply_filters( 'ta_get_user_ip_address', $ip );
257
+ }
258
+
259
+ /**
260
+ * Get the thirstylink slug set on the settings.
261
+ *
262
+ * @since 3.0.0
263
+ * @access public
264
+ *
265
+ * @return string $link_prefix Thirstyling link prefix.
266
+ */
267
+ public function get_thirstylink_link_prefix() {
268
+
269
+ $link_prefix = get_option( 'ta_link_prefix' , 'recommends' );
270
+
271
+ if ( $link_prefix === 'custom' )
272
+ $link_prefix = get_option( 'ta_link_prefix_custom' , 'recommends' );
273
+
274
+ return $link_prefix ? $link_prefix : 'recommends';
275
+ }
276
+
277
+ /**
278
+ * Get the affiliate link post default category slug.
279
+ *
280
+ * @since 3.0.0
281
+ * @access public
282
+ *
283
+ * @param int $link_id Affiliate Link ID.
284
+ * @param array $terms Affiliate link categories.
285
+ * @return string Affiliate link default category slug.
286
+ */
287
+ public function get_default_category_slug( $link_id , $terms = array() ) {
288
+
289
+ if ( ! is_array( $terms ) || empty( $terms ) )
290
+ $terms = get_the_terms( $link_id , Plugin_Constants::AFFILIATE_LINKS_TAX );
291
+
292
+ if ( is_wp_error( $terms ) || empty( $terms ) )
293
+ return;
294
+
295
+ $link_cat_obj = array_shift( $terms );
296
+
297
+ return $link_cat_obj->slug;
298
+ }
299
+
300
+ /**
301
+ * Search affiliate links query
302
+ *
303
+ * @since 3.0.0
304
+ * @access public
305
+ *
306
+ * @param string $keyword Search keyword.
307
+ * @param int $paged WP_Query paged value.
308
+ * @param string $category Affiliate link category to search.
309
+ * @param array $exclude List of posts to be excluded.
310
+ * @return array List of affiliate link IDs.
311
+ */
312
+ public function search_affiliate_links_query( $keyword = '' , $paged = 1 , $category = '' , $exclude = array() ) {
313
+
314
+ $args = array(
315
+ 'post_type' => Plugin_Constants::AFFILIATE_LINKS_CPT,
316
+ 'post_status' => 'publish',
317
+ 's' => $keyword,
318
+ 'fields' => 'ids',
319
+ 'paged' => $paged,
320
+ 'post__not_in' => $exclude
321
+ );
322
+
323
+ if ( $category ) {
324
+
325
+ $args[ 'tax_query' ] = array(
326
+ array(
327
+ 'taxonomy' => Plugin_Constants::AFFILIATE_LINKS_TAX,
328
+ 'field' => 'slug',
329
+ 'terms' => $category
330
+ )
331
+ );
332
+ }
333
+
334
+ $query = new \WP_Query( $args );
335
+
336
+ return $query->posts;
337
+ }
338
+
339
+ /**
340
+ * Check if affiliate link needs to be uncloaked.
341
+ *
342
+ * @deprecated 3.2.0
343
+ *
344
+ * @since 3.0.0
345
+ * @access public
346
+ *
347
+ * @param Affiliate_Link $thirstylink Thirsty affiliate link object.
348
+ * @return boolean Sets to true when affiliate link needs to be uncloaked.
349
+ */
350
+ public function is_uncloak_link( $thirstylink ) {
351
+
352
+ return $thirstylink->is( 'uncloak_link' );
353
+ }
354
+
355
+ /**
356
+ * Error log with a trace.
357
+ *
358
+ * @since 3.0.0
359
+ * @access public
360
+ */
361
+ public function ta_error_log( $msg ) {
362
+
363
+ $trace = debug_backtrace();
364
+ $caller = array_shift( $trace );
365
+
366
+ error_log( $msg . ' | Trace: ' . $caller[ 'file' ] . ' on line ' . $caller[ 'line' ] );
367
+
368
+ }
369
+
370
+ /**
371
+ * Utility function that determines if a plugin is active or not.
372
+ *
373
+ * @since 3.0.0
374
+ * @access public
375
+ *
376
+ * @param string $plugin_basename Plugin base name. Ex. woocommerce/woocommerce.php
377
+ * @return boolean True if active, false otherwise.
378
+ */
379
+ public function is_plugin_active( $plugin_basename ) {
380
+
381
+ // Makes sure the plugin is defined before trying to use it
382
+ if ( !function_exists( 'is_plugin_active' ) )
383
+ include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
384
+
385
+ return is_plugin_active( $plugin_basename );
386
+
387
+ }
388
+
389
+ /**
390
+ * Send email.
391
+ *
392
+ * @since 3.0.0
393
+ * @access public
394
+ *
395
+ * @param array $recipients Array of recipients emails.
396
+ * @param string $subject Email subject.
397
+ * @param string $message Email message.
398
+ * @param array $headers Array of email headers.
399
+ * @param array $attachments Array of email attachments.
400
+ * @return boolean True if email sending is triggered, note it does not mean that the email was received, it just denotes that the email sending is triggered. False if email sending is not triggered.
401
+ */
402
+ public function send_email( $recipients , $subject , $message , $headers = array() , $attachments = array() ) {
403
+
404
+ $from_name = apply_filters( 'ta_email_from_name' , get_bloginfo( 'name' ) );
405
+ $from_email = apply_filters( 'ta_email_from_email' , get_option( 'admin_email' ) );
406
+
407
+ $headers[] = 'From: ' . $from_name . ' <' . $from_email . '>';
408
+ $headers[] = 'Content-Type: text/html; charset=' . get_option( 'blog_charset' );
409
+
410
+ return wp_mail( $recipients , $subject , $message , $headers , $attachments );
411
+
412
+ }
413
+
414
+ /**
415
+ * Get Affiliate_Link data object.
416
+ *
417
+ * @since 3.0.0
418
+ * @access public
419
+ *
420
+ * @param int $id Affiliate_Link post ID.
421
+ * @return Affiliate_Link Affiliate Link object.
422
+ */
423
+ public function get_affiliate_link( $id = 0 ) {
424
+
425
+ return new Affiliate_Link( $id );
426
+ }
427
+
428
+ /**
429
+ * Retrieve all categories as an option array list.
430
+ *
431
+ * @since 3.0.0
432
+ * @since 3.4.0 Add support for slug options.
433
+ * @access public
434
+ *
435
+ * @return array List of category options.
436
+ */
437
+ public function get_all_category_as_options( $use_slug = false ) {
438
+
439
+ $options = array();
440
+
441
+ $categories = get_terms( array(
442
+ 'taxonomy' => Plugin_Constants::AFFILIATE_LINKS_TAX,
443
+ 'hide_empty' => false,
444
+ ) );
445
+
446
+ if ( ! is_wp_error( $categories ) ) {
447
+
448
+ foreach( $categories as $category ) {
449
+ $key = $use_slug ? $category->slug : $category->term_id;
450
+ $options[ $key ] = $category->name;
451
+ }
452
+
453
+ } else {
454
+
455
+ // TODO: Handle error
456
+
457
+ }
458
+
459
+ return $options;
460
+ }
461
+
462
+ /**
463
+ * Set default term when affiliate link is saved.
464
+ *
465
+ * @since 3.2.0
466
+ * @access public
467
+ *
468
+ * @param int $post_id Affiliate link post ID.
469
+ */
470
+ public function save_default_affiliate_link_category( $post_id ) {
471
+
472
+ $default_category = Plugin_Constants::DEFAULT_LINK_CATEGORY;
473
+ $taxonomy_slug = Plugin_Constants::AFFILIATE_LINKS_TAX;
474
+
475
+ if ( get_option( 'ta_disable_cat_auto_select' ) == 'yes' || get_the_terms( $post_id , $taxonomy_slug ) )
476
+ return;
477
+
478
+ // create the default term if it doesn't exist
479
+ if ( ! term_exists( $default_category , $taxonomy_slug ) )
480
+ wp_insert_term( $default_category , $taxonomy_slug );
481
+
482
+ $default_term = get_term_by( 'name' , $default_category , $taxonomy_slug );
483
+
484
+ wp_set_post_terms( $post_id , $default_term->term_id , $taxonomy_slug );
485
+ }
486
+
487
+ /**
488
+ * This function is an alias for WP get_option(), but will return the default value if option value is empty or invalid.
489
+ *
490
+ * @since 3.2.0
491
+ * @access public
492
+ *
493
+ * @param string $option_name Name of the option of value to fetch.
494
+ * @param mixed $default_value Defaut option value.
495
+ * @return mixed Option value.
496
+ */
497
+ public function get_option( $option_name , $default_value = '' ) {
498
+
499
+ $option_value = get_option( $option_name , $default_value );
500
+
501
+ return ( gettype( $option_value ) === gettype( $default_value ) && $option_value ) ? $option_value : $default_value;
502
+ }
503
+
504
+ /**
505
+ * Get blocked bots from settings or default value.
506
+ *
507
+ * @since 3.3.2
508
+ * @access public
509
+ *
510
+ * @return array List of blocked bots.
511
+ */
512
+ public function get_blocked_bots() {
513
+
514
+ $bots_string = $this->get_option( 'ta_blocked_bots' , Plugin_Constants::DEFAULT_BLOCKED_BOTS );
515
+ return str_replace( ',' , '|' , $bots_string );
516
+ }
517
+
518
+ /**
519
+ * Check if useragent is bot.
520
+ *
521
+ * @since 3.3.3
522
+ * @access public
523
+ *
524
+ * @return bool True if detected as bot, otherwise false.
525
+ */
526
+ public function is_user_agent_bot() {
527
+
528
+ $user_agent = isset( $_SERVER[ 'HTTP_USER_AGENT' ] ) ? strtolower( $_SERVER[ 'HTTP_USER_AGENT' ] ) : '';
529
+ $bots = apply_filters( 'ta_useragent_bots_phrase_list' , $this->get_blocked_bots() );
530
+ $pattern = '/' . $bots . '/i';
531
+
532
+ return preg_match( $pattern , $user_agent );
533
+ }
534
+
535
+ /**
536
+ * Get screen ID.
537
+ *
538
+ * @since 3.3.3
539
+ * @access public
540
+ */
541
+ public function get_screen_id( $object_id ) {
542
+
543
+ $screen_id = null;
544
+
545
+ if ( isset( $_GET[ 'post_type' ] ) && $_GET[ 'post_type' ] == Plugin_Constants::AFFILIATE_LINKS_CPT ) {
546
+
547
+ if ( isset( $_GET[ 'taxonomy' ] ) )
548
+ $screen_id = 'edit-' . $_GET[ 'taxonomy' ];
549
+ elseif ( isset( $_GET[ 'page' ] ) )
550
+ $screen_id = 'thirstylink_page_' . $_GET[ 'page' ];
551
+ else
552
+ $screen_id = 'edit-thirstylink';
553
+
554
+ } elseif ( $object_id )
555
+ $screen_id = 'thirstylink';
556
+
557
+ return apply_filters( 'ta_get_screen_id' , $screen_id );
558
+ }
559
+
560
+ /**
561
+ * Get visistor's browser and device.
562
+ *
563
+ * @since 3.4.0
564
+ * @access public
565
+ *
566
+ * @return string Browser and device.
567
+ */
568
+ public function get_visitor_browser_device() {
569
+
570
+ if ( get_option( 'ta_disable_browser_device_collection' ) === 'yes' || ! ini_get( "browscap" ) )
571
+ return;
572
+
573
+ $browser = get_browser( null );
574
+ return is_object( $browser ) ? $browser->browser . '|' . $browser->platform . '|' . $browser->device_type : '';
575
+ }
576
+
577
+ /**
578
+ * Check if a page builder is active or not. Currently supports: Beaver Builder and Elementor.
579
+ *
580
+ * @since 3.4.0
581
+ * @access public
582
+ */
583
+ public function is_page_builder_active() {
584
+
585
+ $is_bb_active = class_exists( 'FLBuilderModel' ) && \FLBuilderModel::is_builder_active();
586
+ $elementor = class_exists( 'Elementor\Editor' ) && isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'elementor';
587
+
588
+ return $is_bb_active || $elementor;
589
+ }
590
+
591
+ }
Helpers/Plugin_Constants.php CHANGED
@@ -1,164 +1,164 @@
1
- <?php
2
- namespace ThirstyAffiliates\Helpers;
3
-
4
- use ThirstyAffiliates\Abstracts\Abstract_Main_Plugin_Class;
5
-
6
- if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
7
-
8
- /**
9
- * Model that houses all the plugin constants.
10
- * Note as much as possible, we need to make this class succinct as the only purpose of this is to house all the constants that is utilized by the plugin.
11
- * Therefore we omit class member comments and minimize comments as much as possible.
12
- * In fact the only verbouse comment here is this comment you are reading right now.
13
- * And guess what, it just got worse coz now this comment takes 5 lines instead of 3.
14
- *
15
- * @since 3.0.0
16
- */
17
- class Plugin_Constants {
18
-
19
- /*
20
- |--------------------------------------------------------------------------
21
- | Class Properties
22
- |--------------------------------------------------------------------------
23
- */
24
-
25
- private static $_instance;
26
-
27
- // Plugin configuration constants
28
- const TOKEN = 'ta';
29
- const INSTALLED_VERSION = 'ta_installed_version';
30
- const VERSION = '3.4.0';
31
- const TEXT_DOMAIN = 'thirstyaffiliates';
32
- const THEME_TEMPLATE_PATH = 'thirstyaffiliates';
33
- const META_DATA_PREFIX = '_ta_';
34
-
35
- // CPT Taxonomy constants
36
- const AFFILIATE_LINKS_CPT = 'thirstylink';
37
- const AFFILIATE_LINKS_TAX = 'thirstylink-category';
38
- const DEFAULT_LINK_CATEGORY = 'Uncategorized';
39
-
40
- // CRON
41
- const CRON_REQUEST_REVIEW = 'ta_cron_request_review';
42
- const CRON_MIGRATE_OLD_PLUGIN_DATA = 'ta_cron_migrate_old_plugin_data';
43
- const CRON_TAPRO_NOTICE = 'ta_cron_tapro_notice';
44
- const CRON_STATS_TRIMMER = 'ta_cron_stats_trimmer';
45
-
46
- // Options
47
- const SHOW_REQUEST_REVIEW = 'ta_show_request_review';
48
- const REVIEW_REQUEST_RESPONSE = 'ta_request_review_response';
49
- const MIGRATION_COMPLETE_FLAG = 'ta_migration_complete_flag';
50
- const SHOW_TAPRO_NOTICE = 'ta_show_tapro_notice';
51
-
52
- // Settings Constants
53
- const DEFAULT_BLOCKED_BOTS = 'googlebot,bingbot,Slurp,DuckDuckBot,Baiduspider,YandexBot,Sogou,Exabot,facebo,ia_archiver';
54
-
55
- // DB Tables
56
- const LINK_CLICK_DB = 'ta_link_clicks';
57
- const LINK_CLICK_META_DB = 'ta_link_clicks_meta';
58
-
59
- // Help Section
60
- const CLEAN_UP_PLUGIN_OPTIONS = 'ta_clean_up_plugin_options';
61
-
62
-
63
-
64
-
65
- /*
66
- |--------------------------------------------------------------------------
67
- | Class Methods
68
- |--------------------------------------------------------------------------
69
- */
70
-
71
- public function __construct( Abstract_Main_Plugin_Class $main_plugin ) {
72
-
73
- // Path constants
74
- $this->_MAIN_PLUGIN_FILE_PATH = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . 'thirstyaffiliates' . DIRECTORY_SEPARATOR . 'thirstyaffiliates.php';
75
- $this->_PLUGIN_DIR_PATH = plugin_dir_path( $this->_MAIN_PLUGIN_FILE_PATH );
76
- $this->_PLUGIN_DIR_URL = plugin_dir_url( $this->_MAIN_PLUGIN_FILE_PATH );
77
- $this->_PLUGIN_DIRNAME = plugin_basename( dirname( $this->_MAIN_PLUGIN_FILE_PATH ) );
78
- $this->_PLUGIN_BASENAME = plugin_basename( $this->_MAIN_PLUGIN_FILE_PATH );
79
-
80
- $this->_CSS_ROOT_URL = $this->_PLUGIN_DIR_URL . 'css/';
81
- $this->_IMAGES_ROOT_URL = $this->_PLUGIN_DIR_URL . 'images/';
82
- $this->_JS_ROOT_URL = $this->_PLUGIN_DIR_URL . 'js/';
83
-
84
- $this->_VIEWS_ROOT_PATH = $this->_PLUGIN_DIR_PATH . 'views/';
85
- $this->_TEMPLATES_ROOT_PATH = $this->_PLUGIN_DIR_PATH . 'templates/';
86
- $this->_LOGS_ROOT_PATH = $this->_PLUGIN_DIR_PATH . 'logs/';
87
-
88
- $this->_REDIRECT_TYPES = apply_filters( 'ta_redirect_types' , array(
89
- '301' => __( '301 Permanent' , 'thirstyaffiliates' ),
90
- '302' => __( '302 Temporary' , 'thirstyaffiliates' ),
91
- '307' => __( '307 Temporary (alternative)' , 'thirstyaffiliates' )
92
- ) );
93
-
94
- $main_plugin->add_to_public_helpers( $this );
95
-
96
- }
97
-
98
- public static function get_instance( Abstract_Main_Plugin_Class $main_plugin ) {
99
-
100
- if ( !self::$_instance instanceof self )
101
- self::$_instance = new self( $main_plugin );
102
-
103
- return self::$_instance;
104
-
105
- }
106
-
107
- public function VERSION() {
108
- return self::VERSION;
109
- }
110
-
111
- public function MAIN_PLUGIN_FILE_PATH() {
112
- return $this->_MAIN_PLUGIN_FILE_PATH;
113
- }
114
-
115
- public function PLUGIN_DIR_PATH() {
116
- return $this->_PLUGIN_DIR_PATH;
117
- }
118
-
119
- public function PLUGIN_DIR_URL() {
120
- return $this->_PLUGIN_DIR_URL;
121
- }
122
-
123
- public function PLUGIN_DIRNAME() {
124
- return $this->_PLUGIN_DIRNAME;
125
- }
126
-
127
- public function PLUGIN_BASENAME() {
128
- return $this->_PLUGIN_BASENAME;
129
- }
130
-
131
- public function CSS_ROOT_URL() {
132
- return $this->_CSS_ROOT_URL;
133
- }
134
-
135
- public function IMAGES_ROOT_URL() {
136
- return $this->_IMAGES_ROOT_URL;
137
- }
138
-
139
- public function JS_ROOT_URL() {
140
- return $this->_JS_ROOT_URL;
141
- }
142
-
143
- public function VIEWS_ROOT_PATH() {
144
- return $this->_VIEWS_ROOT_PATH;
145
- }
146
-
147
- public function TEMPLATES_ROOT_PATH() {
148
- return $this->_TEMPLATES_ROOT_PATH;
149
- }
150
-
151
- public function LOGS_ROOT_PATH() {
152
- return $this->_LOGS_ROOT_PATH;
153
- }
154
-
155
- public function REDIRECT_TYPES() {
156
- return $this->_REDIRECT_TYPES;
157
- }
158
-
159
- // HTAccess Module
160
- public function HTACCESS_FILE() {
161
- return ABSPATH . '/.htaccess';
162
- }
163
-
164
- }
1
+ <?php
2
+ namespace ThirstyAffiliates\Helpers;
3
+
4
+ use ThirstyAffiliates\Abstracts\Abstract_Main_Plugin_Class;
5
+
6
+ if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
7
+
8
+ /**
9
+ * Model that houses all the plugin constants.
10
+ * Note as much as possible, we need to make this class succinct as the only purpose of this is to house all the constants that is utilized by the plugin.
11
+ * Therefore we omit class member comments and minimize comments as much as possible.
12
+ * In fact the only verbouse comment here is this comment you are reading right now.
13
+ * And guess what, it just got worse coz now this comment takes 5 lines instead of 3.
14
+ *
15
+ * @since 3.0.0
16
+ */
17
+ class Plugin_Constants {
18
+
19
+ /*
20
+ |--------------------------------------------------------------------------
21
+ | Class Properties
22
+ |--------------------------------------------------------------------------
23
+ */
24
+
25
+ private static $_instance;
26
+
27
+ // Plugin configuration constants
28
+ const TOKEN = 'ta';
29
+ const INSTALLED_VERSION = 'ta_installed_version';
30
+ const VERSION = '3.5';
31
+ const TEXT_DOMAIN = 'thirstyaffiliates';
32
+ const THEME_TEMPLATE_PATH = 'thirstyaffiliates';
33
+ const META_DATA_PREFIX = '_ta_';
34
+
35
+ // CPT Taxonomy constants
36
+ const AFFILIATE_LINKS_CPT = 'thirstylink';
37
+ const AFFILIATE_LINKS_TAX = 'thirstylink-category';
38
+ const DEFAULT_LINK_CATEGORY = 'Uncategorized';
39
+
40
+ // CRON
41
+ const CRON_REQUEST_REVIEW = 'ta_cron_request_review';
42
+ const CRON_MIGRATE_OLD_PLUGIN_DATA = 'ta_cron_migrate_old_plugin_data';
43
+ const CRON_TAPRO_NOTICE = 'ta_cron_tapro_notice';
44
+ const CRON_STATS_TRIMMER = 'ta_cron_stats_trimmer';
45
+
46
+ // Options
47
+ const SHOW_REQUEST_REVIEW = 'ta_show_request_review';
48
+ const REVIEW_REQUEST_RESPONSE = 'ta_request_review_response';
49
+ const MIGRATION_COMPLETE_FLAG = 'ta_migration_complete_flag';
50
+ const SHOW_TAPRO_NOTICE = 'ta_show_tapro_notice';
51
+
52
+ // Settings Constants
53
+ const DEFAULT_BLOCKED_BOTS = 'googlebot,bingbot,Slurp,DuckDuckBot,Baiduspider,YandexBot,Sogou,Exabot,facebo,ia_archiver';
54
+
55
+ // DB Tables
56
+ const LINK_CLICK_DB = 'ta_link_clicks';
57
+ const LINK_CLICK_META_DB = 'ta_link_clicks_meta';
58
+
59
+ // Help Section
60
+ const CLEAN_UP_PLUGIN_OPTIONS = 'ta_clean_up_plugin_options';
61
+
62
+
63
+
64
+
65
+ /*
66
+ |--------------------------------------------------------------------------
67
+ | Class Methods
68
+ |--------------------------------------------------------------------------
69
+ */
70
+
71
+ public function __construct( Abstract_Main_Plugin_Class $main_plugin ) {
72
+
73
+ // Path constants
74
+ $this->_MAIN_PLUGIN_FILE_PATH = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . 'thirstyaffiliates' . DIRECTORY_SEPARATOR . 'thirstyaffiliates.php';
75
+ $this->_PLUGIN_DIR_PATH = plugin_dir_path( $this->_MAIN_PLUGIN_FILE_PATH );
76
+ $this->_PLUGIN_DIR_URL = plugin_dir_url( $this->_MAIN_PLUGIN_FILE_PATH );
77
+ $this->_PLUGIN_DIRNAME = plugin_basename( dirname( $this->_MAIN_PLUGIN_FILE_PATH ) );
78
+ $this->_PLUGIN_BASENAME = plugin_basename( $this->_MAIN_PLUGIN_FILE_PATH );
79
+
80
+ $this->_CSS_ROOT_URL = $this->_PLUGIN_DIR_URL . 'css/';
81
+ $this->_IMAGES_ROOT_URL = $this->_PLUGIN_DIR_URL . 'images/';
82
+ $this->_JS_ROOT_URL = $this->_PLUGIN_DIR_URL . 'js/';
83
+
84
+ $this->_VIEWS_ROOT_PATH = $this->_PLUGIN_DIR_PATH . 'views/';
85
+ $this->_TEMPLATES_ROOT_PATH = $this->_PLUGIN_DIR_PATH . 'templates/';
86
+ $this->_LOGS_ROOT_PATH = $this->_PLUGIN_DIR_PATH . 'logs/';
87
+
88
+ $this->_REDIRECT_TYPES = apply_filters( 'ta_redirect_types' , array(
89
+ '301' => __( '301 Permanent' , 'thirstyaffiliates' ),
90
+ '302' => __( '302 Temporary' , 'thirstyaffiliates' ),
91
+ '307' => __( '307 Temporary (alternative)' , 'thirstyaffiliates' )
92
+ ) );
93
+
94
+ $main_plugin->add_to_public_helpers( $this );
95
+
96
+ }
97
+
98
+ public static function get_instance( Abstract_Main_Plugin_Class $main_plugin ) {
99
+
100
+ if ( !self::$_instance instanceof self )
101
+ self::$_instance = new self( $main_plugin );
102
+
103
+ return self::$_instance;
104
+
105
+ }
106
+
107
+ public function VERSION() {
108
+ return self::VERSION;
109
+ }
110
+
111
+ public function MAIN_PLUGIN_FILE_PATH() {
112
+ return $this->_MAIN_PLUGIN_FILE_PATH;
113
+ }
114
+
115
+ public function PLUGIN_DIR_PATH() {
116
+ return $this->_PLUGIN_DIR_PATH;
117
+ }
118
+
119
+ public function PLUGIN_DIR_URL() {
120
+ return $this->_PLUGIN_DIR_URL;
121
+ }
122
+
123
+ public function PLUGIN_DIRNAME() {
124
+ return $this->_PLUGIN_DIRNAME;
125
+ }
126
+
127
+ public function PLUGIN_BASENAME() {
128
+ return $this->_PLUGIN_BASENAME;
129
+ }
130
+
131
+ public function CSS_ROOT_URL() {
132
+ return $this->_CSS_ROOT_URL;
133
+ }
134
+
135
+ public function IMAGES_ROOT_URL() {
136
+ return $this->_IMAGES_ROOT_URL;
137
+ }
138
+
139
+ public function JS_ROOT_URL() {
140
+ return $this->_JS_ROOT_URL;
141
+ }
142
+
143
+ public function VIEWS_ROOT_PATH() {
144
+ return $this->_VIEWS_ROOT_PATH;
145
+ }
146
+
147
+ public function TEMPLATES_ROOT_PATH() {
148
+ return $this->_TEMPLATES_ROOT_PATH;
149
+ }
150
+
151
+ public function LOGS_ROOT_PATH() {
152
+ return $this->_LOGS_ROOT_PATH;
153
+ }
154
+
155
+ public function REDIRECT_TYPES() {
156
+ return $this->_REDIRECT_TYPES;
157
+ }
158
+
159
+ // HTAccess Module
160
+ public function HTACCESS_FILE() {
161
+ return ABSPATH . '/.htaccess';
162
+ }
163
+
164
+ }
Helpers/index.php CHANGED
File without changes
Interfaces/Activatable_Interface.php CHANGED
File without changes
Interfaces/Deactivatable_Interface.php CHANGED
@@ -1,22 +1,22 @@
1
- <?php
2
- namespace ThirstyAffiliates\Interfaces;
3
-
4
- if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
5
-
6
- /**
7
- * Abstraction that provides contract relating to deactivation.
8
- * Any model that needs some sort of deactivation must implement this interface.
9
- *
10
- * @since 3.1.0
11
- */
12
- interface Deactivatable_Interface {
13
-
14
- /**
15
- * Contract for deactivation.
16
- *
17
- * @since 3.1.0
18
- * @access public
19
- */
20
- public function deactivate();
21
-
22
- }
1
+ <?php
2
+ namespace ThirstyAffiliates\Interfaces;
3
+
4
+ if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
5
+
6
+ /**
7
+ * Abstraction that provides contract relating to deactivation.
8
+ * Any model that needs some sort of deactivation must implement this interface.
9
+ *
10
+ * @since 3.1.0
11
+ */
12
+ interface Deactivatable_Interface {
13
+
14
+ /**
15
+ * Contract for deactivation.
16
+ *
17
+ * @since 3.1.0
18
+ * @access public
19
+ */
20
+ public function deactivate();
21
+
22
+ }
Interfaces/Initiable_Interface.php CHANGED
File without changes
Interfaces/Model_Interface.php CHANGED
File without changes
Interfaces/index.php CHANGED
File without changes
Models/Affiliate_Link.php CHANGED
@@ -1,768 +1,768 @@
1
- <?php
2
-
3
- namespace ThirstyAffiliates\Models;
4
-
5
- use ThirstyAffiliates\Abstracts\Abstract_Main_Plugin_Class;
6
-
7
- use ThirstyAffiliates\Interfaces\Model_Interface;
8
-
9
- use ThirstyAffiliates\Helpers\Plugin_Constants;
10
- use ThirstyAffiliates\Helpers\Helper_Functions;
11
-
12
- /**
13
- * Model that houses the data model of an affiliate link.
14
- *
15
- * @since 3.0.0
16
- */
17
- class Affiliate_Link {
18
-
19
-
20
- /*
21
- |--------------------------------------------------------------------------
22
- | Class Properties
23
- |--------------------------------------------------------------------------
24
- */
25
-
26
- /**
27
- * Model that houses the main plugin object.
28
- *
29
- * @since 3.0.0
30
- * @access private
31
- * @var Abstract_Main_Plugin_Class
32
- */
33
- private $_main_plugin;
34
-
35
- /**
36
- * Model that houses all the plugin constants.
37
- *
38
- * @since 3.0.0
39
- * @access private
40
- * @var Plugin_Constants
41
- */
42
- private $_constants;
43
-
44
- /**
45
- * Property that houses all the helper functions of the plugin.
46
- *
47
- * @since 3.0.0
48
- * @access private
49
- * @var Helper_Functions
50
- */
51
- private $_helper_functions;
52
-
53
- /**
54
- * Stores affiliate link ID.
55
- *
56
- * @since 3.0.0
57
- * @access protected
58
- * @var array
59
- */
60
- protected $id;
61
-
62
- /**
63
- * Stores affiliate link data.
64
- *
65
- * @since 3.0.0
66
- * @access protected
67
- * @var array
68
- */
69
- protected $data = array();
70
-
71
- /**
72
- * Stores affiliate link default data.
73
- *
74
- * @since 3.0.0
75
- * @access protected
76
- * @var array
77
- */
78
- protected $default_data = array(
79
- 'name' => '',
80
- 'slug' => '',
81
- 'date_created' => '',
82
- 'date_modified' => '',
83
- 'status' => '',
84
- 'permalink' => '',
85
- 'destination_url' => '',
86
- 'rel_tags' => '',
87
- 'css_classes' => '',
88
- 'redirect_type' => 'global',
89
- 'no_follow' => 'global',
90
- 'new_window' => 'global',
91
- 'uncloak_link' => 'global',
92
- 'pass_query_str' => 'global',
93
- 'image_ids' => array(),
94
- 'categories' => array(),
95
- 'category_slug' => '',
96
- 'category_slug_id' => 0,
97
- 'inserted_to' => array(),
98
- 'scanned_inserted' => ''
99
- );
100
-
101
- /**
102
- * Stores affiliate link default data.
103
- *
104
- * @since 3.0.0
105
- * @access protected
106
- * @var array
107
- */
108
- protected $extend_data = array();
109
-
110
- /**
111
- * Stores affiliate link post data.
112
- *
113
- * @since 3.0.0
114
- * @access private
115
- * @var object
116
- */
117
- protected $post_data;
118
-
119
- /**
120
- * This is where changes to the $data will be saved.
121
- *
122
- * @since 3.0.0
123
- * @access protected
124
- * @var object
125
- */
126
- protected $changes = array();
127
-
128
- /**
129
- * Stores boolean if the data has been read from the database or not.
130
- *
131
- * @since 3.0.0
132
- * @access protected
133
- * @var object
134
- */
135
- protected $object_is_read = false;
136
-
137
- /**
138
- * List of deprecated properties.
139
- *
140
- * @since 3.3.1
141
- * @access protected
142
- * @var array
143
- */
144
- protected $deprecated_props = array(
145
- 'link_health_issue',
146
- 'link_health_issue_ignored'
147
- );
148
-
149
-
150
-
151
-
152
- /*
153
- |--------------------------------------------------------------------------
154
- | Class Methods
155
- |--------------------------------------------------------------------------
156
- */
157
-
158
- /**
159
- * Class constructor.
160
- *
161
- * @since 3.0.0
162
- * @access public
163
- *
164
- * @param Abstract_Main_Plugin_Class $main_plugin Main plugin object.
165
- * @param Plugin_Constants $constants Plugin constants object.
166
- * @param Helper_Functions $helper_functions Helper functions object.
167
- */
168
- public function __construct( $id = null ) {
169
-
170
- $this->_constants = ThirstyAffiliates()->helpers[ 'Plugin_Constants' ];
171
- $this->_helper_functions = ThirstyAffiliates()->helpers[ 'Helper_Functions' ];
172
- $this->deprecated_props = apply_filters( 'ta_affiliate_link_deprecated_props' , $this->deprecated_props );
173
-
174
- if ( filter_var( $id , FILTER_VALIDATE_INT ) && $id ) {
175
-
176
- $this->extend_data = apply_filters( 'ta_affiliate_link_extended_data' , $this->extend_data , $this->default_data );
177
- $this->data = $this->get_merged_default_extended_data();
178
- $this->id = absint( $id );
179
-
180
- $this->read();
181
-
182
- }
183
-
184
- }
185
-
186
- /**
187
- * Read data from DB and save on instance.
188
- *
189
- * @since 3.0.0
190
- * @since 3.2.2 Optimize method by lessening the metadata fetch into one single function call.
191
- * @access public
192
- */
193
- private function read() {
194
-
195
- $this->post_data = get_post( $this->id );
196
-
197
- if ( ! is_a( $this->post_data , 'WP_Post' ) || $this->object_is_read )
198
- return;
199
-
200
- $this->id = $this->post_data->ID; // set the affiliate link ID
201
- $meta_data = get_metadata( 'post' , $this->id );
202
- $default_data = $this->get_merged_default_extended_data();
203
-
204
- foreach ( $default_data as $prop => $value ) {
205
-
206
- // fetch raw meta data if present.
207
- $raw_data = isset( $meta_data[ Plugin_Constants::META_DATA_PREFIX . $prop ] ) ? maybe_unserialize( $meta_data[ Plugin_Constants::META_DATA_PREFIX . $prop ][0] ) : '';
208
-
209
- switch ( $prop ) {
210
-
211
- case 'name' :
212
- case 'slug' :
213
- case 'status' :
214
- case 'date_created' :
215
- case 'permalink' :
216
- $this->data[ $prop ] = $this->get_post_data_equivalent( $prop );
217
- break;
218
-
219
- case 'no_follow' :
220
- case 'new_window' :
221
- case 'pass_query_str' :
222
- case 'uncloak_link' :
223
- case 'redirect_type' :
224
- $this->data[ $prop ] = ! empty( $raw_data ) ? $raw_data : $default_data[ $prop ];
225
- break;
226
-
227
- case 'rel_tags' :
228
- case 'css_classes' :
229
- $this->data[ $prop ] = ! empty( $raw_data ) ? $raw_data : $this->get_prop_global_option_value( $prop );
230
- break;
231
-
232
- case 'image_ids' :
233
- case 'inserted_to' :
234
- $this->data[ $prop ] = ( is_array( $raw_data ) && ! empty( $raw_data ) ) ? $raw_data : $default_data[ $prop ];
235
- break;
236
-
237
- case 'categories' :
238
- $categories = wp_get_post_terms( $this->id , Plugin_Constants::AFFILIATE_LINKS_TAX );
239
- $this->data[ $prop ] = ! empty( $categories ) ? $categories : $default_data[ $prop ];
240
- break;
241
-
242
- default :
243
- $this->data[ $prop ] = apply_filters( 'ta_read_thirstylink_property' , $raw_data , $prop , $default_data , $this->id , $meta_data );
244
- break;
245
-
246
- }
247
-
248
- }
249
-
250
- $this->object_is_read = true;
251
-
252
- }
253
-
254
-
255
-
256
-
257
- /*
258
- |--------------------------------------------------------------------------
259
- | Data getters
260
- |--------------------------------------------------------------------------
261
- */
262
-
263
- /**
264
- * Get merged $default_data and $extended_data class properties.
265
- *
266
- * @since 3.0.0
267
- * @access public
268
- *
269
- * @return array Data properties.
270
- */
271
- private function get_merged_default_extended_data() {
272
-
273
- return array_merge( $this->default_data , $this->extend_data );
274
-
275
- }
276
-
277
- /**
278
- * Return's the post data equivalent of a certain affiliate link data property.
279
- *
280
- * @since 3.0.0
281
- * @access private
282
- *
283
- * @param string $prop Affiliate link property name.
284
- * @return string WP Post property equivalent.
285
- */
286
- private function get_post_data_equivalent( $prop ) {
287
-
288
- $equivalents = apply_filters( 'ta_affiliate_link_post_data_equivalent' , array(
289
- 'name' => $this->post_data->post_title,
290
- 'slug' => $this->post_data->post_name,
291
- 'permalink' => get_permalink( $this->post_data->ID ),
292
- 'status' => $this->post_data->post_status,
293
- 'date_created' => $this->post_data->post_date,
294
- 'date_modified' => $this->post_data->post_modified,
295
- ) , $this->post_data );
296
-
297
- if ( array_key_exists( $prop , $equivalents ) )
298
- return $equivalents[ $prop ];
299
- else
300
- return;
301
-
302
- }
303
-
304
- /**
305
- * Return data property.
306
- *
307
- * @since 3.0.0
308
- * @since 3.3.1 Make sure deprecated props are ignored.
309
- * @access public
310
- *
311
- * @param string $prop Data property slug.
312
- * @param mixed $default Set property default value (optional).
313
- * @return mixed Property data.
314
- */
315
- public function get_prop( $prop , $default = '' ) {
316
-
317
- $default_data = $this->get_merged_default_extended_data();
318
-
319
- if ( in_array( $prop , $this->deprecated_props ) )
320
- return $default;
321
-
322
- if ( array_key_exists( $prop , $this->data ) && $this->data[ $prop ] )
323
- $return_value = $this->data[ $prop ];
324
- else
325
- $return_value = ( $default ) ? $default : $default_data[ $prop ];
326
-
327
- return $return_value;
328
-
329
- }
330
-
331
- /**
332
- * Get redirect type
333
- *
334
- * @since 3.4.0
335
- * @access public
336
- *
337
- * @param string $default Default redirect type.
338
- * @return string Redirect type.
339
- */
340
- public function get_redirect_type( $default = '' ) {
341
-
342
- $redirect_type = $this->data[ 'redirect_type' ];
343
-
344
- if ( $redirect_type === 'global' )
345
- $redirect_type = get_option( 'ta_link_redirect_type' , $default );
346
- elseif ( ! $redirect_type )
347
- $redirect_type = $default ? $default : $default_data[ 'redirect_type' ];
348
-
349
- return $redirect_type;
350
- }
351
-
352
- /**
353
- * Return Affiliate_Link ID.
354
- *
355
- * @since 3.0.0
356
- * @access public
357
- *
358
- * @return int Affiliate_Link ID.
359
- */
360
- public function get_id() {
361
-
362
- return absint( $this->id );
363
-
364
- }
365
-
366
- /**
367
- * Return changed data property.
368
- *
369
- * @since 3.0.0
370
- * @access public
371
- *
372
- * @param string $prop Data property slug.
373
- * @param mixed $default Set property default value (optional).
374
- * @return mixed Property data.
375
- */
376
- public function get_changed_prop( $prop , $default = '' ) {
377
-
378
- return isset( $this->changes[ $prop ] ) ? $this->changes[ $prop ] : $this->get_prop( $prop , $default );
379
-
380
- }
381
-
382
- /**
383
- * Return affiliate link's WP_Post data.
384
- *
385
- * @since 3.0.0
386
- * @access public
387
- *
388
- * @return object Post data object.
389
- */
390
- public function get_post_data() {
391
-
392
- return $this->post_data;
393
-
394
- }
395
-
396
- /**
397
- * Get the properties global option value.
398
- *
399
- * @since 3.0.0
400
- * @since 3.2.2 Remove 'uncloak' option.
401
- * @since 3.4.0 Remove 'redirect_type' option. Add additional CSS classes option.
402
- * @access public
403
- *
404
- * @param string $prop Name of property.
405
- * @return string Global option value.
406
- */
407
- public function get_prop_global_option_value( $prop , $default = '' ) {
408
-
409
- switch( $prop ) {
410
-
411
- case 'rel_tags' :
412
- $option = 'ta_additional_rel_tags';
413
- break;
414
-
415
- case 'css_classes' :
416
- $option = 'ta_additional_css_classes';
417
- break;
418
- }
419
-
420
- return $option ? get_option( $option , $default ) : $default;
421
- }
422
-
423
- /**
424
- * Get the global value for the uncloak property.
425
- *
426
- * @deprecated 3.2.0
427
- *
428
- * @since 3.0.0
429
- * @access public
430
- *
431
- * @return string Global option value.
432
- */
433
- public function get_global_uncloak_value() {
434
-
435
- return $this->get_toggle_prop_global_value( 'uncloak_link' );
436
- }
437
-
438
- /**
439
- * Get toggle property global value. This function also checks for "category" valued options.
440
- *
441
- * @since 3.2.0
442
- * @access public
443
- *
444
- * @param string $toggle_prop Affiliate linkg toggle property name.
445
- * @return string 'yes' if true, otherwise 'no'.
446
- */
447
- public function get_toggle_prop_global_value( $toggle_prop ) {
448
-
449
- $global_props = apply_filters( 'ta_props_with_global_option' , array(
450
- 'no_follow' => 'ta_no_follow',
451
- 'new_window' => 'ta_new_window',
452
- 'uncloak_link' => '',
453
- 'pass_query_str' => 'ta_pass_query_str'
454
- ) );
455
- $global_props_cat = apply_filters( 'ta_prop_selected_categories_option' , array(
456
- 'no_follow' => 'ta_no_follow_category',
457
- 'new_window' => 'ta_new_window_category',
458
- 'uncloak_link' => 'ta_category_to_uncloak',
459
- 'pass_query_str' => 'ta_pass_query_str_category'
460
- ) );
461
-
462
- if ( ! array_key_exists( $toggle_prop , $global_props ) )
463
- return 'no';
464
-
465
- // get equivalent global value.
466
- $prop_value = $toggle_prop == 'uncloak_link' ? 'category' : get_option( $global_props[ $toggle_prop ] );
467
-
468
- if ( ! $prop_value )
469
- return 'no';
470
-
471
- // if prop value is not set to 'category', then return option value.
472
- if ( $prop_value != 'category' || ! array_key_exists( $toggle_prop , $global_props_cat ) )
473
- return $prop_value;
474
-
475
- $prop_cats = maybe_unserialize( get_option( $global_props_cat[ $toggle_prop ] , array() ) );
476
- $link_cats = $this->get_prop( 'categories' );
477
-
478
- // skip when there are no categories to check (false)
479
- if ( ! is_array( $prop_cats ) || empty( $prop_cats ) || empty( $link_cats ) )
480
- return 'no';
481
-
482
- foreach ( $link_cats as $category ) {
483
-
484
- if ( in_array( $category->term_id , $prop_cats ) )
485
- return 'yes';
486
- }
487
-
488
- return 'no';
489
- }
490
-
491
- /**
492
- * Gets the category slug used for affiliate link (if present).
493
- *
494
- * @since 3.2.2
495
- * @access public
496
- *
497
- * @return string Affiliate link ategory slug.
498
- */
499
- public function get_category_slug() {
500
-
501
- $cat_slug = $this->get_prop( 'category_slug' );
502
- return $cat_slug ? $cat_slug : $this->_helper_functions->get_default_category_slug( $this->get_id() , $this->get_prop( 'categories' ) );
503
- }
504
-
505
-
506
-
507
-
508
- /*
509
- |--------------------------------------------------------------------------
510
- | Data setters
511
- |--------------------------------------------------------------------------
512
- */
513
-
514
- /**
515
- * Set new value to properties and save it to $changes property.
516
- * This stores changes in a special array so we can track what needs to be saved on the DB later.
517
- *
518
- * @since 3.0.0
519
- * @access public
520
- *
521
- * @param string $prop Data property slug.
522
- * @param string $value New property value.
523
- */
524
- public function set_prop( $prop , $value ) {
525
-
526
- $default_data = $this->get_merged_default_extended_data();
527
-
528
- if ( array_key_exists( $prop , $this->data ) ) {
529
-
530
- // permalink property must not be changed
531
- if ( $prop == 'permalink' )
532
- return;
533
-
534
- if ( gettype( $value ) == gettype( $default_data[ $prop ] ) )
535
- $this->changes[ $prop ] = $value;
536
- else {
537
-
538
- // TODO: handle error here.
539
-
540
- }
541
-
542
- } else {
543
-
544
- $this->data[ $prop ] = $value;
545
- $this->changes[ $prop ] = $value;
546
-
547
- }
548
-
549
- }
550
-
551
-
552
-
553
-
554
- /*
555
- |--------------------------------------------------------------------------
556
- | Save (Create / Update) data to DB
557
- |--------------------------------------------------------------------------
558
- */
559
-
560
- /**
561
- * Save data in $changes to the database.
562
- *
563
- * @since 3.0.0
564
- * @access public
565
- *
566
- * @return WP_Error | int On success will return the post ID, otherwise it will return a WP_Error object.
567
- */
568
- public function save() {
569
-
570
- if ( ! empty( $this->changes ) ) {
571
-
572
- $post_metas = array();
573
- $post_data = array(
574
- 'post_title' => $this->get_changed_prop( 'name' ),
575
- 'post_name' => $this->get_changed_prop( 'slug' ),
576
- 'post_status' => $this->get_changed_prop( 'status' , 'publish' ),
577
- 'post_date' => $this->get_changed_prop( 'date_created' , current_time( 'mysql' ) ),
578
- 'post_modified' => $this->get_changed_prop( 'date_modified' , current_time( 'mysql' ) )
579
- );
580
-
581
- foreach ( $this->changes as $prop => $value ) {
582
-
583
- // make sure that property is registered in default data
584
- if ( ! array_key_exists( $prop , $this->get_merged_default_extended_data() ) )
585
- continue;
586
-
587
- if ( in_array( $prop , array( 'permalink' , 'name' , 'slug' , 'status' , 'date_created' , 'date_modified' ) ) )
588
- continue;
589
-
590
- $post_metas[ $prop ] = $value;
591
- }
592
-
593
- // create or update post
594
- if ( $this->id )
595
- $post_id = $this->update( $post_data );
596
- else
597
- $post_id = $this->create( $post_data );
598
-
599
- if ( ! is_wp_error( $post_id ) )
600
- $this->update_metas( $post_id , $post_metas );
601
- else
602
- return $post_id; // Return WP_Error object on error
603
-
604
- do_action( 'ta_save_affiliate_link' , $this->changes , $this );
605
-
606
- // update instance with new changes.
607
- $this->object_is_read = false;
608
- $this->read();
609
-
610
- } else
611
- return new \WP_Error( 'ta_affiliate_link_no_changes' , __( 'Unable to save affiliate link as there are no changes registered on the object yet.' , 'thirstyaffiliates' ) , array( 'changes' => $this->changes , 'affiliate_link' => $this ) );
612
-
613
- return $post_id;
614
- }
615
-
616
- /**
617
- * Create the affiliate link post.
618
- *
619
- * @since 3.0.0
620
- * @access private
621
- *
622
- * @param array $post_data Affiliate link post data.
623
- * @param WP_Error|int WP_Error on error, ID of newly created post otherwise.
624
- */
625
- private function create( $post_data ) {
626
-
627
- $post_data = array_merge( array( 'post_type' => Plugin_Constants::AFFILIATE_LINKS_CPT ) , $post_data );
628
- $this->id = wp_insert_post( $post_data );
629
-
630
- return $this->id;
631
-
632
- }
633
-
634
- /**
635
- * Update the affiliate link post.
636
- *
637
- * @since 3.0.0
638
- * @access private
639
- *
640
- * @param array $post_data Affiliate link post data.
641
- * @return int ID of the updated post upon success. 0 on failure.
642
- */
643
- private function update( $post_data ) {
644
-
645
- $post_data = array_merge( array( 'ID' => $this->id ) , $post_data );
646
- return wp_update_post( $post_data , true );
647
-
648
- }
649
-
650
- /**
651
- * Update/add the affiliate link meta data.
652
- *
653
- * @since 3.0.0
654
- * @access private
655
- *
656
- * @param int $post_id Affiliate link post ID.
657
- * @param array $post_metas Affiliate link meta data.
658
- */
659
- private function update_metas( $post_id , $post_metas ) {
660
-
661
- foreach ( $post_metas as $key => $value )
662
- update_post_meta( $post_id , Plugin_Constants::META_DATA_PREFIX . $key , $value );
663
-
664
- }
665
-
666
-
667
-
668
-
669
- /*
670
- |--------------------------------------------------------------------------
671
- | Utility Functions.
672
- |--------------------------------------------------------------------------
673
- */
674
-
675
- /**
676
- * Conditional function that checks if a property is true for the affiliate link. This function also checks global and category set values.
677
- *
678
- * @since 3.2.0
679
- * @access public
680
- *
681
- * @param string $toggle_prop Affiliate link toggle property to check.
682
- * @return boolean true | false.
683
- */
684
- public function is( $toggle_prop ) {
685
-
686
- // check if global setting for uncloak link is enabled.
687
- if ( $toggle_prop == 'uncloak_link' && get_option( 'ta_uncloak_link_per_link' ) !== 'yes' )
688
- return;
689
-
690
- $prop_value = $this->get_prop( $toggle_prop );
691
-
692
- // if prop value is not set to 'global', then return with the boolean equivalent of its value.
693
- if ( $prop_value != 'global' )
694
- return $prop_value == 'yes' ? true : false;
695
-
696
- // get property global value. Also checks for category selected options.
697
- $prop_value = $this->get_toggle_prop_global_value( $toggle_prop );
698
-
699
- return $prop_value == 'yes' ? true : false;
700
- }
701
-
702
- /**
703
- * Count affiliate link clicks.
704
- *
705
- * @since 3.0.0
706
- * @since 3.1.0 Add $date_offset parameter to count links only to a certain point.
707
- * @access public
708
- *
709
- * @param string $date_offset Date before limit to check
710
- * @return int Total number of clicks.
711
- */
712
- public function count_clicks( $date_offset = '' ) {
713
-
714
- global $wpdb;
715
-
716
- $table_name = $wpdb->prefix . Plugin_Constants::LINK_CLICK_DB;
717
- $link_id = $this->get_id();
718
- $query = "SELECT count(*) from $table_name WHERE link_id = $link_id";
719
- $query .= ( $date_offset && \DateTime::createFromFormat('Y-m-d H:i:s', $date_offset ) !== false ) ? " AND date_clicked > '$date_offset'" : '';
720
- $clicks = $wpdb->get_var( $query );
721
-
722
- return (int) $clicks;
723
- }
724
-
725
- /**
726
- * Scan where links are inserted.
727
- *
728
- * @since 3.2.0
729
- * @since 3.3.3 Improve the query to specify the results by searching using the permalink value, and alternating between the used link prefixes.
730
- * @access public
731
- *
732
- * @return array List of WP_Post IDs where affiliate link is inserted in content.
733
- */
734
- public function scan_where_links_inserted() {
735
-
736
- global $wpdb;
737
-
738
- // prepare the query.
739
- $post_ids = array();
740
- $link_id = $this->get_id();
741
- $cpt_slug = Plugin_Constants::AFFILIATE_LINKS_CPT;
742
- $types = get_post_types( array( 'public' => true ) , 'names' , 'and' );
743
- $types_str = implode( "','" , $types );
744
- $permalink = $this->get_prop( 'permalink' );
745
- $link_prefix = $this->_helper_functions->get_thirstylink_link_prefix();
746
- $link_prefixes = $this->_helper_functions->get_option( 'ta_used_link_prefixes' , array() );
747
- $like_query = array();
748
-
749
- foreach ( $link_prefixes as $prefix )
750
- $like_query[] = str_replace( $link_prefix , $prefix , "post_content LIKE '%$permalink\"%'" );
751
-
752
- $like_query_str = implode( ' OR ' , $like_query );
753
- $query = "SELECT ID FROM $wpdb->posts WHERE ( $like_query_str OR post_content LIKE '%[thirstylink%ids=\"$link_id%' ) AND post_type IN ( '$types_str' ) AND post_status = 'publish'";
754
-
755
- // fetch WP_Post IDs where link is inserted to.
756
- $raw_ids = $wpdb->get_col( $query );
757
-
758
- // save last scanned
759
- update_post_meta( $this->get_id() , Plugin_Constants::META_DATA_PREFIX . 'scanned_inserted' , current_time( 'mysql' , true ) );
760
-
761
- // save to custom meta.
762
- $post_ids = array_map( 'intval' , $raw_ids );
763
- update_post_meta( $this->get_id() , Plugin_Constants::META_DATA_PREFIX . 'inserted_to' , $post_ids );
764
-
765
- return $post_ids;
766
- }
767
-
768
- }
1
+ <?php
2
+
3
+ namespace ThirstyAffiliates\Models;
4
+
5
+ use ThirstyAffiliates\Abstracts\Abstract_Main_Plugin_Class;
6
+
7
+ use ThirstyAffiliates\Interfaces\Model_Interface;
8
+
9
+ use ThirstyAffiliates\Helpers\Plugin_Constants;
10
+ use ThirstyAffiliates\Helpers\Helper_Functions;
11
+
12
+ /**
13
+ * Model that houses the data model of an affiliate link.
14
+ *
15
+ * @since 3.0.0
16
+ */
17
+ class Affiliate_Link {
18
+
19
+
20
+ /*
21
+ |--------------------------------------------------------------------------
22
+ | Class Properties
23
+ |--------------------------------------------------------------------------
24
+ */
25
+
26
+ /**
27
+ * Model that houses the main plugin object.
28
+ *
29
+ * @since 3.0.0
30
+ * @access private
31
+ * @var Abstract_Main_Plugin_Class
32
+ */
33
+ private $_main_plugin;
34
+
35
+ /**
36
+ * Model that houses all the plugin constants.
37
+ *
38
+ * @since 3.0.0
39
+ * @access private
40
+ * @var Plugin_Constants
41
+ */
42
+ private $_constants;
43
+
44
+ /**
45
+ * Property that houses all the helper functions of the plugin.
46
+ *
47
+ * @since 3.0.0
48
+ * @access private
49
+ * @var Helper_Functions
50
+ */
51
+ private $_helper_functions;
52
+
53
+ /**
54
+ * Stores affiliate link ID.
55
+ *
56
+ * @since 3.0.0
57
+ * @access protected
58
+ * @var array
59
+ */
60
+ protected $id;
61
+
62
+ /**
63
+ * Stores affiliate link data.
64
+ *
65
+ * @since 3.0.0
66
+ * @access protected
67
+ * @var array
68
+ */
69
+ protected $data = array();
70
+
71
+ /**
72
+ * Stores affiliate link default data.
73
+ *
74
+ * @since 3.0.0
75
+ * @access protected
76
+ * @var array
77
+ */
78
+ protected $default_data = array(
79
+ 'name' => '',
80
+ 'slug' => '',
81
+ 'date_created' => '',
82
+ 'date_modified' => '',
83
+ 'status' => '',
84
+ 'permalink' => '',
85
+ 'destination_url' => '',
86
+ 'rel_tags' => '',
87
+ 'css_classes' => '',
88
+ 'redirect_type' => 'global',
89
+ 'no_follow' => 'global',
90
+ 'new_window' => 'global',
91
+ 'uncloak_link' => 'global',
92
+ 'pass_query_str' => 'global',
93
+ 'image_ids' => array(),
94
+ 'categories' => array(),
95
+ 'category_slug' => '',
96
+ 'category_slug_id' => 0,
97
+ 'inserted_to' => array(),
98
+ 'scanned_inserted' => ''
99
+ );
100
+
101
+ /**
102
+ * Stores affiliate link default data.
103
+ *
104
+ * @since 3.0.0
105
+ * @access protected
106
+ * @var array
107
+ */
108
+ protected $extend_data = array();
109
+
110
+ /**
111
+ * Stores affiliate link post data.
112
+ *
113
+ * @since 3.0.0
114
+ * @access private
115
+ * @var object
116
+ */
117
+ protected $post_data;
118
+
119
+ /**
120
+ * This is where changes to the $data will be saved.
121
+ *
122
+ * @since 3.0.0
123
+ * @access protected
124
+ * @var object
125
+ */
126
+ protected $changes = array();
127
+
128
+ /**
129
+ * Stores boolean if the data has been read from the database or not.
130
+ *
131
+ * @since 3.0.0
132
+ * @access protected
133
+ * @var object
134
+ */
135
+ protected $object_is_read = false;
136
+
137
+ /**
138
+ * List of deprecated properties.
139
+ *
140
+ * @since 3.3.1
141
+ * @access protected
142
+ * @var array
143
+ */
144
+ protected $deprecated_props = array(
145
+ 'link_health_issue',
146
+ 'link_health_issue_ignored'
147
+ );
148
+
149
+
150
+
151
+
152
+ /*
153
+ |--------------------------------------------------------------------------
154
+ | Class Methods
155
+ |--------------------------------------------------------------------------
156
+ */
157
+
158
+ /**
159
+ * Class constructor.
160
+ *
161
+ * @since 3.0.0
162
+ * @access public
163
+ *
164
+ * @param Abstract_Main_Plugin_Class $main_plugin Main plugin object.
165
+ * @param Plugin_Constants $constants Plugin constants object.
166
+ * @param Helper_Functions $helper_functions Helper functions object.
167
+ */
168
+ public function __construct( $id = null ) {
169
+
170
+ $this->_constants = ThirstyAffiliates()->helpers[ 'Plugin_Constants' ];
171
+ $this->_helper_functions = ThirstyAffiliates()->helpers[ 'Helper_Functions' ];
172
+ $this->deprecated_props = apply_filters( 'ta_affiliate_link_deprecated_props' , $this->deprecated_props );
173
+
174
+ if ( filter_var( $id , FILTER_VALIDATE_INT ) && $id ) {
175
+
176
+ $this->extend_data = apply_filters( 'ta_affiliate_link_extended_data' , $this->extend_data , $this->default_data );
177
+ $this->data = $this->get_merged_default_extended_data();
178
+ $this->id = absint( $id );
179
+
180
+ $this->read();
181
+
182
+ }
183
+
184
+ }
185
+
186
+ /**
187
+ * Read data from DB and save on instance.
188
+ *
189
+ * @since 3.0.0
190
+ * @since 3.2.2 Optimize method by lessening the metadata fetch into one single function call.
191
+ * @access public
192
+ */
193
+ private function read() {
194
+
195
+ $this->post_data = get_post( $this->id );
196
+
197
+ if ( ! is_a( $this->post_data , 'WP_Post' ) || $this->object_is_read )
198
+ return;
199
+
200
+ $this->id = $this->post_data->ID; // set the affiliate link ID
201
+ $meta_data = get_metadata( 'post' , $this->id );
202
+ $default_data = $this->get_merged_default_extended_data();
203
+
204
+ foreach ( $default_data as $prop => $value ) {
205
+
206
+ // fetch raw meta data if present.
207
+ $raw_data = isset( $meta_data[ Plugin_Constants::META_DATA_PREFIX . $prop ] ) ? maybe_unserialize( $meta_data[ Plugin_Constants::META_DATA_PREFIX . $prop ][0] ) : '';
208
+
209
+ switch ( $prop ) {
210
+
211
+ case 'name' :
212
+ case 'slug' :
213
+ case 'status' :
214
+ case 'date_created' :
215
+ case 'permalink' :
216
+ $this->data[ $prop ] = $this->get_post_data_equivalent( $prop );
217
+ break;
218
+
219
+ case 'no_follow' :
220
+ case 'new_window' :
221
+ case 'pass_query_str' :
222
+ case 'uncloak_link' :
223
+ case 'redirect_type' :
224
+ $this->data[ $prop ] = ! empty( $raw_data ) ? $raw_data : $default_data[ $prop ];
225
+ break;
226
+
227
+ case 'rel_tags' :
228
+ case 'css_classes' :
229
+ $this->data[ $prop ] = ! empty( $raw_data ) ? $raw_data : $this->get_prop_global_option_value( $prop );
230
+ break;
231
+
232
+ case 'image_ids' :
233
+ case 'inserted_to' :
234
+ $this->data[ $prop ] = ( is_array( $raw_data ) && ! empty( $raw_data ) ) ? $raw_data : $default_data[ $prop ];
235
+ break;
236
+
237
+ case 'categories' :
238
+ $categories = wp_get_post_terms( $this->id , Plugin_Constants::AFFILIATE_LINKS_TAX );
239
+ $this->data[ $prop ] = ! empty( $categories ) ? $categories : $default_data[ $prop ];
240
+ break;
241
+
242
+ default :
243
+ $this->data[ $prop ] = apply_filters( 'ta_read_thirstylink_property' , $raw_data , $prop , $default_data , $this->id , $meta_data );
244
+ break;
245
+
246
+ }
247
+
248
+ }
249
+
250
+ $this->object_is_read = true;
251
+
252
+ }
253
+
254
+
255
+
256
+
257
+ /*
258
+ |--------------------------------------------------------------------------
259
+ | Data getters
260
+ |--------------------------------------------------------------------------
261
+ */
262
+
263
+ /**
264
+ * Get merged $default_data and $extended_data class properties.
265
+ *
266
+ * @since 3.0.0
267
+ * @access public
268
+ *
269
+ * @return array Data properties.
270
+ */
271
+ private function get_merged_default_extended_data() {
272
+
273
+ return array_merge( $this->default_data , $this->extend_data );
274
+
275
+ }
276
+
277
+ /**
278
+ * Return's the post data equivalent of a certain affiliate link data property.
279
+ *
280
+ * @since 3.0.0
281
+ * @access private
282
+ *
283
+ * @param string $prop Affiliate link property name.
284
+ * @return string WP Post property equivalent.
285
+ */
286
+ private function get_post_data_equivalent( $prop ) {
287
+
288
+ $equivalents = apply_filters( 'ta_affiliate_link_post_data_equivalent' , array(
289
+ 'name' => $this->post_data->post_title,
290
+ 'slug' => $this->post_data->post_name,
291
+ 'permalink' => get_permalink( $this->post_data->ID ),
292
+ 'status' => $this->post_data->post_status,
293
+ 'date_created' => $this->post_data->post_date,
294
+ 'date_modified' => $this->post_data->post_modified,
295
+ ) , $this->post_data );
296
+
297
+ if ( array_key_exists( $prop , $equivalents ) )
298
+ return $equivalents[ $prop ];
299
+ else
300
+ return;
301
+
302
+ }
303
+
304
+ /**
305
+ * Return data property.
306
+ *
307
+ * @since 3.0.0
308
+ * @since 3.3.1 Make sure deprecated props are ignored.
309
+ * @access public
310
+ *
311
+ * @param string $prop Data property slug.
312
+ * @param mixed $default Set property default value (optional).
313
+ * @return mixed Property data.
314
+ */
315
+ public function get_prop( $prop , $default = '' ) {
316
+
317
+ $default_data = $this->get_merged_default_extended_data();
318
+
319
+ if ( in_array( $prop , $this->deprecated_props ) )
320
+ return $default;
321
+
322
+ if ( array_key_exists( $prop , $this->data ) && $this->data[ $prop ] )
323
+ $return_value = $this->data[ $prop ];
324
+ else
325
+ $return_value = ( $default ) ? $default : $default_data[ $prop ];
326
+
327
+ return $return_value;
328
+
329
+ }
330
+
331
+ /**
332
+ * Get redirect type
333
+ *
334
+ * @since 3.4.0
335
+