ThirstyAffiliates Affiliate Link Manager - Version 3.9.1

Version Description

  • Bug Fix: Incorrect client IP retrieved from HTTP_X_FORWARDED_FOR header
  • Bug Fix: External images not working in TA Image block
Download this release

Release Info

Developer caseproof
Plugin Icon 128x128 ThirstyAffiliates Affiliate Link Manager
Version 3.9.1
Comparing to
See all releases

Code changes from version 3.9 to 3.9.1

Files changed (150) hide show
  1. .htaccess +2 -2
  2. Abstracts/Abstract_Main_Plugin_Class.php +112 -112
  3. Abstracts/index.php +0 -0
  4. Helpers/Helper_Functions.php +670 -660
  5. Helpers/Plugin_Constants.php +164 -164
  6. Helpers/index.php +0 -0
  7. Interfaces/Activatable_Interface.php +0 -0
  8. Interfaces/Deactivatable_Interface.php +22 -22
  9. Interfaces/Initiable_Interface.php +0 -0
  10. Interfaces/Model_Interface.php +0 -0
  11. Interfaces/index.php +0 -0
  12. Models/Affiliate_Link.php +768 -768
  13. Models/Affiliate_Link_Attachment.php +14 -14
  14. Models/Affiliate_Links_CPT.php +1026 -1014
  15. Models/Bootstrap.php +520 -520
  16. Models/Guided_Tour.php +389 -389
  17. Models/Link_Fixer.php +289 -289
  18. Models/Link_Picker.php +710 -710
  19. Models/Marketing.php +0 -0
  20. Models/Migration.php +0 -0
  21. Models/REST_API.php +354 -354
  22. Models/Rewrites_Redirection.php +440 -440
  23. Models/Script_Loader.php +0 -0
  24. Models/Settings.php +2114 -2114
  25. Models/Shortcodes.php +303 -303
  26. Models/Stats_Reporting.php +1077 -1077
  27. Models/index.php +0 -0
  28. css/admin/index.php +0 -0
  29. css/admin/ta-affiliate-link-list.css +29 -29
  30. css/admin/ta-editor.css +46 -46
  31. css/admin/ta-guided-tour.css +11 -11
  32. css/admin/ta-reports.css +255 -255
  33. css/admin/ta-settings.css +113 -113
  34. css/admin/tinymce/editor.css +8 -8
  35. css/index.php +0 -0
  36. css/lib/jquery-tiptip/jquery-tiptip.css +109 -109
  37. css/lib/select2/select2.css +484 -484
  38. css/lib/select2/select2.min.css +1 -1
  39. images/admin-review-notice-logo.png +0 -0
  40. images/index.php +0 -0
  41. images/sidebar.jpg +0 -0
  42. images/spinner-2x.gif +0 -0
  43. images/spinner.gif +0 -0
  44. index.php +0 -0
  45. js/app/advance_link_picker/.eslintrc.json +29 -29
  46. js/app/advance_link_picker/dist/advance-link-picker.css +0 -0
  47. js/app/advance_link_picker/dist/advance-link-picker.js +0 -0
  48. js/app/advance_link_picker/package-lock.json +9586 -9586
  49. js/app/advance_link_picker/package.json +28 -28
  50. js/app/advance_link_picker/src/advance-link-picker-search.js +236 -236
  51. js/app/advance_link_picker/src/assets/styles/results-panel.scss +174 -174
  52. js/app/advance_link_picker/src/index.js +12 -12
  53. js/app/advance_link_picker/src/insert-affiliate-link-normal.js +285 -285
  54. js/app/affiliate_link_page/.eslintrc.json +29 -29
  55. js/app/affiliate_link_page/dist/affiliate-link-page.css +0 -0
  56. js/app/affiliate_link_page/dist/affiliate-link-page.js +23 -23
  57. js/app/affiliate_link_page/package-lock.json +6830 -6830
  58. js/app/affiliate_link_page/package.json +27 -27
  59. js/app/affiliate_link_page/src/affiliate-link-attachment.js +315 -315
  60. js/app/affiliate_link_page/src/affiliate-link-urls.js +43 -43
  61. js/app/affiliate_link_page/src/assets/styles/affiliate-link-urls.scss +20 -20
  62. js/app/affiliate_link_page/src/assets/styles/link-inserted-scanner.scss +82 -82
  63. js/app/affiliate_link_page/src/index.js +16 -16
  64. js/app/affiliate_link_page/src/link-category-show-in-slug.js +135 -135
  65. js/app/affiliate_link_page/src/link-insert-scanner.js +51 -51
  66. js/app/edit_shortcode/.eslintrc.json +28 -28
  67. js/app/edit_shortcode/dist/edit-shortcode.css +0 -0
  68. js/app/edit_shortcode/dist/edit-shortcode.js +0 -0
  69. js/app/edit_shortcode/package.json +27 -27
  70. js/app/edit_shortcode/src/assets/styles/index.scss +94 -94
  71. js/app/edit_shortcode/src/edit-shortcode.js +84 -84
  72. js/app/edit_shortcode/src/index.js +9 -9
  73. js/app/gutenberg_support/.babelrc +23 -23
  74. js/app/gutenberg_support/dist/gutenberg-support.css +1 -1
  75. js/app/gutenberg_support/dist/gutenberg-support.js +6 -6
  76. js/app/gutenberg_support/dist/gutenberg-support.js.map +1 -1
  77. js/app/gutenberg_support/package-lock.json +7417 -7417
  78. js/app/gutenberg_support/package.json +33 -33
  79. js/app/gutenberg_support/src/assets/styles/index.scss +200 -200
  80. js/app/gutenberg_support/src/blocks/index.js +19 -19
  81. js/app/gutenberg_support/src/blocks/ta-image/edit.js +630 -625
  82. js/app/gutenberg_support/src/blocks/ta-image/image-size.js +78 -78
  83. js/app/gutenberg_support/src/blocks/ta-image/index.js +173 -173
  84. js/app/gutenberg_support/src/blocks/ta-image/search-input.js +227 -227
  85. js/app/gutenberg_support/src/blocks/ta-image/util.js +67 -67
  86. js/app/gutenberg_support/src/formats/index.js +14 -14
  87. js/app/gutenberg_support/src/formats/ta-link/index.js +179 -179
  88. js/app/gutenberg_support/src/formats/ta-link/inline.js +316 -316
  89. js/app/gutenberg_support/src/formats/ta-link/url-input.js +358 -358
  90. js/app/gutenberg_support/src/formats/ta-link/url-popover.js +88 -88
  91. js/app/gutenberg_support/src/formats/ta-link/utils.js +74 -74
  92. js/app/gutenberg_support/src/index.js +7 -7
  93. js/app/gutenberg_support/webpack.config.js +31 -31
  94. js/app/import_export/.eslintrc.json +29 -29
  95. js/app/import_export/dist/import-export.css +0 -0
  96. js/app/import_export/dist/import-export.js +24 -24
  97. js/app/import_export/package.json +29 -29
  98. js/app/import_export/src/import.js +64 -64
  99. js/app/migration/.eslintrc.json +29 -29
  100. js/app/migration/dist/migration.css +0 -0
  101. js/app/migration/dist/migration.js +24 -24
  102. js/app/migration/package.json +28 -28
  103. js/app/migration/src/index.js +65 -65
  104. js/app/quick_add_affiliate_link/.eslintrc.json +29 -29
  105. js/app/quick_add_affiliate_link/dist/quick-add-affiliate-link.css +0 -0
  106. js/app/quick_add_affiliate_link/dist/quick-add-affiliate-link.js +23 -23
  107. js/app/quick_add_affiliate_link/package.json +27 -27
  108. js/app/quick_add_affiliate_link/src/assets/styles/form-style.scss +99 -99
  109. js/app/quick_add_affiliate_link/src/index.js +10 -10
  110. js/app/quick_add_affiliate_link/src/process-add-affiliate-link.js +204 -204
  111. js/app/ta-editor.js +198 -198
  112. js/app/ta-guided-tour.js +210 -210
  113. js/app/ta-reports.js +381 -381
  114. js/app/ta-review-request.js +38 -38
  115. js/app/ta-settings.js +137 -137
  116. js/app/ta.js +246 -246
  117. js/index.php +0 -0
  118. js/lib/chosen/chosen-sprite.png +0 -0
  119. js/lib/chosen/chosen-sprite@2x.png +0 -0
  120. js/lib/chosen/chosen.jquery.js +1321 -1321
  121. js/lib/chosen/chosen.jquery.min.js +1 -1
  122. js/lib/chosen/chosen.min.css +2 -2
  123. js/lib/flot/jquery.flot.min.js +7 -7
  124. js/lib/flot/jquery.flot.time.min.js +6 -6
  125. js/lib/jquery-tiptip/jquery.tipTip.js +190 -190
  126. js/lib/jquery-tiptip/jquery.tipTip.min.js +0 -0
  127. js/lib/selectize/selectize.default.css +394 -394
  128. js/lib/selectize/selectize.min.js +2 -2
  129. js/lib/thirstymce/editor-plugin.js +598 -598
  130. js/lib/thirstymce/img/aff-new.gif +0 -0
  131. js/lib/thirstymce/img/aff.gif +0 -0
  132. js/lib/thirstymce/widget-editor.js +134 -134
  133. languages/index.php +0 -0
  134. languages/thirstyaffiliates.pot +1321 -1321
  135. readme.txt +540 -536
  136. thirstyaffiliates.php +378 -378
  137. uninstall.php +0 -0
  138. views/cpt/view-attach-images-metabox-external-image.php +8 -8
  139. views/cpt/view-attach-images-metabox-single-image.php +8 -8
  140. views/cpt/view-attach-images-metabox.php +63 -63
  141. views/cpt/view-inserted-link-scanner-metabox.php +24 -24
  142. views/cpt/view-link-options-metabox.php +98 -98
  143. views/cpt/view-save-affiliate-link-metabox.php +15 -15
  144. views/cpt/view-urls-metabox.php +48 -48
  145. views/index.php +0 -0
  146. views/linkpicker/advance-link-picker.php +72 -72
  147. views/linkpicker/edit-shortcode.php +81 -81
  148. views/linkpicker/quick-add-affiliate-link.php +113 -113
  149. views/reports/link-performance-report.php +90 -90
  150. wpml-config.xml +8 -8
