From 57f32a5f6d5f48c3f88cddd2bf79f33a4bbf652c Mon Sep 17 00:00:00 2001
From: Grzegorz Rola <grola@seostudio.pl>
Date: Wed, 25 Jul 2018 17:10:53 +0200
Subject: [PATCH] Added ActivationTracker class and HookableParent trait.

---
 src/Plugin/ActivationTracker.php | 77 ++++++++++++++++++++++++++++++++
 src/Plugin/HookableParetn.php    | 37 +++++++++++++++
 2 files changed, 114 insertions(+)
 create mode 100644 src/Plugin/ActivationTracker.php
 create mode 100644 src/Plugin/HookableParetn.php

diff --git a/src/Plugin/ActivationTracker.php b/src/Plugin/ActivationTracker.php
new file mode 100644
index 0000000..b355605
--- /dev/null
+++ b/src/Plugin/ActivationTracker.php
@@ -0,0 +1,77 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: grola
+ * Date: 25.07.18
+ * Time: 16:55
+ */
+
+namespace WPDesk\PluginBuilder\Plugin;
+
+
+class ActivationTracker {
+
+	/**
+	 * Namespace.
+	 *
+	 * @var string
+	 */
+	private $namespace;
+
+	/**
+	 * ActivationTracker constructor.
+	 *
+	 * @param string $namespace Namespace for settings.
+	 */
+	public function __construct( $namespace ) {
+		$this->namespace = $namespace;
+	}
+
+	/**
+	 * Option name for date storage
+	 *
+	 * @return string
+	 */
+	private function get_option_name_activation_date() {
+		return $this->namespace . '_activation';
+	}
+
+	/**
+	 * Returns activation date and sets it if were not set before
+	 *
+	 * @return int unix timestamp for activation datetime
+	 */
+	public function get_activation_date() {
+		$activation_date
+			= get_option( $this->get_option_name_activation_date() );
+		if ( empty( $activation_date ) ) {
+			return $this->touch_activation_date();
+		}
+
+		return intval( $activation_date );
+	}
+
+	/**
+	 * Was activation more than two weeks before today
+	 *
+	 * @return bool
+	 */
+	public function is_activated_more_than_two_weeks() {
+		$two_weeks = 60 * 60 * 24 * 7 * 2;
+
+		return $this->get_activation_date() + $two_weeks < time();
+	}
+
+	/**
+	 * Sets activatiion date for today
+	 *
+	 * @return int unit timestamp for now
+	 */
+	public function touch_activation_date() {
+		$now = time();
+		update_option( $this->get_option_name_activation_date(), $now );
+
+		return $now;
+	}
+
+}
\ No newline at end of file
diff --git a/src/Plugin/HookableParetn.php b/src/Plugin/HookableParetn.php
new file mode 100644
index 0000000..e99e19d
--- /dev/null
+++ b/src/Plugin/HookableParetn.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace WPDesk\PluginBuilder\Plugin;
+
+trait HookableParent {
+
+	/**
+	 * Hookable objects.
+	 *
+	 * @var array[Hookable]
+	 */
+	private $hookable_objects = array();
+
+	/**
+	 * Add hookable object.
+	 *
+	 * @param Hookable|HookablePluginDependant $hookable_object Hookable object.
+	 */
+	public function add_hookable( $hookable_object ) {
+		if ( $hookable_object instanceof HookablePluginDependant ) {
+			$hookable_object->set_plugin( $this );
+		}
+		$this->hookable_objects[] = $hookable_object;
+	}
+
+	/**
+	 * Run hooks method on all hookable objects.
+	 */
+	protected function hooks_no_hookable_objects() {
+		/** @var Hookable $hookable_object $hookable_object */
+		foreach ( $this->hookable_objects as $hookable_object ) {
+			$hookable_object->hooks();
+		}
+	}
+
+
+}
\ No newline at end of file
-- 
GitLab