PluginProbe
Sign-up Sheets / trunk
Sign-up Sheets vtrunk
trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 2.2 2.2.0.1 2.2.1 2.2.10 2.2.11 2.2.11.1 2.2.12 2.2.13 2.2.14 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.0.1 All 32 releases
sign-up-sheets / id.php

id.php in Sign-up Sheets trunk, at id.php

202 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FDSUS;
4
5 if (!defined('ABSPATH')) exit; // Exit if accessed directly
6
7 use function is_plugin_active;
8
9 if (!function_exists('is_plugin_active')) {
10 require_once(ABSPATH . 'wp-admin/includes/plugin.php');
11 }
12
13 if (!class_exists('\\' . __NAMESPACE__ . '\Id')):
14
15 class Id
16 {
17 const PREFIX = 'dlssus';
18 const NAME = 'Sign-up Sheets - WordPress Plugin';
19 const AUTHOR = 'Fetch Designs';
20 const URL = 'https://www.fetchdesigns.com';
21 const DEBUG = false; // log detailed debug info to logs
22 const DEBUG_DISPLAY = false; // display detailed debug info on screen
23 const IS_LOCAL = false;
24 // TODO update basename constants to use private visibility to prevent direct usage, but requires php 7.1+ so only do in larger version update
25 /** @var string Fallback free basename. Don't use directly, instead use IdPro::getPluginBasename($type) */
26 const FREE_PLUGIN_BASENAME = 'sign-up-sheets/sign-up-sheets.php';
27 /** @var string Fallback pro basename. Don't use directly, instead use IdPro::getPluginBasename($type) */
28 const PRO_PLUGIN_BASENAME = 'sign-up-sheets-pro/sign-up-sheets.php';
29
30 /**
31 * @var int
32 */
33 public static $lastMemory = 0;
34
35 /**
36 * Get version from main PHP file `Version:` comment header
37 *
38 * @param 'pro'|'free'|'' $type 'pro', 'free', or '' for the current type
39 * @param bool $fallbackAllowed set to false if it forces uses the correct non-fallback version
40 *
41 * @return string version number
42 */
43 public static function version($type = '', $fallbackAllowed = true)
44 {
45 if (!function_exists('get_plugin_data')) {
46 require_once(ABSPATH . 'wp-admin' . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'plugin.php');
47 }
48
49 $pluginBasename = self::getPluginBasename($type, $fallbackAllowed);
50
51 if (!file_exists(WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $pluginBasename)) {
52 return '';
53 }
54
55 $pluginData = get_plugin_data(WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $pluginBasename, false, false);
56
57 return $pluginData && $pluginData['Version'] ? $pluginData['Version'] : '';
58 }
59
60 /**
61 * Log
62 *
63 * @param string $msg
64 * @param string|null $filename
65 * @param string $emailSubject Leave blank to prevent email from sending
66 */
67 public static function log($msg, $filename = null, $emailSubject = '')
68 {
69 if (!is_null($filename)) {
70 $filename = '-' . $filename;
71 }
72
73 if (self::DEBUG) {
74 $currentMemory = memory_get_usage();
75 $msg .= ' [current memory: ' . $currentMemory . ' - diff: ' . ($currentMemory - self::$lastMemory) . ']';
76 self::$lastMemory = $currentMemory;
77 }
78
79 if (!empty($emailSubject)) {
80 $msg .= PHP_EOL .
81 "Site URL: " . site_url() . PHP_EOL .
82 "IP: " . self::getClientIP() . PHP_EOL .
83 "Browser Data: " . $_SERVER['HTTP_USER_AGENT'];
84 }
85
86 error_log(
87 date("Y-m-d H:i:s") . " - $msg" . PHP_EOL,
88 3,
89 WP_CONTENT_DIR . DIRECTORY_SEPARATOR . self::PREFIX . $filename . '.log'
90 );
91
92 if (!empty($emailSubject)) {
93 wp_mail(
94 self::_getAlertRecipient(),
95 get_bloginfo('name') . ' - ' . $emailSubject,
96 $msg
97 );
98 }
99 }
100
101 /**
102 * Log while debug mode is enabled
103 *
104 * @param string $msg
105 * @param string|null $filename
106 * @param string $emailSubject Leave blank to prevent email from sending
107 */
108 public static function debug($msg, $filename = null, $emailSubject = '')
109 {
110 if (!self::DEBUG) {
111 return;
112 }
113
114 self::log($msg, $filename, $emailSubject);
115 }
116
117 /**
118 * Get email alert recipient or default to admin value
119 *
120 * @return string email address
121 */
122 private static function _getAlertRecipient()
123 {
124 $email = get_option(self::PREFIX . '_alert_recipient');
125 if (empty($email)) {
126 $email = get_option('admin_email');
127 }
128 return $email;
129 }
130
131 /**
132 * Get client IP
133 *
134 * @return string
135 */
136 public static function getClientIP()
137 {
138 $server_vars = array(
139 'HTTP_CLIENT_IP',
140 'HTTP_X_FORWARDED_FOR',
141 'HTTP_X_FORWARDED',
142 'HTTP_X_CLUSTER_CLIENT_IP',
143 'HTTP_FORWARDED_FOR',
144 'HTTP_FORWARDED',
145 'REMOTE_ADDR',
146 );
147 foreach ($server_vars as $key) {
148 if (array_key_exists($key, $_SERVER) === true) {
149 foreach (explode(',', $_SERVER[$key]) as $ip) {
150 if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
151 return $ip;
152 }
153 }
154 }
155 }
156 return null;
157 }
158
159 /**
160 * Get plugin path
161 *
162 * @return string
163 */
164 public static function getPluginPath()
165 {
166 return dirname(__FILE__) . DIRECTORY_SEPARATOR;
167 }
168
169 /**
170 * Get plugin basename
171 *
172 * @param 'pro'|'free'|'' $type
173 * @param bool $fallbackAllowed
174 *
175 * @return string
176 */
177 public static function getPluginBasename($type = '', $fallbackAllowed = true)
178 {
179 if ($type === '') {
180 $type = self::isPro() ? 'pro' : 'free';
181 }
182
183 if ($type === 'free' && !$fallbackAllowed) {
184 return self::FREE_PLUGIN_BASENAME;
185 }
186
187 return ($type === 'pro')
188 ? (defined('FDSUS_PRO_PLUGIN_BASENAME') ? FDSUS_PRO_PLUGIN_BASENAME : self::PRO_PLUGIN_BASENAME)
189 : (defined('FDSUS_FREE_PLUGIN_BASENAME') ? FDSUS_FREE_PLUGIN_BASENAME : self::FREE_PLUGIN_BASENAME);
190 }
191
192 /**
193 * Is the Pro version running?
194 */
195 public static function isPro()
196 {
197 return is_plugin_active(Id::getPluginBasename('pro'));
198 }
199 }
200
201 endif;
202