diff --git a/src/Plugin/ActivationTracker.php b/src/Plugin/ActivationTracker.php new file mode 100644 index 0000000000000000000000000000000000000000..e9a217a766ead233f62db8099b9ce8e915f38b18 --- /dev/null +++ b/src/Plugin/ActivationTracker.php @@ -0,0 +1,71 @@ +<?php + +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/HookableParent.php b/src/Plugin/HookableParent.php new file mode 100644 index 0000000000000000000000000000000000000000..937ff77efe5ba9c3528e69c3fd78586369e9db34 --- /dev/null +++ b/src/Plugin/HookableParent.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 $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_on_hookable_objects() { + /** @var Hookable $hookable_object $hookable_object */ + foreach ( $this->hookable_objects as $hookable_object ) { + $hookable_object->hooks(); + } + } + + +} \ No newline at end of file diff --git a/src/Plugin/TemplateLoad.php b/src/Plugin/TemplateLoad.php new file mode 100644 index 0000000000000000000000000000000000000000..89f1f782942be4944157d06d95ed209a01c70aaa --- /dev/null +++ b/src/Plugin/TemplateLoad.php @@ -0,0 +1,61 @@ +<?php + +namespace WPDesk\PluginBuilder\Plugin; + +trait TemplateLoad { + + /** + * Plugin path. + * + * @var string + */ + protected $plugin_path; + + /** + * Template path. + * + * @var string + */ + protected $template_path; + + /** + * Init base variables for plugin + */ + public function init_template_base_variables() { + $this->plugin_path = $this->plugin_info->get_plugin_dir(); + $this->template_path = $this->plugin_info->get_text_domain(); + } + + /** + * Renders end returns selected template + * + * @param string $name Name of the template. + * @param string $path Additional inner path to the template. + * @param array $args args Accessible from template. + * + * @return string + */ + public function load_template( $name, $path = '', $args = array() ) { + $plugin_template_path = trailingslashit( $this->plugin_path ) . 'templates/'; + + // Look within passed path within the theme - this is priority. + $template = locate_template( + array( + trailingslashit( $this->get_template_path() ) . trailingslashit( $path ) . $name . '.php', + ) + ); + + if ( ! $template ) { + $template = $plugin_template_path . trailingslashit( $path ) . $name . '.php'; + } + + extract( $args ); + ob_start(); + include( $template ); + + return ob_get_clean(); + } + + + +} \ No newline at end of file