.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,660 +1,670 @@
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 && isset( $city[ 'timezone_id' ] ) )
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
- * @since 3.6 Add suport for Gutenberg.
305
- * @since 3.7 Add option to add list of affiliate link attached images for Gutenberg.
306
- * @access public
307
- *
308
- * @param string $keyword Search keyword.
309
- * @param int $paged WP_Query paged value.
310
- * @param string $category Affiliate link category to search.
311
- * @param array $exclude List of posts to be excluded.
312
- * @param array $is_gutenberg Toggle if searching for Gutenberg link picker.
313
- * @param array $with_images Toggle if search for affiliate links with images.
314
- * @return array List of affiliate link IDs.
315
- */
316
- public function search_affiliate_links_query( $keyword = '' , $paged = 1 , $category = '' , $exclude = array() , $is_gutenberg = false , $with_images = false ) {
317
-
318
- $args = array(
319
- 'post_type' => Plugin_Constants::AFFILIATE_LINKS_CPT,
320
- 'post_status' => 'publish',
321
- 's' => $keyword,
322
- 'fields' => 'ids',
323
- 'paged' => $paged,
324
- 'post__not_in' => $exclude
325
- );
326
-
327
- if ( $category ) {
328
-
329
- $args[ 'tax_query' ] = array(
330
- array(
331
- 'taxonomy' => Plugin_Constants::AFFILIATE_LINKS_TAX,
332
- 'field' => 'slug',
333
- 'terms' => $category
334
- )
335
- );
336
- }
337
-
338
- if ( $is_gutenberg ) {
339
- unset( $args[ 'fields' ] );
340
- unset( $args[ 'paged' ] );
341
-
342
- $args[ 'posts_per_page' ] = 20;
343
- }
344
-
345
- if ( $with_images ) {
346
-
347
- $args[ 'meta_query' ] = array(
348
- array(
349
- 'key' => Plugin_Constants::META_DATA_PREFIX . 'image_ids',
350
- 'compare' => 'EXISTS'
351
- )
352
- );
353
- }
354
-
355
- $query = new \WP_Query( $args );
356
-
357
- return $is_gutenberg ? array_map( function( $post ) use ( $with_images ) {
358
-
359
- $image_ids = get_post_meta( $post->ID , Plugin_Constants::META_DATA_PREFIX . 'image_ids' , true );
360
-
361
- return array(
362
- 'id' => $post->ID,
363
- 'link' => get_permalink( $post ),
364
- 'title' => $post->post_title,
365
- 'images' => $with_images ? $this->get_image_srcs( $image_ids ) : false
366
- );
367
- } , $query->posts ) : $query->posts;
368
- }
369
-
370
- /**
371
- * Get image and src attribute for list of image ids.
372
- *
373
- * @since 3.7
374
- * @access public
375
- *
376
- * @param array $image_ids List of image ids.
377
- * @param string $size Size of image to output.
378
- * @return array List of image ids with src attribute.
379
- */
380
- public function get_image_srcs( $image_ids , $size = 'thumbnail' ) {
381
-
382
- if ( ! is_array( $image_ids ) || empty( $image_ids ) ) return array();
383
-
384
- return array_map( function( $id ) use ( $size ) {
385
- return array(
386
- 'id' => $id,
387
- 'src' => wp_get_attachment_image_src( $id , $size )[0],
388
- );
389
- } , $image_ids );
390
- }
391
-
392
- /**
393
- * Check if affiliate link needs to be uncloaked.
394
- *
395
- * @deprecated 3.2.0
396
- *
397
- * @since 3.0.0
398
- * @access public
399
- *
400
- * @param Affiliate_Link $thirstylink Thirsty affiliate link object.
401
- * @return boolean Sets to true when affiliate link needs to be uncloaked.
402
- */
403
- public function is_uncloak_link( $thirstylink ) {
404
-
405
- return $thirstylink->is( 'uncloak_link' );
406
- }
407
-
408
- /**
409
- * Error log with a trace.
410
- *
411
- * @since 3.0.0
412
- * @access public
413
- */
414
- public function ta_error_log( $msg ) {
415
-
416
- $trace = debug_backtrace();
417
- $caller = array_shift( $trace );
418
-
419
- error_log( $msg . ' | Trace: ' . $caller[ 'file' ] . ' on line ' . $caller[ 'line' ] );
420
-
421
- }
422
-
423
- /**
424
- * Utility function that determines if a plugin is active or not.
425
- *
426
- * @since 3.0.0
427
- * @access public
428
- *
429
- * @param string $plugin_basename Plugin base name. Ex. woocommerce/woocommerce.php
430
- * @return boolean True if active, false otherwise.
431
- */
432
- public function is_plugin_active( $plugin_basename ) {
433
-
434
- // Makes sure the plugin is defined before trying to use it
435
- if ( !function_exists( 'is_plugin_active' ) )
436
- include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
437
-
438
- return is_plugin_active( $plugin_basename );
439
-
440
- }
441
-
442
- /**
443
- * Send email.
444
- *
445
- * @since 3.0.0
446
- * @access public
447
- *
448
- * @param array $recipients Array of recipients emails.
449
- * @param string $subject Email subject.
450
- * @param string $message Email message.
451
- * @param array $headers Array of email headers.
452
- * @param array $attachments Array of email attachments.
453
- * @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.
454
- */
455
- public function send_email( $recipients , $subject , $message , $headers = array() , $attachments = array() ) {
456
-
457
- $from_name = apply_filters( 'ta_email_from_name' , get_bloginfo( 'name' ) );
458
- $from_email = apply_filters( 'ta_email_from_email' , get_option( 'admin_email' ) );
459
-
460
- $headers[] = 'From: ' . $from_name . ' <' . $from_email . '>';
461
- $headers[] = 'Content-Type: text/html; charset=' . get_option( 'blog_charset' );
462
-
463
- return wp_mail( $recipients , $subject , $message , $headers , $attachments );
464
-
465
- }
466
-
467
- /**
468
- * Get Affiliate_Link data object.
469
- *
470
- * @since 3.0.0
471
- * @access public
472
- *
473
- * @param int $id Affiliate_Link post ID.
474
- * @return Affiliate_Link Affiliate Link object.
475
- */
476
- public function get_affiliate_link( $id = 0 ) {
477
-
478
- return new Affiliate_Link( $id );
479
- }
480
-
481
- /**
482
- * Retrieve all categories as an option array list.
483
- *
484
- * @since 3.0.0
485
- * @since 3.4.0 Add support for slug options.
486
- * @access public
487
- *
488
- * @return array List of category options.
489
- */
490
- public function get_all_category_as_options( $use_slug = false ) {
491
-
492
- $options = array();
493
-
494
- $categories = get_terms( array(
495
- 'taxonomy' => Plugin_Constants::AFFILIATE_LINKS_TAX,
496
- 'hide_empty' => false,
497
- ) );
498
-
499
- if ( ! is_wp_error( $categories ) ) {
500
-
501
- foreach( $categories as $category ) {
502
- $key = $use_slug ? $category->slug : $category->term_id;
503
- $options[ $key ] = $category->name;
504
- }
505
-
506
- } else {
507
-
508
- // TODO: Handle error
509
-
510
- }
511
-
512
- return $options;
513
- }
514
-
515
- /**
516
- * Set default term when affiliate link is saved.
517
- *
518
- * @since 3.2.0
519
- * @access public
520
- *
521
- * @param int $post_id Affiliate link post ID.
522
- */
523
- public function save_default_affiliate_link_category( $post_id ) {
524
-
525
- $default_category = Plugin_Constants::DEFAULT_LINK_CATEGORY;
526
- $taxonomy_slug = Plugin_Constants::AFFILIATE_LINKS_TAX;
527
-
528
- if ( get_option( 'ta_disable_cat_auto_select' ) == 'yes' || get_the_terms( $post_id , $taxonomy_slug ) )
529
- return;
530
-
531
- // create the default term if it doesn't exist
532
- if ( ! term_exists( $default_category , $taxonomy_slug ) )
533
- wp_insert_term( $default_category , $taxonomy_slug );
534
-
535
- $default_term = get_term_by( 'name' , $default_category , $taxonomy_slug );
536
-
537
- wp_set_post_terms( $post_id , $default_term->term_id , $taxonomy_slug );
538
- }
539
-
540
- /**
541
- * This function is an alias for WP get_option(), but will return the default value if option value is empty or invalid.
542
- *
543
- * @since 3.2.0
544
- * @access public
545
- *
546
- * @param string $option_name Name of the option of value to fetch.
547
- * @param mixed $default_value Defaut option value.
548
- * @return mixed Option value.
549
- */
550
- public function get_option( $option_name , $default_value = '' ) {
551
-
552
- $option_value = get_option( $option_name , $default_value );
553
-
554
- return ( gettype( $option_value ) === gettype( $default_value ) && $option_value ) ? $option_value : $default_value;
555
- }
556
-
557
- /**
558
- * Get blocked bots from settings or default value.
559
- *
560
- * @since 3.3.2
561
- * @access public
562
- *
563
- * @return array List of blocked bots.
564
- */
565
- public function get_blocked_bots() {
566
-
567
- $bots_string = $this->get_option( 'ta_blocked_bots' , Plugin_Constants::DEFAULT_BLOCKED_BOTS );
568
- return str_replace( ',' , '|' , $bots_string );
569
- }
570
-
571
- /**
572
- * Check if useragent is bot.
573
- *
574
- * @since 3.3.3
575
- * @access public
576
- *
577
- * @return bool True if detected as bot, otherwise false.
578
- */
579
- public function is_user_agent_bot() {
580
-
581
- $user_agent = isset( $_SERVER[ 'HTTP_USER_AGENT' ] ) ? strtolower( $_SERVER[ 'HTTP_USER_AGENT' ] ) : '';
582
- $bots = apply_filters( 'ta_useragent_bots_phrase_list' , $this->get_blocked_bots() );
583
- $pattern = '/' . $bots . '/i';
584
-
585
- return preg_match( $pattern , $user_agent );
586
- }
587
-
588
- /**
589
- * Get screen ID.
590
- *
591
- * @since 3.3.3
592
- * @access public
593
- */
594
- public function get_screen_id( $object_id ) {
595
-
596
- $screen_id = null;
597
-
598
- if ( isset( $_GET[ 'post_type' ] ) && $_GET[ 'post_type' ] == Plugin_Constants::AFFILIATE_LINKS_CPT ) {
599
-
600
- if ( isset( $_GET[ 'taxonomy' ] ) )
601
- $screen_id = 'edit-' . $_GET[ 'taxonomy' ];
602
- elseif ( isset( $_GET[ 'page' ] ) )
603
- $screen_id = 'thirstylink_page_' . $_GET[ 'page' ];
604
- else
605
- $screen_id = 'edit-thirstylink';
606
-
607
- } elseif ( $object_id && get_post_type( $object_id ) === Plugin_Constants::AFFILIATE_LINKS_CPT )
608
- $screen_id = 'thirstylink';
609
-
610
- return apply_filters( 'ta_get_screen_id' , $screen_id );
611
- }
612
-
613
- /**
614
- * Get visistor's browser and device.
615
- *
616
- * @since 3.4.0
617
- * @access public
618
- *
619
- * @return string Browser and device.
620
- */
621
- public function get_visitor_browser_device() {
622
-
623
- if ( get_option( 'ta_disable_browser_device_collection' ) === 'yes' || ! ini_get( "browscap" ) )
624
- return;
625
-
626
- $browser = get_browser( null );
627
- return is_object( $browser ) ? $browser->browser . '|' . $browser->platform . '|' . $browser->device_type : '';
628
- }
629
-
630
- /**
631
- * Check if a page builder is active or not. Currently supports: Beaver Builder and Elementor.
632
- *
633
- * @since 3.4.0
634
- * @access public
635
- */
636
- public function is_page_builder_active() {
637
-
638
- $is_bb_active = class_exists( 'FLBuilderModel' ) && \FLBuilderModel::is_builder_active();
639
- $elementor = class_exists( 'Elementor\Editor' ) && isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'elementor';
640
-
641
- return $is_bb_active || $elementor;
642
- }
643
-
644
- /**
645
- * Get string in between two strins.
646
- * Source: http://www.justin-cook.com/2006/03/31/php-parse-a-string-between-two-strings/
647
- *
648
- * @since 3.6
649
- */
650
- public function get_string_between( $string , $start , $end ) {
651
-
652
- $string = ' ' . $string;
653
- $ini = strpos($string, $start);
654
- if ($ini == 0) return '';
655
- $ini += strlen($start);
656
- $len = strpos($string, $end, $ini) - $ini;
657
- return substr($string, $ini, $len);
658
- }
659
-
660
- }
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 && isset( $city[ 'timezone_id' ] ) )
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
+
250
+ if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
251
+ $ip = $_SERVER['HTTP_CLIENT_IP'];
252
+ } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
253
+ $ip = explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] );
254
+ $ip = trim( $ip[0] );
255
+ } else {
256
+ $ip = $_SERVER['REMOTE_ADDR'];
257
+ }
258
+
259
+ return apply_filters( 'ta_get_user_ip_address', $ip );
260
+ }
261
+
262
+ /**
263
+ * Get the thirstylink slug set on the settings.
264
+ *
265
+ * @since 3.0.0
266
+ * @access public
267
+ *
268
+ * @return string $link_prefix Thirstyling link prefix.
269
+ */
270
+ public function get_thirstylink_link_prefix() {
271
+
272
+ $link_prefix = get_option( 'ta_link_prefix' , 'recommends' );
273
+
274
+ if ( $link_prefix === 'custom' )
275
+ $link_prefix = get_option( 'ta_link_prefix_custom' , 'recommends' );
276
+
277
+ return $link_prefix ? $link_prefix : 'recommends';
278
+ }
279
+
280
+ /**
281
+ * Get the affiliate link post default category slug.
282
+ *
283
+ * @since 3.0.0
284
+ * @access public
285
+ *
286
+ * @param int $link_id Affiliate Link ID.
287
+ * @param array $terms Affiliate link categories.
288
+ * @return string Affiliate link default category slug.
289
+ */
290
+ public function get_default_category_slug( $link_id , $terms = array() ) {
291
+
292
+ if ( ! is_array( $terms ) || empty( $terms ) )
293
+ $terms = get_the_terms( $link_id , Plugin_Constants::AFFILIATE_LINKS_TAX );
294
+
295
+ if ( is_wp_error( $terms ) || empty( $terms ) )
296
+ return;
297
+
298
+ $link_cat_obj = array_shift( $terms );
299
+
300
+ return $link_cat_obj->slug;
301
+ }
302
+
303
+ /**
304
+ * Search affiliate links query
305
+ *
306
+ * @since 3.0.0
307
+ * @since 3.6 Add suport for Gutenberg.
308
+ * @since 3.7 Add option to add list of affiliate link attached images for Gutenberg.
309
+ * @access public
310
+ *
311
+ * @param string $keyword Search keyword.
312
+ * @param int $paged WP_Query paged value.
313
+ * @param string $category Affiliate link category to search.
314
+ * @param array $exclude List of posts to be excluded.
315
+ * @param array $is_gutenberg Toggle if searching for Gutenberg link picker.
316
+ * @param array $with_images Toggle if search for affiliate links with images.
317
+ * @return array List of affiliate link IDs.
318
+ */
319
+ public function search_affiliate_links_query( $keyword = '' , $paged = 1 , $category = '' , $exclude = array() , $is_gutenberg = false , $with_images = false ) {
320
+
321
+ $args = array(
322
+ 'post_type' => Plugin_Constants::AFFILIATE_LINKS_CPT,
323
+ 'post_status' => 'publish',
324
+ 's' => $keyword,
325
+ 'fields' => 'ids',
326
+ 'paged' => $paged,
327
+ 'post__not_in' => $exclude
328
+ );
329
+
330
+ if ( $category ) {
331
+
332
+ $args[ 'tax_query' ] = array(
333
+ array(
334
+ 'taxonomy' => Plugin_Constants::AFFILIATE_LINKS_TAX,
335
+ 'field' => 'slug',
336
+ 'terms' => $category
337
+ )
338
+ );
339
+ }
340
+
341
+ if ( $is_gutenberg ) {
342
+ unset( $args[ 'fields' ] );
343
+ unset( $args[ 'paged' ] );
344
+
345
+ $args[ 'posts_per_page' ] = 20;
346
+ }
347
+
348
+ if ( $with_images ) {
349
+
350
+ $args[ 'meta_query' ] = array(
351
+ array(
352
+ 'key' => Plugin_Constants::META_DATA_PREFIX . 'image_ids',
353
+ 'compare' => 'EXISTS'
354
+ )
355
+ );
356
+ }
357
+
358
+ $query = new \WP_Query( $args );
359
+
360
+ return $is_gutenberg ? array_map( function( $post ) use ( $with_images ) {
361
+
362
+ $image_ids = get_post_meta( $post->ID , Plugin_Constants::META_DATA_PREFIX . 'image_ids' , true );
363
+
364
+ return array(
365
+ 'id' => $post->ID,
366
+ 'link' => get_permalink( $post ),
367
+ 'title' => $post->post_title,
368
+ 'images' => $with_images ? $this->get_image_srcs( $image_ids ) : false
369
+ );
370
+ } , $query->posts ) : $query->posts;
371
+ }
372
+
373
+ /**
374
+ * Get image and src attribute for list of image ids.
375
+ *
376
+ * @since 3.7
377
+ * @access public
378
+ *
379
+ * @param array $image_ids List of image ids.
380
+ * @param string $size Size of image to output.
381
+ * @return array List of image ids with src attribute.
382
+ */
383
+ public function get_image_srcs( $image_ids , $size = 'thumbnail' ) {
384
+
385
+ if ( ! is_array( $image_ids ) || empty( $image_ids ) ) return array();
386
+
387
+ return array_map( function( $id ) use ( $size ) {
388
+ if ( ! is_numeric( $id ) ) {
389
+ return array(
390
+ 'id' => null,
391
+ 'src' => $id
392
+ );
393
+ }
394
+
395
+ return array(
396
+ 'id' => $id,
397
+ 'src' => wp_get_attachment_image_src( $id , $size )[0]
398
+ );
399
+ } , $image_ids );
400
+ }
401
+
402
+ /**
403
+ * Check if affiliate link needs to be uncloaked.
404
+ *
405
+ * @deprecated 3.2.0
406
+ *
407
+ * @since 3.0.0
408
+ * @access public
409
+ *
410
+ * @param Affiliate_Link $thirstylink Thirsty affiliate link object.
411
+ * @return boolean Sets to true when affiliate link needs to be uncloaked.
412
+ */
413
+ public function is_uncloak_link( $thirstylink ) {
414
+
415
+ return $thirstylink->is( 'uncloak_link' );
416
+ }
417
+
418
+ /**
419
+ * Error log with a trace.
420
+ *
421
+ * @since 3.0.0
422
+ * @access public
423
+ */
424
+ public function ta_error_log( $msg ) {
425
+
426
+ $trace = debug_backtrace();
427
+ $caller = array_shift( $trace );
428
+
429
+ error_log( $msg . ' | Trace: ' . $caller[ 'file' ] . ' on line ' . $caller[ 'line' ] );
430
+
431
+ }
432
+
433
+ /**
434
+ * Utility function that determines if a plugin is active or not.
435
+ *
436
+ * @since 3.0.0
437
+ * @access public
438
+ *
439
+ * @param string $plugin_basename Plugin base name. Ex. woocommerce/woocommerce.php
440
+ * @return boolean True if active, false otherwise.
441
+ */
442
+ public function is_plugin_active( $plugin_basename ) {
443
+
444
+ // Makes sure the plugin is defined before trying to use it
445
+ if ( !function_exists( 'is_plugin_active' ) )
446
+ include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
447
+
448
+ return is_plugin_active( $plugin_basename );
449
+
450
+ }
451
+
452
+ /**
453
+ * Send email.
454
+ *
455
+ * @since 3.0.0
456
+ * @access public
457
+ *
458
+ * @param array $recipients Array of recipients emails.
459
+ * @param string $subject Email subject.
460
+ * @param string $message Email message.
461
+ * @param array $headers Array of email headers.
462
+ * @param array $attachments Array of email attachments.
463
+ * @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.
464
+ */
465
+ public function send_email( $recipients , $subject , $message , $headers = array() , $attachments = array() ) {
466
+
467
+ $from_name = apply_filters( 'ta_email_from_name' , get_bloginfo( 'name' ) );
468
+ $from_email = apply_filters( 'ta_email_from_email' , get_option( 'admin_email' ) );
469
+
470
+ $headers[] = 'From: ' . $from_name . ' <' . $from_email . '>';
471
+ $headers[] = 'Content-Type: text/html; charset=' . get_option( 'blog_charset' );
472
+
473
+ return wp_mail( $recipients , $subject , $message , $headers , $attachments );
474
+
475
+ }
476
+
477
+ /**
478
+ * Get Affiliate_Link data object.
479
+ *
480
+ * @since 3.0.0
481
+ * @access public
482
+ *
483
+ * @param int $id Affiliate_Link post ID.
484
+ * @return Affiliate_Link Affiliate Link object.
485
+ */
486
+ public function get_affiliate_link( $id = 0 ) {
487
+
488
+ return new Affiliate_Link( $id );
489
+ }
490
+
491
+ /**
492
+ * Retrieve all categories as an option array list.
493
+ *
494
+ * @since 3.0.0
495
+ * @since 3.4.0 Add support for slug options.
496
+ * @access public
497
+ *
498
+ * @return array List of category options.
499
+ */
500
+ public function get_all_category_as_options( $use_slug = false ) {
501
+
502
+ $options = array();
503
+
504
+ $categories = get_terms( array(
505
+ 'taxonomy' => Plugin_Constants::AFFILIATE_LINKS_TAX,
506
+ 'hide_empty' => false,
507
+ ) );
508
+
509
+ if ( ! is_wp_error( $categories ) ) {
510
+
511
+ foreach( $categories as $category ) {
512
+ $key = $use_slug ? $category->slug : $category->term_id;
513
+ $options[ $key ] = $category->name;
514
+ }
515
+
516
+ } else {
517
+
518
+ // TODO: Handle error
519
+
520
+ }
521
+
522
+ return $options;
523
+ }
524
+
525
+ /**
526
+ * Set default term when affiliate link is saved.
527
+ *
528
+ * @since 3.2.0
529
+ * @access public
530
+ *
531
+ * @param int $post_id Affiliate link post ID.
532
+ */
533
+ public function save_default_affiliate_link_category( $post_id ) {
534
+
535
+ $default_category = Plugin_Constants::DEFAULT_LINK_CATEGORY;
536
+ $taxonomy_slug = Plugin_Constants::AFFILIATE_LINKS_TAX;
537
+
538
+ if ( get_option( 'ta_disable_cat_auto_select' ) == 'yes' || get_the_terms( $post_id , $taxonomy_slug ) )
539
+ return;
540
+
541
+ // create the default term if it doesn't exist
542
+ if ( ! term_exists( $default_category , $taxonomy_slug ) )
543
+ wp_insert_term( $default_category , $taxonomy_slug );
544
+
545
+ $default_term = get_term_by( 'name' , $default_category , $taxonomy_slug );
546
+
547
+ wp_set_post_terms( $post_id , $default_term->term_id , $taxonomy_slug );
548
+ }
549
+
550
+ /**
551
+ * This function is an alias for WP get_option(), but will return the default value if option value is empty or invalid.
552
+ *
553
+ * @since 3.2.0
554
+ * @access public
555
+ *
556
+ * @param string $option_name Name of the option of value to fetch.
557
+ * @param mixed $default_value Defaut option value.
558
+ * @return mixed Option value.
559
+ */
560
+ public function get_option( $option_name , $default_value = '' ) {
561
+
562
+ $option_value = get_option( $option_name , $default_value );
563
+
564
+ return ( gettype( $option_value ) === gettype( $default_value ) && $option_value ) ? $option_value : $default_value;
565
+ }
566
+
567
+ /**
568
+ * Get blocked bots from settings or default value.
569
+ *
570
+ * @since 3.3.2
571
+ * @access public
572
+ *
573
+ * @return array List of blocked bots.
574
+ */
575
+ public function get_blocked_bots() {
576
+
577
+ $bots_string = $this->get_option( 'ta_blocked_bots' , Plugin_Constants::DEFAULT_BLOCKED_BOTS );
578
+ return str_replace( ',' , '|' , $bots_string );
579
+ }
580
+
581
+ /**
582
+ * Check if useragent is bot.
583
+ *
584
+ * @since 3.3.3
585
+ * @access public
586
+ *
587
+ * @return bool True if detected as bot, otherwise false.
588
+ */
589
+ public function is_user_agent_bot() {
590
+
591
+ $user_agent = isset( $_SERVER[ 'HTTP_USER_AGENT' ] ) ? strtolower( $_SERVER[ 'HTTP_USER_AGENT' ] ) : '';
592
+ $bots = apply_filters( 'ta_useragent_bots_phrase_list' , $this->get_blocked_bots() );
593
+ $pattern = '/' . $bots . '/i';
594
+
595
+ return preg_match( $pattern , $user_agent );
596
+ }
597
+
598
+ /**
599
+ * Get screen ID.
600
+ *
601
+ * @since 3.3.3
602
+ * @access public
603
+ */
604
+ public function get_screen_id( $object_id ) {
605
+
606
+ $screen_id = null;
607
+
608
+ if ( isset( $_GET[ 'post_type' ] ) && $_GET[ 'post_type' ] == Plugin_Constants::AFFILIATE_LINKS_CPT ) {
609
+
610
+ if ( isset( $_GET[ 'taxonomy' ] ) )
611
+ $screen_id = 'edit-' . $_GET[ 'taxonomy' ];
612
+ elseif ( isset( $_GET[ 'page' ] ) )
613
+ $screen_id = 'thirstylink_page_' . $_GET[ 'page' ];
614
+ else
615
+ $screen_id = 'edit-thirstylink';
616
+
617
+ } elseif ( $object_id && get_post_type( $object_id ) === Plugin_Constants::AFFILIATE_LINKS_CPT )
618
+ $screen_id = 'thirstylink';
619
+
620
+ return apply_filters( 'ta_get_screen_id' , $screen_id );
621
+ }
622
+
623
+ /**
624
+ * Get visistor's browser and device.
625
+ *
626
+ * @since 3.4.0
627
+ * @access public
628
+ *
629
+ * @return string Browser and device.
630
+ */
631
+ public function get_visitor_browser_device() {
632
+
633
+ if ( get_option( 'ta_disable_browser_device_collection' ) === 'yes' || ! ini_get( "browscap" ) )
634
+ return;
635
+
636
+ $browser = get_browser( null );
637
+ return is_object( $browser ) ? $browser->browser . '|' . $browser->platform . '|' . $browser->device_type : '';
638
+ }
639
+
640
+ /**
641
+ * Check if a page builder is active or not. Currently supports: Beaver Builder and Elementor.
642
+ *
643
+ * @since 3.4.0
644
+ * @access public
645
+ */
646
+ public function is_page_builder_active() {
647
+
648
+ $is_bb_active = class_exists( 'FLBuilderModel' ) && \FLBuilderModel::is_builder_active();
649
+ $elementor = class_exists( 'Elementor\Editor' ) && isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'elementor';
650
+
651
+ return $is_bb_active || $elementor;
652
+ }
653
+
654
+ /**
655
+ * Get string in between two strins.
656
+ * Source: http://www.justin-cook.com/2006/03/31/php-parse-a-string-between-two-strings/
657
+ *
658
+ * @since 3.6
659
+ */
660
+ public function get_string_between( $string , $start , $end ) {
661
+
662
+ $string = ' ' . $string;
663
+ $ini = strpos($string, $start);
664
+ if ($ini == 0) return '';
665
+ $ini += strlen($start);
666
+ $len = strpos($string, $end, $ini) - $ini;
667
+ return substr($string, $ini, $len);
668
+ }
669
+
670
+ }
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.9';
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.9.1';
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
+ );