/df_home/static/test/portalbkd/wp-content/plugins/wordpress-popup/inc/rules/class-popup-rule-referrer.php

https://gitlab.com/darmawan.fatria/df-skp-2014 · PHP · 354 lines · 162 code · 48 blank · 144 comment · 35 complexity · f363a089bcf4f75ba06bc78e39b9a217 MD5 · raw file

  1. <?php
  2. /*
  3. Name: Referrer
  4. Plugin URI: http://premium.wpmudev.org/project/the-pop-over-plugin/
  5. Description: Examine how the visitor arrived on the current page.
  6. Author: Philipp (Incsub)
  7. Author URI: http://premium.wpmudev.org
  8. Type: Rule
  9. Rules: From a specific referrer, Not from a specific referrer, Not from an internal link, From a search engine
  10. Version: 1.0
  11. NOTE: DON'T RENAME THIS FILE!!
  12. This filename is saved as metadata with each popup that uses these rules.
  13. Renaming the file will DISABLE the rules, which is very bad!
  14. */
  15. class IncPopupRule_Referrer extends IncPopupRule {
  16. /**
  17. * Initialize the rule object.
  18. *
  19. * @since 4.6
  20. */
  21. protected function init() {
  22. $this->filename = basename( __FILE__ );
  23. // 'referrer' rule.
  24. $this->add_rule(
  25. 'referrer',
  26. __( 'From a specific referrer', PO_LANG ),
  27. __( 'Shows the PopUp if the user arrived via a specific referrer.', PO_LANG ),
  28. 'no_referrer',
  29. 15
  30. );
  31. // 'no_referrer' rule.
  32. $this->add_rule(
  33. 'no_referrer',
  34. __( 'Not from a specific referrer', PO_LANG ),
  35. __( 'Hides the PopUp if the user arrived via a specific referrer.', PO_LANG ),
  36. 'referrer',
  37. 15
  38. );
  39. // 'no_internal' rule.
  40. $this->add_rule(
  41. 'no_internal',
  42. __( 'Not from an internal link', PO_LANG ),
  43. __( 'Shows the PopUp if the user did not arrive on this page via another page on your site.', PO_LANG ),
  44. '',
  45. 15
  46. );
  47. // 'searchengine' rule.
  48. $this->add_rule(
  49. 'searchengine',
  50. __( 'From a search engine', PO_LANG ),
  51. __( 'Shows the PopUp if the user arrived via a search engine.', PO_LANG ),
  52. '',
  53. 15
  54. );
  55. }
  56. /*==============================*\
  57. ==================================
  58. == ==
  59. == REFERRER ==
  60. == ==
  61. ==================================
  62. \*==============================*/
  63. /**
  64. * Apply the rule-logic to the specified popup
  65. *
  66. * @since 4.6
  67. * @param mixed $data Rule-data which was saved via the save_() handler.
  68. * @return bool Decission to display popup or not.
  69. */
  70. protected function apply_referrer( $data ) {
  71. return $this->test_referrer( $data );
  72. }
  73. /**
  74. * Output the Admin-Form for the active rule.
  75. *
  76. * @since 4.6
  77. * @param mixed $data Rule-data which was saved via the save_() handler.
  78. */
  79. protected function form_referrer( $data ) {
  80. if ( is_string( $data ) ) { $referrer = $data; }
  81. else if ( is_array( $data ) ) { $referrer = implode( "\n", $data ); }
  82. else { $referrer = ''; }
  83. ?>
  84. <label for="po-rule-data-referrer">
  85. <?php _e( 'Referrers. Can be full URL or a pattern like ".example.com" (one per line):', PO_LANG ); ?>
  86. </label>
  87. <textarea name="po_rule_data[referrer]" id="po-rule-data-referrer" class="block"><?php
  88. echo esc_attr( $referrer );
  89. ?></textarea>
  90. <?php
  91. }
  92. /**
  93. * Update and return the $settings array to save the form values.
  94. *
  95. * @since 4.6
  96. * @param array $data The contents of $_POST['po_rule_data'].
  97. * @return mixed Data collection of this rule.
  98. */
  99. protected function save_referrer( $data ) {
  100. lib2()->array->equip( $data, 'referrer' );
  101. return explode( "\n", $data['referrer'] );
  102. }
  103. /*==============================*\
  104. ==================================
  105. == ==
  106. == NO_REFERRER ==
  107. == ==
  108. ==================================
  109. \*==============================*/
  110. /**
  111. * Apply the rule-logic to the specified popup
  112. *
  113. * @since 4.6
  114. * @param mixed $data Rule-data which was saved via the save_() handler.
  115. * @return bool Decission to display popup or not.
  116. */
  117. protected function apply_no_referrer( $data ) {
  118. return ! $this->test_referrer( $data );
  119. }
  120. /**
  121. * Output the Admin-Form for the active rule.
  122. *
  123. * @since 4.6
  124. * @param mixed $data Rule-data which was saved via the save_() handler.
  125. */
  126. protected function form_no_referrer( $data ) {
  127. if ( is_string( $data ) ) { $no_referrer = $data; }
  128. else if ( is_array( $data ) ) { $no_referrer = implode( "\n", $data ); }
  129. else { $no_referrer = ''; }
  130. ?>
  131. <label for="po-rule-data-no_referrer">
  132. <?php _e( 'Referrers. Can be full URL or a pattern like ".example.com" (one per line):', PO_LANG ); ?>
  133. </label>
  134. <textarea name="po_rule_data[no_referrer]" id="po-rule-data-no_referrer" class="block"><?php
  135. echo esc_attr( $no_referrer );
  136. ?></textarea>
  137. <?php
  138. }
  139. /**
  140. * Update and return the $settings array to save the form values.
  141. *
  142. * @since 4.6
  143. * @param array $data The contents of $_POST['po_rule_data'].
  144. * @return mixed Data collection of this rule.
  145. */
  146. protected function save_no_referrer( $data ) {
  147. lib2()->array->equip( $data, 'no_referrer' );
  148. return explode( "\n", $data['no_referrer'] );
  149. }
  150. /*=================================*\
  151. =====================================
  152. == ==
  153. == NO_INTERNAL ==
  154. == ==
  155. =====================================
  156. \*=================================*/
  157. /**
  158. * Apply the rule-logic to the specified popup
  159. *
  160. * @since 4.6
  161. * @param mixed $data Rule-data which was saved via the save_() handler.
  162. * @return bool Decission to display popup or not.
  163. */
  164. protected function apply_no_internal( $data ) {
  165. if ( ! empty( $_POST['_po_method_'] ) && 'raw' == $_POST['_po_method_'] ) {
  166. // This indicates a form-submit from inside a popup. Naturally the
  167. // referrer will always be internal, so ignore this rule here.
  168. return true;
  169. } else {
  170. $internal = preg_replace( '#^https?://#', '', get_option( 'home' ) );
  171. return ! $this->test_referrer( $internal );
  172. }
  173. }
  174. /*==================================*\
  175. ======================================
  176. == ==
  177. == SEARCHENGINE ==
  178. == ==
  179. ======================================
  180. \*==================================*/
  181. /**
  182. * Apply the rule-logic to the specified popup
  183. *
  184. * @since 4.6
  185. * @param mixed $data Rule-data which was saved via the save_() handler.
  186. * @return bool Decission to display popup or not.
  187. */
  188. protected function apply_searchengine( $data ) {
  189. return $this->test_searchengine();
  190. }
  191. /*======================================*\
  192. ==========================================
  193. == ==
  194. == HELPER FUNCTIONS ==
  195. == ==
  196. ==========================================
  197. \*======================================*/
  198. /**
  199. * Tests if the current referrer is one of the referers of the list.
  200. * Current referrer has to be specified in the URL param "thereferer".
  201. *
  202. * @since 4.6
  203. * @param array $list List of referers to check.
  204. * @return bool
  205. */
  206. public function test_referrer( $list ) {
  207. $response = false;
  208. if ( is_string( $list ) ) { $list = array( $list ); }
  209. if ( ! is_array( $list ) ) { return true; }
  210. $referrer = $this->get_referrer();
  211. if ( empty( $referrer ) ) {
  212. $response = false;
  213. } else {
  214. foreach ( $list as $item ) {
  215. $item = trim( $item );
  216. $res = stripos( $referrer, $item );
  217. if ( false !== $res ) {
  218. $response = true;
  219. break;
  220. }
  221. }
  222. }
  223. return $response;
  224. }
  225. /**
  226. * Tests if the current referrer is a search engine.
  227. * Current referrer has to be specified in the URL param "thereferer".
  228. *
  229. * @since 4.6
  230. * @return bool
  231. */
  232. public function test_searchengine() {
  233. $response = false;
  234. $referrer = $this->get_referrer();
  235. $patterns = array(
  236. '/search?',
  237. '.google.',
  238. 'web.info.com',
  239. 'search.',
  240. 'del.icio.us/search',
  241. 'delicious.com/search',
  242. 'soso.com',
  243. '/search/',
  244. '.yahoo.',
  245. '.bing.',
  246. );
  247. foreach ( $patterns as $url ) {
  248. if ( false !== stripos( $referrer, $url ) ) {
  249. if ( $url == '.google.' ) {
  250. if ( $this->is_googlesearch( $referrer ) ) {
  251. $response = true;
  252. } else {
  253. $response = false;
  254. }
  255. } else {
  256. $response = true;
  257. }
  258. break;
  259. }
  260. }
  261. return $response;
  262. }
  263. /**
  264. * Checks if the referrer is a google web-source.
  265. *
  266. * @since 4.6
  267. * @param string $referrer
  268. * @return bool
  269. */
  270. public function is_googlesearch( $referrer = '' ) {
  271. $response = true;
  272. // Get the query strings and check its a web source.
  273. $qs = parse_url( $referrer, PHP_URL_QUERY );
  274. $qget = array();
  275. foreach ( explode( '&', $qs ) as $keyval ) {
  276. $kv = explode( '=', $keyval );
  277. if ( 2 == count( $kv ) ) {
  278. $qget[ trim( $kv[0] ) ] = trim( $kv[1] );
  279. }
  280. }
  281. if ( isset( $qget['source'] ) ) {
  282. $response = $qget['source'] == 'web';
  283. }
  284. return $response;
  285. }
  286. /**
  287. * Returns the referrer.
  288. *
  289. * @since 4.6
  290. * @return string
  291. */
  292. public function get_referrer() {
  293. $referrer = '';
  294. $is_ajax = (defined( 'DOING_AJAX' ) && DOING_AJAX)
  295. || ( ! empty( $_POST['_po_method_'] ) && 'raw' == $_POST['_po_method_'] );
  296. if ( isset( $_REQUEST['thereferrer'] ) ) {
  297. $referrer = $_REQUEST['thereferrer'];
  298. } else if ( ! $is_ajax && isset( $_SERVER['HTTP_REFERER'] ) ) {
  299. // When doing Ajax request we NEVER use the HTTP_REFERER!
  300. $referrer = $_SERVER['HTTP_REFERER'];
  301. }
  302. return $referrer;
  303. }
  304. };
  305. IncPopupRules::register( 'IncPopupRule_Referrer' );