diff --git a/LICENSE b/LICENSE index 84b4a64..e6684cf 100644 --- a/LICENSE +++ b/LICENSE @@ -18,4 +18,6 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file +SOFTWARE. + +License IS COMPATIBLE with GNU General Public License. diff --git a/assets/banner-772x250.png b/assets/banner-772x250.png new file mode 100644 index 0000000..8c1056a Binary files /dev/null and b/assets/banner-772x250.png differ diff --git a/assets/icon-128x128.png b/assets/icon-128x128.png new file mode 100644 index 0000000..7448486 Binary files /dev/null and b/assets/icon-128x128.png differ diff --git a/assets/icon-256x256.png b/assets/icon-256x256.png new file mode 100644 index 0000000..1ae03f0 Binary files /dev/null and b/assets/icon-256x256.png differ diff --git a/assets/screenshot-1.png b/assets/screenshot-1.png new file mode 100644 index 0000000..8740ab7 Binary files /dev/null and b/assets/screenshot-1.png differ diff --git a/assets/screenshot-2.png b/assets/screenshot-2.png new file mode 100644 index 0000000..57d3ef0 Binary files /dev/null and b/assets/screenshot-2.png differ diff --git a/assets/screenshot-3.png b/assets/screenshot-3.png new file mode 100644 index 0000000..3b1ba1d Binary files /dev/null and b/assets/screenshot-3.png differ diff --git a/deploy-exclude.lst b/deploy-exclude.lst index 72bac22..d43124a 100644 --- a/deploy-exclude.lst +++ b/deploy-exclude.lst @@ -3,6 +3,7 @@ *.idea* *.idea* *nbproject* +*assets* *dist* composer.json composer.lock diff --git a/deploy.php b/deploy.php index 73ef369..7de37f7 100644 --- a/deploy.php +++ b/deploy.php @@ -6,7 +6,7 @@ * @author Redbit s.r.o. */ -$deploy = new DeployScript( 'simple-shop-connector' ); +$deploy = new DeployScript( 'simpleshop-cz' ); $deploy->distDir = __DIR__ . '/dist'; try { @@ -92,7 +92,7 @@ private function buildPackage() { __DIR__ . '/' . $this->productName . '.php', array( 'Version: dev-master' => sprintf( 'Version: %s', $this->getNakedVersion() ), - 'define(\'SSC_PLUGIN_VERSION\',\'dev-master\');' => sprintf( 'define(\'SSC_PLUGIN_VERSION\',\'%s\');', + 'define( \'SIMPLESHOP_PLUGIN_VERSION\', \'dev-master\' );' => sprintf( 'define( \'SIMPLESHOP_PLUGIN_VERSION\', \'%s\' );', $this->version ), ), $this->distDir @@ -191,12 +191,12 @@ private function zip( $fileOrPath, $zip, $baseDir = __DIR__ ) { } /** - * Strips "v" from versions (v1.2.3 -> 1.2.3) + * Strips "v" from versions (v1.2.3-beta -> 1.2.3) * * @return bool|string */ private function getNakedVersion() { - return substr( $this->version, 1 ); + return preg_replace('/^v(\d+\.\d+\.\d+)(?:-.*)+$/D', '$1', $this->version); } /** diff --git a/img/icon.png b/img/icon.png new file mode 100644 index 0000000..e3c1994 Binary files /dev/null and b/img/icon.png differ diff --git a/includes/class.ssc_access.php b/includes/class.ssc_access.php deleted file mode 100644 index b062304..0000000 --- a/includes/class.ssc_access.php +++ /dev/null @@ -1,249 +0,0 @@ -get_post_groups(); - - // If the post is protected and user is not logged in, redirect him to login - if($post_groups && !is_user_logged_in()){ - wp_safe_redirect(wp_login_url($_SERVER['REQUEST_URI'])); - } - - // Check if current user has access to the post, if not, redirect him to defined URL or home if the URL is not set - if($post_groups && !$this->user_can_view_post() && !is_home() && !is_front_page()){ - $no_access_url = $this->get_no_access_redirect_url(); - - $url = $no_access_url ?: site_url(); - wp_redirect($url); - exit(); - } - } - - /** - * Check if user has permission to view the post - * @param string $post_id - * @param string $user_id - * @return bool|\WP_Error - */ - function user_can_view_post($post_id = '',$user_id = ''){ - // Admins can view all posts - - if($this->user_is_admin()) - return true; - - global $post; - if(!$post_id) - $post_id = $post->ID; - - if(!$user_id) - $user_id = get_current_user_id(); - - - if(!($post_id > 0) || !($user_id >= 0)){ - return new \WP_Error('400','Wrong post ID or user ID'); - } - - $post_groups = $this->get_post_groups($post_id); - - - if(!$post_groups || $post_groups == '') - return true; - - - foreach($post_groups as $post_group){ - $group = new SSC_Group($post_group); - if($group->user_is_member_of_group($user_id)){ - // Ok, the user is member of at least one group that has access to this post - - // Check, if the post has set date, after which it can be accessed - if ($date_to_access = $this->get_post_date_to_access()) { - if (date('Y-m-d') < $date_to_access) { - // The post should not be accessed yet, not depending on group, so just return false - return false; - } - } - - // Check, if the post has set date, until which it can be accessed - if ($date_to_access = $this->get_post_date_until_to_access()) { - if (date('Y-m-d') > $date_to_access) { - // The post should not be accessed yet, not depending on group, so just return false - return false; - } - } - - // The user is member of some group, check if the post has minimum days to access set - $membership = new SSC_Membership($user_id); - - // Check if the subscription is valid - if (isset($membership->groups[$post_group]['valid_to']) && $membership->groups[$post_group]['valid_to']) { - if ($membership->groups[$post_group]['valid_to'] < date('Y-m-d')) { - // if the the subscription expired, just break the loop here, as the user might have multiple subscriptions - continue; - } - } - - if ($days_to_access = $this->get_post_days_to_access()) { - $subscription_date = $membership->groups[$post_group]['subscription_date']; - // Get the date of subscription to the group - if ($subscription_date > date('Y-m-d', strtotime("now -$days_to_access days"))) { - // if the user does not have access YET, just break the loop here, as the user might have multiple subscriptions - continue; - } - } - - return true; - } - } - - return false; - } - - /** - * Check if the user is admin - * This is filterable, - * @return mixed - */ - function user_is_admin(){ - $is_admin = current_user_can('administrator') ? true : false; - return apply_filters('ssc_user_is_admin',$is_admin); - } - - /** - * Get the URL to redirect the user if he has no access - * @param string $post_id - * - * @return mixed - */ - function get_no_access_redirect_url($post_id = ''){ - global $post; - - if(!$post_id) - $post_id = $post->ID; - - // First check, if we should redirect the user to login form -// if ($redirect_post_id = get_post_meta($post_id, SSC_PREFIX . 'no_access_redirect_to_login_form', true)) { -// $actual_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; -// return wp_login_url($actual_url); -// } - - // Next try to get the ID and return permalink - if($redirect_post_id = get_post_meta($post_id,'_ssc_no_access_redirect_post_id',true)){ - return get_the_permalink($redirect_post_id); - } - - return get_post_meta($post_id,'_ssc_no_access_redirect',true); - } - - /** - * Setup the cart in menu - * @param $item - * @return mixed - */ - function setup_nav_menu_item($item){ - - if(!$this->user_can_view_post($item->object_id)){ - $item->classes[] = 'ssc-hide'; - $item->title = ''; - $item->url = ''; - return $item; - } - - return $item; - } - - function get_post_groups($post_id = ''){ - global $post; - - if(!$post_id) - $post_id = $post->ID; - - return get_post_meta($post_id,'_ssc_groups',true); - } - - /** - * Get the number of days the user has to be subscribed to have access to the post - * @param string $post_id - * @return mixed - */ - function get_post_days_to_access($post_id = '') - { - global $post; - - if (!$post_id) - $post_id = $post->ID; - - return get_post_meta($post_id, SSC_PREFIX . 'days_to_access', true); - } - - /** - * Get the date to access the post - * @param string $post_id - * @return mixed - */ - function get_post_date_to_access($post_id = '') - { - global $post; - - if (!$post_id) - $post_id = $post->ID; - - return get_post_meta($post_id, SSC_PREFIX . 'date_to_access', true); - } - - /** - * Get the date until the access to the post is allowed - * @param string $post_id - * @return mixed - */ - function get_post_date_until_to_access($post_id = '') - { - global $post; - - if (!$post_id) - $post_id = $post->ID; - - return get_post_meta($post_id, SSC_PREFIX . 'date_until_to_access', true); - } - - - /** - * Hide items in menu - */ - function hide_menu_items(){ - ?> - - email && $ssc->secure_key){ - $vyfakturuj_api = new Vyfakturuj\VyfakturujAPI($ssc->email,$ssc->secure_key); - $ret = $vyfakturuj_api->getProducts(); - - if($ret){ - foreach($ret as $product){ - $values[$product['code']] = $product['name']; - } - } - } - echo json_encode($values); - exit(); - } - - /** - * Remove quick edit from groups - * @param $actions - * @param $post - * @return mixed - */ - function remove_quick_edit($actions,$post){ - if($post->post_type == "ssc_group"){ - - unset($actions['inline hide-if-no-js']); - } - return $actions; - } - - /** - * Hide publishing actions in group detail - */ - function publishing_actions(){ - $mg_post_type = 'ssc_group'; - global $post; - if($post->post_type == $mg_post_type){ - echo ''; - } ?> - - - - - __('Členské sekce','ssc'), - 'singular_name' => __('Skupina','ssc'), - 'menu_name' => __('Členské sekce','ssc'), - 'name_admin_bar' => __('Členské sekce','ssc'), - 'add_new' => __('Přidat skupinu','ssc'), - 'add_new_item' => __('Přidat novou skupinu','ssc'), - 'new_item' => __('Nová skupina','ssc'), - 'edit_item' => __('Upravit skupinu','ssc'), - 'view_item' => __('Zobrazit skupinu','ssc'), - 'all_items' => __('Členské sekce','ssc'), - 'search_items' => __('Hledat skupiny','ssc'), - 'parent_item_colon' => __('Nadřazená skupina:','ssc'), - 'not_found' => __('Nebyly nalezeny žádné skupiny.','ssc'), - 'not_found_in_trash' => __('Žádné skupiny v koši','ssc') - ); - - $args = array( - 'labels' => $labels, - 'public' => false, - 'publicly_queryable' => false, - 'show_ui' => true, - 'show_in_menu' => 'simple_shop_settings', - 'query_var' => true, - 'capability_type' => 'post', - 'has_archive' => false, - 'hierarchical' => true, - 'menu_position' => null, - 'supports' => array('title') - ); - - register_post_type('ssc_group',$args); - } - - /** - * Register a custom menu page. - */ - function add_settings_page(){ - add_menu_page( - __('SimpleShop','ssc'),__('SimpleShop','ssc'),'manage_options','simple_shop_settings',array($this,'render_settings_page'),SSC_PLUGIN_URL.'/img/white_logo.png',99 - ); - } - - /** - * Add custom columns to admin groups listing - * @param $columns - * @return mixed - */ - function ssc_group_columns($columns){ - $columns['ssc_id'] = 'SSC ID'; - - return $columns; - } - - /** - * Add content to custom columns in groups listing - * @param $column - * @param $post_id - */ - function ssc_group_column_content($column,$post_id){ - global $post; - - switch($column){ - case 'ssc_id' : - echo $post->ID; - break; - } - } - - /** - * Enqueue admin scripts - */ - function enqueue_admin_scripts() { - wp_enqueue_style( 'ssc', SSC_PLUGIN_URL.'css/ssc.css' ); - wp_register_style( 'jquery-ui', 'http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css' ); - wp_enqueue_style( 'jquery-ui' ); - } - -} - -new SSC_Admin(); diff --git a/includes/class.ssc_cron.php b/includes/class.ssc_cron.php deleted file mode 100644 index 5210b72..0000000 --- a/includes/class.ssc_cron.php +++ /dev/null @@ -1,119 +0,0 @@ - 'any', - 'posts_per_page' => -1, - 'meta_query' => array( - 'relation' => 'OR', - SSC_PREFIX . 'days_to_access' => array( - 'key' => SSC_PREFIX . 'days_to_access', - 'compare' => 'EXISTS' - ), - SSC_PREFIX . 'date_to_access' => array( - 'key' => SSC_PREFIX . 'date_to_access', - 'compare' => 'EXISTS' - ) - ) - ); - - $the_query = new \WP_Query($args); - - - if ($the_query->have_posts()) { - - $group = new SSC_Group(); - - // Get all users - $users = get_users(); - - // Get all groups to array - $users_groups = array(); - foreach ($users as $user) { - $membership = new SSC_Membership($user->ID); - $users_groups[$user->ID] = $membership->groups; - } - - while ($the_query->have_posts()) { - $the_query->the_post(); - global $post; - - // Check if the post has some email set, if not, continue - $email_text = get_post_meta($post->ID, SSC_PREFIX . 'email_user_can_access', true); - if (!$email_text) - continue; - - $email_subject = get_post_meta($post->ID, SSC_PREFIX . 'email_subject_user_can_access', true); - - $access = new SSC_Access(); - // Get post groups - $groups = $access->get_post_groups(); - // Get days to access - $days_to_access = $access->get_post_days_to_access(); - // Get date to access - $date_to_access = $access->get_post_date_to_access(); - - // TODO: Rewrite this to first find the groups that have access to the post, than find users for these groups. - // That way we won't have to scrub through all the users all the time - // Scrub through the groups and check, if the user is member of the group - foreach ($groups as $group) { - foreach ($users_groups as $user_id => $user_groups) { - $send_email = false; - - // Check, if the user is member of this group - if (array_key_exists($group, $user_groups)) { - // If so, finally check, if we should send the email - - // First check, if today is the date when the post can be accessed - if ($date_to_access == date('Y-m-d')) { - // Cool, send email - $send_email = true; - } elseif ($days_to_access) { - $subscribed = $user_groups[$group]['subscription_date']; - $date_to_compare = date('Y-m-d', strtotime("$subscribed -$days_to_access days")); - - if (date('Y-m-d') == $date_to_compare) { - $send_email = true; - } - } - } - - if ($send_email) { - // Woohoo, send the email - $userdata = get_userdata($user_id); - if (!get_user_meta($user_id, SSC_PREFIX . 'notification_email_sent_' . $post->ID, true)) { - wp_mail($userdata->user_email, $email_subject, $email_text); - update_user_meta($user_id, SSC_PREFIX . 'notification_email_sent_' . $post->ID, 1); - } - } - } - } - } - } - - wp_reset_postdata(); - } -} - -new SSC_Cron(); -//add_action('init', '\SSC\test2'); -function test2() -{ - $cron = new SSC_Cron(); - $cron->send_user_has_access_to_post_notification(); -} diff --git a/includes/class.ssc_groups.php b/includes/class.ssc_groups.php deleted file mode 100644 index c865d95..0000000 --- a/includes/class.ssc_groups.php +++ /dev/null @@ -1,119 +0,0 @@ -id = $id; - $this->get_group(); - } - } - - /** - * Get all groups (custom post type) - * @return array - */ - function get_groups(){ - - $args = array( - 'post_type' => 'ssc_group', - 'posts_per_page' => -1, - 'post_status' => 'publish' - ); - - $groups = array(); - - $posts = get_posts($args); - - foreach ($posts as $item) { - $groups[$item->ID] = $item->post_title; - } - - return $groups; - } - - /** - * Get a single group - * @return bool - */ - function get_group(){ - $group = get_post($this->id); - if($group){ - // Set the group details - - $this->name = $group->post_title; - return true; - }else{ - return false; - } - } - - /** - * Check if group exists - * @return array|null|\WP_Post - */ - function group_exists(){ - return get_post($this->id); - } - - /** - * Get groups the user belongs to - * @param string $user_id - * @return mixed - */ - function get_user_groups($user_id = ''){ - if(!$user_id) - $user_id = get_current_user_id(); - - return get_user_meta($user_id,'_ssc_user_groups',true); - } - - /** - * Add user to a group - * @param $user_id - */ - function add_user_to_group($user_id){ - $groups = $this->get_user_groups($user_id); - - if(!$groups) - $groups = array(); - - if(!in_array($this->id,$groups)){ - $groups[] = $this->id; - update_user_meta($user_id,'_ssc_user_groups',$groups); - - // Set the date of user registration to the group - $membership = new SSC_Membership($user_id); - $membership->set_subscription_date($this->id); - } - } - - /** - * Check if user is a member of a group - * @param $user_id - * @return bool - */ - function user_is_member_of_group($user_id){ - if(!$user_id) - $user_id = get_current_user_id(); - - $groups = $this->get_user_groups($user_id); - - if(!is_array($groups)){ - return false; - } - if(in_array($this->id,$groups)){ - return true; - }else{ - return false; - } - } - -} - -new SSC_Group(); diff --git a/includes/class.ssc_membership.php b/includes/class.ssc_membership.php deleted file mode 100644 index 68c4b3c..0000000 --- a/includes/class.ssc_membership.php +++ /dev/null @@ -1,87 +0,0 @@ -user_id = (int)$user_id; - $this->get(); - } - } - - /** - * Get membership data for a specific user - */ - function get() - { - $ssc_groups = new SSC_Group(); - $groups = $ssc_groups->get_user_groups($this->user_id); - - foreach ($groups as $group) { - $this->groups[$group] = array( - 'group_id' => $group, - 'subscription_date' => $this->get_subscription_date($group), - 'valid_to' => $this->get_valid_to($group) - ); - } - } - - /** - * Set the date until the memership is valid - * @param $group_id - * @param $valid_to - * @return bool|int - */ - function set_valid_to($group_id, $valid_to) - { - if (!$this->user_id) - return false; - - return update_user_meta($this->user_id, '_ssc_group_subscription_valid_to_' . $group_id, $valid_to); - } - - /** - * Set the date of user subscription to the group - * @param $group_id - * @return bool|int - */ - function set_subscription_date($group_id) - { - if (!$this->user_id) - return false; - - return update_user_meta($this->user_id, '_ssc_group_subscription_date_' . $group_id, date('Y-m-d')); - } - - /** - * Get group subscription date - * @param $group_id - * @return mixed - */ - function get_subscription_date($group_id) - { - return get_user_meta($this->user_id, '_ssc_group_subscription_date_' . $group_id, true); - } - - /** - * Get the date until the subscription is valid for specific group - * @param $group_id - * @return mixed - */ - function get_valid_to($group_id) - { - return get_user_meta($this->user_id, '_ssc_group_subscription_valid_to_' . $group_id, true); - } -} - diff --git a/includes/class.ssc_metaboxes.php b/includes/class.ssc_metaboxes.php deleted file mode 100644 index 1d92593..0000000 --- a/includes/class.ssc_metaboxes.php +++ /dev/null @@ -1,168 +0,0 @@ -get_groups(); - $ssc_access = new SSC_Access(); - $ssc = new SSC(); - $post_types = $ssc->get_post_types(); - - - if($groups && $ssc_access->user_is_admin()){ - /** - * Initiate the metabox - */ - $cmb = new_cmb2_box(array( - 'id' => 'ssc_page_groups', - 'title' => __('SimpleShop - členské sekce','ssc'), - 'object_types' => $post_types, - 'context' => 'normal', - 'priority' => 'high', - 'show_names' => true, - )); - - $cmb->add_field(array( - 'name' => __('Členské sekce, které mají přístup na stránku','ssc'), - 'desc' => __('Pouze uživatelé v zaškrtnutých členskéch sekcích mají přístup na tuto stránku. Pokud nic nezaškrtnete, stránku uvidí všichni uživatelé','ssc'), - 'id' => $this->prefix.'groups', - 'type' => 'multicheck', - 'options' => $groups, - )); - - $tmp_post_types = $post_types; - unset($tmp_post_types['attachment']); - - $cmb->add_field(array( - 'name' => __('ID stránky pro přesměrování','ssc'), - 'desc' => __('Vyberte stránku, na kterou uživatel bude přesměrován, pokud je přihlášen, ale nemá oprávnění k přístupu. Toto je preferovaný způsob - přsměrování bude fungovat, i pokud se v budoucnu změní adresa stránky','ssc'), - 'id' => $this->prefix.'no_access_redirect_post_id', - 'type' => 'post_search_text', - 'select_type' => 'radio', - 'select_behavior' => 'replace', - 'post_type' => $tmp_post_types - )); - - - $cmb->add_field(array( - 'name' => __('Manuální adresa přesměrování','ssc'), - 'desc' => __('Zadejte ručně adresu, na kterou uživatel bude přesměrován, pokud je přihlášen, ale nemá oprávnění k přístupu. Pokud využijete tuto volbu, pole výše musí být prázdné.','ssc'), - 'id' => $this->prefix.'no_access_redirect', - 'type' => 'text' - )); - -// $cmb->add_field(array( -// 'name' => __('Přesměrovat na přihlášení', 'ssc'), -// 'desc' => __('Zaškrtněte, pokud chcete uživatele přesměrovat na přihlašovací formulář. Po přihlášení bude uživatel přesměrován zpět na tuto stránku.', 'ssc'), -// 'id' => $this->prefix . 'no_access_redirect_to_login_form', -// 'type' => 'checkbox' -// )); - - - $cmb->add_field(array( - 'name' => __('Povolit přístup po X dnech od přiřazení do skupiny', 'ssc'), - 'desc' => __('Zadejte počet dní, které musí uplynout od přihlášení uživatele do skupiny pro získání přístupu k tomuto obsahu. Pokud např. uživatel koupí produkt 1.ledna a nastavíte 5 dní, uživatel bude mít ke stránce přístup 6. ledna.', 'ssc'), - 'id' => $this->prefix . 'days_to_access', - 'type' => 'text' - )); - - $cmb->add_field(array( - 'name' => __('Povolit přístup OD data', 'ssc'), - 'desc' => __('Zadejte datum, od kterého bude stránka přístupná (platí pro všechny skupiny)', 'ssc'), - 'id' => $this->prefix . 'date_to_access', - 'type' => 'text_date', - 'date_format' => 'Y-m-d', - )); - - $cmb->add_field(array( - 'name' => __('Povolit přístup DO data', 'ssc'), - 'desc' => __('Zadejte datum, do kterého bude stránka přístupná (platí pro všechny skupiny)', 'ssc'), - 'id' => $this->prefix . 'date_until_to_access', - 'type' => 'text_date', - 'date_format' => 'Y-m-d', - )); - - $cmb->add_field(array( - 'name' => __('Předmět emailu při zpřístupnění obsahu', 'ssc'), - 'desc' => __('Zadejte předmět emailu, který se uživateli automaticky odešlě v okamžiku, kdy získá přístup k tomuto obsahu na základě nastavení dní výše.', 'ssc'), - 'id' => $this->prefix . 'email_subject_user_can_access', - 'type' => 'text' - )); - - $cmb->add_field(array( - 'name' => __('Email při zpřístupnění obsahu', 'ssc'), - 'desc' => __('Zadejte email, který se uživateli automaticky odešlě v okamžiku, kdy získá přístup k tomuto obsahu na základě nastavení dní výše.', 'ssc'), - 'id' => $this->prefix . 'email_user_can_access', - 'type' => 'wysiwyg' - )); - } - } - - /** - * Add metabox to user profile - */ - function user_metaboxes(){ - - /** - * Initiate the metabox - */ - $cmb = new_cmb2_box(array( - 'id' => 'ssc_user_groups', - 'title' => __('SimpleShop','ssc'), - 'object_types' => array('user'), - 'context' => 'normal', - 'priority' => 'high', - 'show_names' => true, - )); - - $ssc_group = new SSC_Group(); - $groups = $ssc_group->get_groups(); - - - $access = new SSC_Access(); - - if ($access->user_is_admin()) { - $cmb->add_field(array( - 'name' => __('SimpleShop - členské sekce
Vyberte, do kterých členských sekcí má mít uživatel přístup.','ssc'), -// 'desc' => __('Vyberte, do kterých členských sekcí má mít uživatel přístup','ssc'), - 'id' => $this->prefix.'user_groups', - 'type' => 'multicheck', - 'options' => $groups, - )); - - foreach ($groups as $key => $group) { - $cmb->add_field(array( - 'name' => ''.sprintf(__('Datum registrace do skupiny %s.','ssc'),$group).'', - 'id' => $this->prefix.'group_subscription_date_'.$key, - 'type' => 'text_date', - 'date_format' => 'Y-m-d', - )); - $cmb->add_field(array( - 'name' => ''.sprintf(__('Datum vypršení platnosti registrace do skupiny %s.','ssc'),$group).'', - 'id' => $this->prefix.'group_subscription_valid_to_'.$key, - 'type' => 'text_date', - 'date_format' => 'Y-m-d', - )); - } - } - - } - -} - -new SSC_Metaboxes(); diff --git a/includes/class.ssc_rest.php b/includes/class.ssc_rest.php deleted file mode 100644 index 7f1bef7..0000000 --- a/includes/class.ssc_rest.php +++ /dev/null @@ -1,226 +0,0 @@ - \WP_REST_Server::READABLE, - 'callback' => array($this,'get_groups'), - 'permission_callback' => array($this,'create_item_permissions_check'), - 'args' => $this->get_endpoint_args_for_item_schema(true), - ), - )); - - register_rest_route($namespace,'/add-member',array( - array( - 'methods' => \WP_REST_Server::READABLE, - 'callback' => array($this,'create_item'), - 'permission_callback' => array($this,'create_item_permissions_check'), - 'args' => $this->get_endpoint_args_for_item_schema(true), - ), - )); - -// register_rest_route($namespace,'/'.$.'/schema',array( -// 'methods' => \WP_REST_Server::READABLE, -// 'callback' => array($this,'get_public_item_schema'), -// )); - } - - function get_groups(){ - $ssc_group = new SSC_Group(); - return new \WP_REST_Response($ssc_group->get_groups(),200); - } - - /** - * Create one item from the collection - * - * @param \WP_REST_Request $request Full data about the request. - * - * @return \WP_REST_Response - */ - public function create_item($request){ - // Check if we got all the needed params - $params_to_validate = array('email'); - foreach($params_to_validate as $param){ - if(!$request->get_param($param)) - return new \WP_Error('required-param-missing',sprintf(__('Required parameter %s is missing','ssc'),$param),array('status' => 500,'plugin_version' => SSC_PLUGIN_VERSION)); - } - - // Check if we got valid email - $email = sanitize_email($request->get_param('email')); - if(!is_email($email)){ - return new \WP_Error('wrong-email-format',__('The email is in wrong format','ssc'),array('status' => 500,'plugin_version' => SSC_PLUGIN_VERSION)); - } - - // Check if user with this email exists, if not, create a new user - $_login = $email; - $_password = 'Změnit ho můžete zde'; - if(!email_exists($email)){ - $_password = wp_generate_password(8,false); - $userdata = array( - 'user_login' => $email, - 'user_email' => $email, - 'first_name' => sanitize_text_field($request->get_param('firstname')), - 'last_name' => sanitize_text_field($request->get_param('lastname')), - 'user_pass' => $_password, - ); - - $user_id = wp_insert_user($userdata); -// wp_new_user_notification($user_id,$userdata['user_pass']); // poslani notifikacniho e-mailu - - if(is_wp_error($user_id)) - return new \WP_Error('could-not-create-user',__("The user couldn't be created",'ssc'),array('status' => 500,'plugin_version' => SSC_PLUGIN_VERSION)); - }else{ - // Get user_by email - $user = get_user_by('email',$email); - $user_id = $user->ID; - } - - // Check if group exists - $user_groups = array(); - foreach($request->get_param('user_group') as $group){ - $ssc_group = new SSC_Group($group); - - // Add the user to group - if($ssc_group->group_exists()){ - $ssc_group->add_user_to_group($user_id); - - // Set the membership valid_to param - $membership = new SSC_Membership($user_id); - $valid_to = $request->get_param('valid_to') ?: ''; - $membership->set_valid_to($group,$valid_to); - - $user_groups[] = $group; - } - } - - // If we are on multisite, add the user the site - if(is_multisite()) - add_user_to_blog(get_current_blog_id(),$user_id,'subscriber'); - - // Get the posts that have some group assigned - global $wpdb; - $posts = $wpdb->get_results("SELECT post_id, meta_value - FROM $wpdb->postmeta - WHERE meta_key = '_ssc_groups' - "); - - // Get the post details - $links = array(); - $i = 0; - - // Foreach group from request - // foreach($request->get_param('user_group') as $group){ - // Foreach each group - $SSC_group = new SSC_Group(); - foreach($SSC_group->get_user_groups($user_id) as $group){ - // Scrub through posts and check, if some of the posts has that group assigned - foreach($posts as $post){ - $access = new SSC_Access(); - - if(in_array($group,unserialize($post->meta_value))){ - // Check if the post can be accessed already, if not, continue - $specific_date = $access->get_post_date_to_access($post->post_id); - $days_to_access = $access->get_post_days_to_access($post->post_id); - - if($specific_date && date('Y-m-d') < $specific_date) - continue; - - if($days_to_access && $days_to_access > 0) - continue; - - // If so, get the post details and add it to the links array - $post_details = get_post($post->post_id); - $links[$group][$i]['title'] = $post_details->post_title; - $links[$group][$i]['url'] = get_permalink($post->post_id); - $i++; - } - } - } - - $email_enable = nl2br(ssc_get_option('ssc_email_enable')); - - // It doesn't seem to make sense to send email without the links, so check first - if(((string) $email_enable) != '2'){ // pokud nemame zakazano posilat mail novym clenum - $email_body = nl2br(ssc_get_option('ssc_email_text')); - $email_subject = nl2br(ssc_get_option('ssc_email_subject')); - $pages = ''; - foreach($links as $groupid => $linksInGroup){ - $post_details = get_post($groupid); - $pages .= '
'.$post_details->post_title.'
' - .''; - } - - $replaceArray = array(// pole ktera je mozne nahradit - '{pages}' => $pages,// zpetna kompatibilita s v1.1 - '{mail}' => $email,// zpetna kompatibilita s v1.1 - '{login}' => $_login, - '{password}' => $_password, - '{login_url}' => wp_login_url(), -// '{login}' => $_login, -// '{password}' => $_password, - ); - $email_body = str_replace(array_keys($replaceArray),array_values($replaceArray),$email_body); -// $email_body = str_replace('{pages}',$pages,$email_body); -// $email_body = str_replace('{mail}',$email,$email_body); - $headers = array('Content-Type: text/html; charset=UTF-8'); - - // Send the email - wp_mail($email,$email_subject,$email_body,$headers); - } - - return new \WP_REST_Response(array('status' => 'success','plugin_version' => SSC_PLUGIN_VERSION),200); - } - - /** - * Check if a given request has access to create items - * - * @param \WP_REST_Request $request Full data about the request. - * @return \WP_Error|bool - */ - public function create_item_permissions_check($request){ - $ssc = new SSC(); - return $ssc->validate_secure_key($request->get_param('hash')); - } - - /** - * Prepare the item for create or update operation - * - * @param \WP_REST_Request $request Request object - * - * @return array $prepared_item - */ - protected function prepare_item_for_database($request){ - return array(); - } - - /** - * Prepare the item for the REST response - * - * @param mixed $item WordPress representation of the item. - * @param \WP_REST_Request $request Request object. - * @return mixed - */ - public function prepare_item_for_response($item,$request){ - return array(); - } - -} - -new SSC_Rest_Order(); diff --git a/includes/class.ssc_settings.php b/includes/class.ssc_settings.php deleted file mode 100644 index f2a5afb..0000000 --- a/includes/class.ssc_settings.php +++ /dev/null @@ -1,320 +0,0 @@ -title = __('Nastavení','ssc'); - } - - /** - * Returns the running object - * - * @return self - */ - public static function get_instance(){ - if(null === self::$instance){ - self::$instance = new self(); - self::$instance->hooks(); - } - - return self::$instance; - } - - /** - * Initiate our hooks - * @since 0.1.0 - */ - public function hooks(){ - add_action('admin_init',array($this,'init')); - add_action('admin_menu',array($this,'add_options_page')); - add_action('cmb2_admin_init',array($this,'add_options_page_metabox')); - } - - /** - * Register our setting to WP - * @since 0.1.0 - */ - public function init(){ - register_setting($this->key,$this->key); - } - - /** - * Add menu options page - * @since 0.1.0 - */ - public function add_options_page(){ - - $this->options_page = add_submenu_page('simple_shop_settings',$this->title,$this->title,'manage_options','admin.php?page='.$this->key,array($this,'admin_page_display')); - - $this->options_page = add_menu_page($this->title,$this->title,'manage_options',$this->key,array($this,'admin_page_display')); - remove_menu_page($this->key); - // Include CMB CSS in the head to avoid FOUC - add_action("admin_print_styles-{$this->options_page}",array('CMB2_hookup','enqueue_cmb_css')); - } - - /** - * Admin page markup. Mostly handled by CMB2 - * @since 0.1.0 - */ - public function admin_page_display(){ - ?> -
-

- metabox_id,$this->key);?> -
- metabox_id}",array($this,'settings_notices'),10,2); - - $cmb = new_cmb2_box(array( - 'id' => $this->metabox_id, - 'hookup' => false, - 'cmb_styles' => true, - 'show_on' => array( - // These are important, don't remove - 'key' => 'options-page', - 'value' => array($this->key,) - ), - )); - - # - # MAIL - # - $cmb->add_field(array( - 'name' => 'Nastavení e-mailu, který se posílá novým členům:', -// 'desc' => 'This is a title description', - 'classes_cb' => array($this,'is_valid_api_keys'), - 'type' => 'title', - 'id' => 'ssc_email_title' - )); - $cmb->add_field(array( - 'name' => 'Poslat e-mail novému členovi?', -// 'desc' => 'Select an option', - 'id' => 'ssc_email_enable', - 'type' => 'select', - 'show_option_none' => false, - 'classes_cb' => array($this,'is_valid_api_keys'), - 'default' => '1', - 'options' => array( - '1' => __('Ano, poslat každému novému členovi e-mail.','cmb2'), - '2' => __('Ne, zakázat posílání e-mailu novým členům.','cmb2'), - ), - )); - $cmb->add_field(array( - 'name' => __('Předmět e-mailu','ssc'), -// 'desc' => __('Najdete ho ve svém SimpleShop účtu v Nastavení -> WP Plugin','ssc'), - 'id' => 'ssc_email_subject', - 'classes_cb' => array($this,'is_valid_api_keys'), - 'type' => 'text', - 'default' => 'Byl Vám udělen přístup do členské sekce', - )); - - - $cmb->add_field(array( - 'name' => __('Text emailu','ssc'), - 'desc' => __('Povolené zástupné znaky:
' - .'
{login} = login
' - .'{password} = heslo
' - .'{login_url} = adresa, na které je možné se přihlásit
' - .'{pages} = seznam stránek, do kterých má uživatel zakoupený přístup
' - .'{mail} = e-mail uživatele (většinou stejný jako login)
' - .'
' - .'','ssc'), - 'id' => 'ssc_email_text', - 'type' => 'wysiwyg', - 'classes_cb' => array($this,'is_valid_api_keys'), - 'default' => 'Dobrý den, -byl udělen přístup do členské sekce. - -Login: {login} -Heslo: {password} - -Přihlásit se můžete na: '.wp_login_url().' - -Váš zakoupený obsah: -{pages} - -S pozdravem a přáním pěkného dne, -SimpleShop.cz - S námi zvládne prodávat každý' - )); - - - # - # API - # - $cmb->add_field(array( - 'name' => 'Nastavení API - propojení s aplikací SimpleShop:', -// 'desc' => 'This is a title description', -// 'show_on_cb' => array($this,'is_valid_api_keys'), - 'type' => 'title', - 'id' => 'ssc_api_title' - )); - - // Set our CMB2 fields - $cmb->add_field(array( - 'name' => __('Přihlašovací email','ssc'), - 'desc' => __('Zadejte email, který používáte pro přihlášení do služby SimpleShop','ssc'), - 'id' => 'ssc_api_email', - 'type' => 'text', - )); - - $cmb->add_field(array( - 'name' => __('SimpleShop API Klíč','ssc'), - 'desc' => __('Najdete ho ve svém SimpleShop účtu v Nastavení -> WP Plugin','ssc'), - 'id' => 'ssc_api_key', - 'type' => 'text', - )); - } - - /** - * Register settings notices for display - * - * @since 0.1.0 - * @param int $object_id Option key - * @param array $updated Array of updated fields - * @return void - */ - public function settings_notices($object_id,$updated){ - $ssc = new \SSC\SSC(); - $vyfakturuj_api = new \SSC\Vyfakturuj\VyfakturujAPI($ssc->email,$ssc->secure_key); - $result = $vyfakturuj_api->initWPPlugin(site_url()); - if(isset($result['status']) && $result['status'] == 'success'){ - update_option('ssc_valid_api_keys',1); - }else{ - update_option('ssc_valid_api_keys',0); - } - - if($object_id !== $this->key || empty($updated)){ - return; - } - - add_settings_error($this->key.'-notices','',__('Settings updated.','ssc'),'updated'); - settings_errors($this->key.'-notices'); - } - - /** - * Check, if we already got valid API key, if not, add 'hidden' class to the settings that are not needed in the first step - * We switched to this approach, because by just hiding the fields the default values are saved even on the first save, - * but previously the fields were removed completely from the form, so the default values were not saved until the API keys were in place - * @return array - */ - function is_valid_api_keys(){ - // If the keys are valid, do nothing - if (get_option('ssc_valid_api_keys') == 1) - return array(); - - return array( 'hidden' ); - - } - - /** - * Public getter method for retrieving protected/private variables - * @since 0.1.0 - * - * @param string $field Field to retrieve - * - * @return mixed Field value or exception is thrown - * @throws Exception - */ - public function __get($field){ - // Allowed fields to retrieve - if(in_array($field,array('key','metabox_id','title','options_page'),true)){ - return $this->{$field}; - } - - throw new Exception('Invalid property: '.$field); - } - -} - -/** - * Helper function to get/return the Myprefix_Admin object - * @since 0.1.0 - * @return SSC_Settings object - */ -function ssc_admin(){ - return SSC_Settings::get_instance(); -} - -/** - * Wrapper function around cmb2_get_option - * @since 0.1.0 - * @param string $key Options array key - * @param mixed $default Optional default value - * @return mixed Option value - */ -function ssc_get_option($key = '',$default = null){ - if(function_exists('cmb2_get_option')){ - // Use cmb2_get_option as it passes through some key filters. - return cmb2_get_option(ssc_admin()->key,$key,$default); - } - - // Fallback to get_option if CMB2 is not loaded yet. - $opts = get_option(ssc_admin()->key,$key,$default); - - $val = $default; - - if('all' == $key){ - $val = $opts; - }elseif(is_array($opts) && array_key_exists($key,$opts) && false !== $opts[$key]){ - $val = $opts[$key]; - } - - return $val; -} - -// Get it started -ssc_admin(); diff --git a/includes/class.ssc_shortcodes.php b/includes/class.ssc_shortcodes.php deleted file mode 100644 index c2333a9..0000000 --- a/includes/class.ssc_shortcodes.php +++ /dev/null @@ -1,102 +0,0 @@ -'; - } - - function simple_shop_content($atts, $content = ""){ - $atts = shortcode_atts( array( - 'group_id' => '', - 'is_member' => '', - 'days_to_view' => '', - 'specific_date_from' => '', - 'specific_date_to' => '', - - ), $atts, 'SimpleShop-content' ); - - $group_id = $atts['group_id']; - $is_member = $atts['is_member']; - $specific_date_from = $atts['specific_date_from']; - $specific_date_to = $atts['specific_date_to']; - $days_to_view = $atts['days_to_view']; - - if (!empty($specific_date_from)) { - // Check against the from date, this has nothing to do with groups or other settings - if (date('Y-m-d') < $specific_date_from) - return ''; - } - - if (!empty($specific_date_to)) { - // Check against the to date, this has nothing to do with groups or other settings - if (date('Y-m-d') > $specific_date_to) - return ''; - } - - // Stop if there's no group_id or is_member, and no specific date is set - if ( empty($group_id) || ( empty( $is_member ) && empty( $specific_date_from ) && empty( $specific_date_to ) ) ) - return ''; - - $group = new SSC_Group($group_id); - - if ($is_member == 'yes') { - // Check, if the user is logged in and is member of the group, if not, bail - if (!is_user_logged_in() || !$group->user_is_member_of_group(get_current_user_id())) - return ''; - } else if ($is_member == 'no') { - // Check, if the user is NOT a member of specific group. This includes non-logged-in users - if (is_user_logged_in() && $group->user_is_member_of_group(get_current_user_id())) - return ''; - } else { - // If the is_member isn't 'yes' or 'no', the parameter is wrong, so stop here - return ''; - } - - // Group check done, check if there are some days set and if is_member is yes - // it doesn't make sense to check days condition for users who should NOT be members of a group - if (!empty($days_to_view) && $is_member == 'yes') { - $membership = new SSC_Membership(get_current_user_id()); - $subscription_date = $membership->groups[$group_id]['subscription_date']; - // Compare against today's date - if (date('Y-m-d') < date('Y-m-d',strtotime("$subscription_date + $days_to_view days"))) { - return ''; - } - } - - // Support shortcodes inside shortcodes - - // Fix for MioWEB - $hook = ssc_remove_anonymous_object_filter('the_content','visualEditorPage','create_content'); - - $content = apply_filters('the_content',$content); - - // Add the filter back if needed - if ($hook) - add_filter($hook[0],$hook[1],$hook[2]); - - return $content; - - } - - - -} - -new SSC_Shortcodes(); - - diff --git a/includes/ssc_helpers.php b/includes/ssc_helpers.php deleted file mode 100644 index 9fc8479..0000000 --- a/includes/ssc_helpers.php +++ /dev/null @@ -1,98 +0,0 @@ -callbacks ) ) { - array_walk( $wp_filter[$hook]->callbacks, function( $callbacks, $priority ) use ( &$hooks ) { - foreach ( $callbacks as $id => $callback ) - $hooks[] = array_merge( array( 'id' => $id, 'priority' => $priority ), $callback ); - }); - } else { - return array(); - } - - foreach( $hooks as &$item ) { - // skip if callback does not exist - if ( !is_callable( $item['function'] ) ) continue; - - // function name as string or static class method eg. 'Foo::Bar' - if ( is_string( $item['function'] ) ) { - $ref = strpos( $item['function'], '::' ) ? new \ReflectionClass( strstr( $item['function'], '::', true ) ) : new \ReflectionFunction( $item['function'] ); - $item['file'] = $ref->getFileName(); - $item['line'] = get_class( $ref ) == 'ReflectionFunction' - ? $ref->getStartLine() - : $ref->getMethod( substr( $item['function'], strpos( $item['function'], '::' ) + 2 ) )->getStartLine(); - - // array( object, method ), array( string object, method ), array( string object, string 'parent::method' ) - } elseif ( is_array( $item['function'] ) ) { - - $ref = new \ReflectionClass( $item['function'][0] ); - - // $item['function'][0] is a reference to existing object - $item['function'] = array( - is_object( $item['function'][0] ) ? get_class( $item['function'][0] ) : $item['function'][0], - $item['function'][1] - ); - $item['file'] = $ref->getFileName(); - $item['line'] = strpos( $item['function'][1], '::' ) - ? $ref->getParentClass()->getMethod( substr( $item['function'][1], strpos( $item['function'][1], '::' ) + 2 ) )->getStartLine() - : $ref->getMethod( $item['function'][1] )->getStartLine(); - - // closures - } elseif ( is_callable( $item['function'] ) ) { - $ref = new \ReflectionFunction( $item['function'] ); - $item['function'] = get_class( $item['function'] ); - $item['file'] = $ref->getFileName(); - $item['line'] = $ref->getStartLine(); - - } - } - - return $hooks; -} - -/** - * An utility function to remove any hook from a class - * @param $tag - * @param $class - * @param $method - * @return array|bool|null - */ -function ssc_remove_anonymous_object_filter( $tag, $class, $method ) -{ - if (!isset($GLOBALS['wp_filter'][ $tag ]) || empty($GLOBALS['wp_filter'][ $tag ])) { - return null; - } - - foreach ( $GLOBALS['wp_filter'][ $tag ] as $priority => $filter ) - { - foreach ( $filter as $identifier => $function ) - { - if ( is_array( $function) - and is_a( $function['function'][0], $class ) - and $method === $function['function'][1] - ) - { - - remove_filter( - $tag, - array ( $function['function'][0], $method ), - $priority - ); - return array($tag,array ( $function['function'][0], $method ),$priority); - } - } - } - - return false; -} - diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..35e1798 --- /dev/null +++ b/readme.txt @@ -0,0 +1,19 @@ +=== SimpleShop.cz === +Contributors: redbitcz, simpleshopcz, jakubboucek +Tags: simpleshop, simple eshop, selling form +Requires at least: 4.0.0 +Tested up to: 4.9.8 +Requires PHP: 5.3.2 +Stable tag: trunk +License: MIT +License URI: https://github.com/redbitcz/simpleshop-wp-plugin/blob/master/LICENSE + +Plugin pro propojení Wordpress a SimpleShop.cz + +== Screenshots == +1. Rozšířený editor +2. Vložení formuláře v administraci +3. Zobrazený formulář ve stránce + +== Changelog == +See https://github.com/redbitcz/simpleshop-wp-plugin/releases \ No newline at end of file diff --git a/simple-shop-connector.php b/simple-shop-connector.php deleted file mode 100644 index 0a4aec2..0000000 --- a/simple-shop-connector.php +++ /dev/null @@ -1,126 +0,0 @@ -require_classes(); - $this->secure_key = $this->get_secure_key(); - $this->email = $this->get_email(); - add_action('tgmpa_register',array($this,'register_required_plugins')); - } - - function require_classes(){ - require_once __DIR__ . '/vendor/autoload.php'; - require_once __DIR__. '/includes/ssc_helpers.php'; - require_once __DIR__. '/includes/class.ssc_settings.php'; - require_once __DIR__. '/includes/class.ssc_admin.php'; - require_once __DIR__. '/includes/class.ssc_groups.php'; - require_once __DIR__. '/includes/class.ssc_membership.php'; - require_once __DIR__. '/includes/class.ssc_rest.php'; - require_once __DIR__. '/includes/class.ssc_cron.php'; - require_once __DIR__. '/includes/class.ssc_metaboxes.php'; - require_once __DIR__. '/includes/class.ssc_access.php'; - require_once __DIR__. '/includes/class.ssc_shortcodes.php'; - } - - function generate_secure_key(){ - return bin2hex(random_bytes(22)); - } - - function save_secure_key($key){ - update_option('ssc_secure_key',$key); - } - - function get_secure_key(){ - return ssc_get_option('ssc_api_key'); - } - - function get_email(){ - return ssc_get_option('ssc_api_email'); - } - - function validate_secure_key($key_to_validate){ - return $key_to_validate == sha1($this->secure_key); - } - - /** - * Register the required plugins for this plugin. - */ - function register_required_plugins(){ - global $wp_version; - if($wp_version < '4.7'){ - $plugins = array( - array( - 'name' => 'Wordpress Rest API', - 'slug' => 'rest-api', - 'required' => true, - ) - ); - - $config = array( - 'id' => 'ssc', - 'default_path' => '', - 'menu' => 'ssc-install-plugins', - 'parent_slug' => 'tools.php', - 'capability' => 'edit_theme_options', - 'has_notices' => true, - 'dismissable' => true, - 'dismiss_msg' => '', - 'is_automatic' => false, - 'message' => '', - ); - - tgmpa($plugins,$config); - } - } - - function get_post_types(){ - $args = array( - 'public' => true - ); - - return get_post_types($args); - } - -} - -new SSC; - -/** - * Activation hook - */ -register_activation_hook(__FILE__,'SSC\ssc_activation_hook'); - -function ssc_activation_hook(){ - if(!function_exists('curl_init') || !function_exists('random_bytes')){ - echo '

'.__('Aktivace se nezdařila. Kontaktuje prosím poskytovatele Vašeho hostingu a požádejte o instalaci rozšíření PHP - CURL a MCRYPT.','ssc').'

'; - - //Adding @ before will prevent XDebug output - @trigger_error(__('Aktivace se nezdařila. Kontaktuje prosím poskytovatele Vašeho hostingu a požádejte o instalaci rozšíření PHP - CURL a MCRYPT.','ssc'),E_USER_ERROR); - } - - - // Generate and save the secure key - $ssc = new SSC(); - $key = $ssc->generate_secure_key(); - $ssc->save_secure_key($key); -} \ No newline at end of file diff --git a/simpleshop-cz.php b/simpleshop-cz.php new file mode 100644 index 0000000..7f97d97 --- /dev/null +++ b/simpleshop-cz.php @@ -0,0 +1,30 @@ + + */ + +/* + Plugin Name: SimpleShop.cz + Plugin URI: https://www.simpleshop.cz + Description: Plugin pro propojení Wordpress a SimpleShop.cz + Author: Redbit s.r.o. + Author URI: https://www.redbit.cz + Version: dev-master + */ + +namespace Redbit\SimpleShop\WpPlugin; + +require_once __DIR__ . '/vendor/autoload.php'; + +define( 'SIMPLESHOP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); +define( 'SIMPLESHOP_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); +define( 'SIMPLESHOP_PLUGIN_VERSION', 'dev-master' ); +define( 'SIMPLESHOP_PREFIX', '_ssc_' ); + +/** + * Start plugin + */ +new Loader(); diff --git a/src/Access.php b/src/Access.php new file mode 100644 index 0000000..122e1e9 --- /dev/null +++ b/src/Access.php @@ -0,0 +1,269 @@ + + */ + +namespace Redbit\SimpleShop\WpPlugin; + +/** + * Handles the access for the posts / pages + */ +class Access { + + public function __construct() { + add_action( 'template_redirect', array( $this, 'check_access' ) ); + add_filter( 'wp_setup_nav_menu_item', array( $this, 'setup_nav_menu_item' ) ); + add_action( 'wp_head', array( $this, 'hide_menu_items' ) ); + add_action( 'init', array( $this, 'mioweb_remove_login_redirect' ) ); + } + + /** + * Remove the MioWeb filter that redirects the user to homepage + */ + public function mioweb_remove_login_redirect() { + Helpers::ssc_remove_anonymous_object_filter( 'login_redirect', 'visualEditorPage', 'login_redirect' ); + } + + /** + * Check if the page is protected and the user has access to the page + */ + public function check_access() { + $post_groups = $this->get_post_groups(); + + // If the post is protected and user is not logged in, redirect him to login + if ( $post_groups && ! is_user_logged_in() ) { + wp_safe_redirect( wp_login_url( $_SERVER['REQUEST_URI'] ) ); + } + + // Check if current user has access to the post, if not, redirect him to defined URL or home if the URL is not set + if ( $post_groups && ! $this->user_can_view_post() && ! is_home() && ! is_front_page() ) { + $no_access_url = $this->get_no_access_redirect_url(); + + $url = $no_access_url ?: site_url(); + wp_redirect( $url ); + exit(); + } + } + + /** + * Check if user has permission to view the post + * + * @param string $post_id + * @param string $user_id + * + * @return bool|\WP_Error + */ + public function user_can_view_post( $post_id = '', $user_id = '' ) { + // Admins can view all posts + + if ( $this->user_is_admin() ) { + return true; + } + + global $post; + if ( ! $post_id ) { + $post_id = $post->ID; + } + + if ( ! $user_id ) { + $user_id = get_current_user_id(); + } + + + if ( ! ( $post_id > 0 ) || ! ( $user_id >= 0 ) ) { + return new \WP_Error( '400', 'Wrong post ID or user ID' ); + } + + $post_groups = $this->get_post_groups( $post_id ); + + + if ( ! $post_groups || $post_groups == '' ) { + return true; + } + + + foreach ( $post_groups as $post_group ) { + $group = new Group( $post_group ); + if ( $group->user_is_member_of_group( $user_id ) ) { + // Ok, the user is member of at least one group that has access to this post + + // Check, if the post has set date, after which it can be accessed + if ( $date_to_access = $this->get_post_date_to_access() ) { + if ( date( 'Y-m-d' ) < $date_to_access ) { + // The post should not be accessed yet, not depending on group, so just return false + return false; + } + } + + // Check, if the post has set date, until which it can be accessed + if ( $date_to_access = $this->get_post_date_until_to_access() ) { + if ( date( 'Y-m-d' ) > $date_to_access ) { + // The post should not be accessed yet, not depending on group, so just return false + return false; + } + } + + // The user is member of some group, check if the post has minimum days to access set + $membership = new Membership( $user_id ); + + // Check if the subscription is valid + if ( isset( $membership->groups[ $post_group ]['valid_to'] ) && $membership->groups[ $post_group ]['valid_to'] ) { + if ( $membership->groups[ $post_group ]['valid_to'] < date( 'Y-m-d' ) ) { + // if the the subscription expired, just break the loop here, as the user might have multiple subscriptions + continue; + } + } + + if ( $days_to_access = $this->get_post_days_to_access() ) { + $subscription_date = $membership->groups[ $post_group ]['subscription_date']; + // Get the date of subscription to the group + if ( $subscription_date > date( 'Y-m-d', strtotime( "now -$days_to_access days" ) ) ) { + // if the user does not have access YET, just break the loop here, as the user might have multiple subscriptions + continue; + } + } + + return true; + } + } + + return false; + } + + /** + * Check if the user is admin + * This is filterable, + * @return mixed + */ + public function user_is_admin() { + $is_admin = current_user_can( 'administrator' ) ? true : false; + + return apply_filters( 'ssc_user_is_admin', $is_admin ); + } + + /** + * Get the URL to redirect the user if he has no access + * + * @param string $post_id + * + * @return mixed + */ + public function get_no_access_redirect_url( $post_id = '' ) { + global $post; + + if ( ! $post_id ) { + $post_id = $post->ID; + } + + // First check, if we should redirect the user to login form +// if ($redirect_post_id = get_post_meta($post_id, SSC_PREFIX . 'no_access_redirect_to_login_form', true)) { +// $actual_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; +// return wp_login_url($actual_url); +// } + + // Next try to get the ID and return permalink + if ( $redirect_post_id = get_post_meta( $post_id, '_ssc_no_access_redirect_post_id', true ) ) { + return get_the_permalink( $redirect_post_id ); + } + + return get_post_meta( $post_id, '_ssc_no_access_redirect', true ); + } + + /** + * Setup the cart in menu + * + * @param $item + * + * @return mixed + */ + public function setup_nav_menu_item( $item ) { + + if ( ! $this->user_can_view_post( $item->object_id ) ) { + $item->classes[] = 'ssc-hide'; + $item->title = ''; + $item->url = ''; + + return $item; + } + + return $item; + } + + public function get_post_groups( $post_id = '' ) { + global $post; + + if ( ! $post_id ) { + $post_id = $post->ID; + } + + return get_post_meta( $post_id, '_ssc_groups', true ); + } + + /** + * Get the number of days the user has to be subscribed to have access to the post + * + * @param string $post_id + * + * @return mixed + */ + public function get_post_days_to_access( $post_id = '' ) { + global $post; + + if ( ! $post_id ) { + $post_id = $post->ID; + } + + return get_post_meta( $post_id, SIMPLESHOP_PREFIX . 'days_to_access', true ); + } + + /** + * Get the date to access the post + * + * @param string $post_id + * + * @return mixed + */ + public function get_post_date_to_access( $post_id = '' ) { + global $post; + + if ( ! $post_id ) { + $post_id = $post->ID; + } + + return get_post_meta( $post_id, SIMPLESHOP_PREFIX . 'date_to_access', true ); + } + + /** + * Get the date until the access to the post is allowed + * + * @param string $post_id + * + * @return mixed + */ + public function get_post_date_until_to_access( $post_id = '' ) { + global $post; + + if ( ! $post_id ) { + $post_id = $post->ID; + } + + return get_post_meta( $post_id, SIMPLESHOP_PREFIX . 'date_until_to_access', true ); + } + + + /** + * Hide items in menu + */ + public function hide_menu_items() { + ?> + + + */ + +namespace Redbit\SimpleShop\WpPlugin; + +use Redbit\SimpleShop\WpPlugin\Vyfakturuj\VyfakturujAPI; + +class Admin { + + /** + * @var Loader + */ + private $loader; + + public function __construct(Loader $loader) { + $this->loader = $loader; + + add_action( 'admin_menu', array( $this, 'add_settings_page' ) ); + add_filter( 'manage_edit-ssc_group_columns', array( $this, 'ssc_group_columns' ) ); + add_action( 'manage_ssc_group_posts_custom_column', array( $this, 'ssc_group_column_content' ), 10, 2 ); + add_action( 'init', array( $this, 'register_groups_cpt' ) ); + add_action( 'init', array( $this, 'tiny_mce_new_buttons' ) ); + add_filter( 'page_row_actions', array( $this, 'remove_quick_edit' ), 10, 2 ); + add_action( 'wp_head', array( $this, 'publishing_actions' ) ); + add_action( 'admin_head', array( $this, 'publishing_actions' ) ); + add_action( 'wp_ajax_load_simple_shop_products', array( $this, 'wp_ajax_load_simple_shop_products' ) ); + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); + } + + /** + * Get products from simple shop via API + */ + public function wp_ajax_load_simple_shop_products() { + $values = array(); + if ( $this->loader->has_credentials() ) { + $vyfakturuj_api = new VyfakturujAPI( $this->loader->get_api_email(), $this->loader->get_api_key() ); + $ret = $vyfakturuj_api->getProducts(); + + if ( $ret ) { + foreach ( $ret as $product ) { + $values[ $product['code'] ] = $product['name']; + } + } + } + echo json_encode( $values ); + exit(); + } + + /** + * Remove quick edit from groups + * + * @param $actions + * @param $post + * + * @return mixed + */ + public function remove_quick_edit( $actions, $post ) { + if ( $post->post_type == 'ssc_group' ) { + + unset( $actions['inline hide-if-no-js'] ); + } + + return $actions; + } + + /** + * Hide publishing actions in group detail + */ + public function publishing_actions() { + $mg_post_type = 'ssc_group'; + global $post; + if ( $post->post_type == $mg_post_type ) { + echo ''; + } ?> + + + + + __( 'Členské sekce', 'ssc' ), + 'singular_name' => __( 'Skupina', 'ssc' ), + 'menu_name' => __( 'Členské sekce', 'ssc' ), + 'name_admin_bar' => __( 'Členské sekce', 'ssc' ), + 'add_new' => __( 'Přidat skupinu', 'ssc' ), + 'add_new_item' => __( 'Přidat novou skupinu', 'ssc' ), + 'new_item' => __( 'Nová skupina', 'ssc' ), + 'edit_item' => __( 'Upravit skupinu', 'ssc' ), + 'view_item' => __( 'Zobrazit skupinu', 'ssc' ), + 'all_items' => __( 'Členské sekce', 'ssc' ), + 'search_items' => __( 'Hledat skupiny', 'ssc' ), + 'parent_item_colon' => __( 'Nadřazená skupina:', 'ssc' ), + 'not_found' => __( 'Nebyly nalezeny žádné skupiny.', 'ssc' ), + 'not_found_in_trash' => __( 'Žádné skupiny v koši', 'ssc' ) + ); + + $args = array( + 'labels' => $labels, + 'public' => false, + 'publicly_queryable' => false, + 'show_ui' => true, + 'show_in_menu' => 'simple_shop_settings', + 'query_var' => true, + 'capability_type' => 'post', + 'has_archive' => false, + 'hierarchical' => true, + 'menu_position' => null, + 'supports' => array( 'title' ) + ); + + register_post_type( 'ssc_group', $args ); + } + + /** + * Register a custom menu page. + */ + public function add_settings_page() { + add_menu_page( + __( 'SimpleShop', 'ssc' ), __( 'SimpleShop', 'ssc' ), 'manage_options', 'simple_shop_settings', + array( $this, 'render_settings_page' ), SIMPLESHOP_PLUGIN_URL . '/img/white_logo.png', 99 + ); + } + + /** + * Add custom columns to admin groups listing + * + * @param $columns + * + * @return mixed + */ + public function ssc_group_columns( $columns ) { + $columns['ssc_id'] = 'SSC ID'; + + return $columns; + } + + /** + * Add content to custom columns in groups listing + * + * @param $column + * @param $post_id + */ + public function ssc_group_column_content( $column, $post_id ) { + global $post; + + switch ( $column ) { + case 'ssc_id' : + echo $post->ID; + break; + } + } + + /** + * Enqueue admin scripts + */ + public function enqueue_admin_scripts() { + wp_enqueue_style( 'ssc', SIMPLESHOP_PLUGIN_URL . 'css/ssc.css' ); + wp_register_style( 'jquery-ui', 'http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css' ); + wp_enqueue_style( 'jquery-ui' ); + } +} diff --git a/src/Cron.php b/src/Cron.php new file mode 100644 index 0000000..15c23c0 --- /dev/null +++ b/src/Cron.php @@ -0,0 +1,122 @@ + + */ + +namespace Redbit\SimpleShop\WpPlugin; + +class Cron { + /** + * @var Loader + */ + private $loader; + + public function __construct(Loader $loader) { + $this->loader = $loader; + + if ( ! wp_next_scheduled( 'ssc_send_user_has_access_to_post_notification' ) ) { + wp_schedule_event( time(), 'daily', 'ssc_send_user_has_access_to_post_notification' ); + } + + add_action( 'ssc_send_user_has_access_to_post_notification', + array( $this, 'send_user_has_access_to_post_notification' ) ); + } + + public function send_user_has_access_to_post_notification() { + // Get posts, that have set either days to view or specific date + $args = array( + 'post_type' => 'any', + 'posts_per_page' => - 1, + 'meta_query' => array( + 'relation' => 'OR', + SIMPLESHOP_PREFIX . 'days_to_access' => array( + 'key' => SIMPLESHOP_PREFIX . 'days_to_access', + 'compare' => 'EXISTS' + ), + SIMPLESHOP_PREFIX . 'date_to_access' => array( + 'key' => SIMPLESHOP_PREFIX . 'date_to_access', + 'compare' => 'EXISTS' + ) + ) + ); + + $the_query = new \WP_Query( $args ); + + + if ( $the_query->have_posts() ) { + + // Get all users + $users = get_users(); + + // Get all groups to array + $users_groups = array(); + foreach ( $users as $user ) { + $membership = new Membership( $user->ID ); + $users_groups[ $user->ID ] = $membership->groups; + } + + while ( $the_query->have_posts() ) { + $the_query->the_post(); + global $post; + + // Check if the post has some email set, if not, continue + $email_text = get_post_meta( $post->ID, SIMPLESHOP_PREFIX . 'email_user_can_access', true ); + if ( ! $email_text ) { + continue; + } + + $email_subject = get_post_meta( $post->ID, SIMPLESHOP_PREFIX . 'email_subject_user_can_access', true ); + + $access = $this->loader->get_access(); + // Get post groups + $groups = $access->get_post_groups(); + // Get days to access + $days_to_access = $access->get_post_days_to_access(); + // Get date to access + $date_to_access = $access->get_post_date_to_access(); + + // TODO: Rewrite this to first find the groups that have access to the post, than find users for these groups. + // That way we won't have to scrub through all the users all the time + // Scrub through the groups and check, if the user is member of the group + foreach ( $groups as $group ) { + foreach ( $users_groups as $user_id => $user_groups ) { + $send_email = false; + + // Check, if the user is member of this group + if ( array_key_exists( $group, $user_groups ) ) { + // If so, finally check, if we should send the email + + // First check, if today is the date when the post can be accessed + if ( $date_to_access == date( 'Y-m-d' ) ) { + // Cool, send email + $send_email = true; + } elseif ( $days_to_access ) { + $subscribed = $user_groups[ $group ]['subscription_date']; + $date_to_compare = date( 'Y-m-d', strtotime( "$subscribed -$days_to_access days" ) ); + + if ( date( 'Y-m-d' ) == $date_to_compare ) { + $send_email = true; + } + } + } + + if ( $send_email ) { + // Woohoo, send the email + $userdata = get_userdata( $user_id ); + if ( ! get_user_meta( $user_id, SIMPLESHOP_PREFIX . 'notification_email_sent_' . $post->ID, + true ) ) { + wp_mail( $userdata->user_email, $email_subject, $email_text ); + update_user_meta( $user_id, SIMPLESHOP_PREFIX . 'notification_email_sent_' . $post->ID, 1 ); + } + } + } + } + } + } + + wp_reset_postdata(); + } +} diff --git a/src/Group.php b/src/Group.php new file mode 100644 index 0000000..d102087 --- /dev/null +++ b/src/Group.php @@ -0,0 +1,126 @@ + + */ + +namespace Redbit\SimpleShop\WpPlugin; + +class Group { + + public $id = ''; + public $name = ''; + + public function __construct( $id = '' ) { + if ( $id ) { + $this->id = $id; + $this->get_group(); + } + } + + /** + * Get all groups (custom post type) + * @return array + */ + public function get_groups() { + + $args = array( + 'post_type' => 'ssc_group', + 'posts_per_page' => - 1, + 'post_status' => 'publish' + ); + + $groups = array(); + + $posts = get_posts( $args ); + + foreach ( $posts as $item ) { + $groups[ $item->ID ] = $item->post_title; + } + + return $groups; + } + + /** + * Get a single group + * @return bool + */ + public function get_group() { + $group = get_post( $this->id ); + if ( $group ) { + // Set the group details + + $this->name = $group->post_title; + } + + return (bool) $group; + } + + /** + * Check if group exists + * @return array|null|\WP_Post + */ + public function group_exists() { + return get_post( $this->id ); + } + + /** + * Get groups the user belongs to + * + * @param string $user_id + * + * @return mixed + */ + public function get_user_groups( $user_id = '' ) { + if ( ! $user_id ) { + $user_id = get_current_user_id(); + } + + return get_user_meta( $user_id, '_ssc_user_groups', true ); + } + + /** + * Add user to a group + * + * @param $user_id + */ + public function add_user_to_group( $user_id ) { + $groups = $this->get_user_groups( $user_id ); + + if ( ! $groups ) { + $groups = array(); + } + + if ( ! in_array( $this->id, $groups ) ) { + $groups[] = $this->id; + update_user_meta( $user_id, '_ssc_user_groups', $groups ); + + // Set the date of user registration to the group + $membership = new Membership( $user_id ); + $membership->set_subscription_date( $this->id ); + } + } + + /** + * Check if user is a member of a group + * + * @param $user_id + * + * @return bool + */ + public function user_is_member_of_group( $user_id ) { + if ( ! $user_id ) { + $user_id = get_current_user_id(); + } + + $groups = $this->get_user_groups( $user_id ); + + if ( ! is_array( $groups ) ) { + return false; + } + + return in_array( $this->id, $groups ) ? true : false; + } +} diff --git a/src/Helpers.php b/src/Helpers.php new file mode 100644 index 0000000..097fbf6 --- /dev/null +++ b/src/Helpers.php @@ -0,0 +1,114 @@ + + */ + +namespace Redbit\SimpleShop\WpPlugin; + +class Helpers { + + /** + * Utiity function to list hooks that are connected to actions / filters + * Great for debugging the actions and filters + * + * @param string $hook + * + * @return array + * @throws \ReflectionException + */ + public static function ssc_list_hooks( $hook = '' ) { + global $wp_filter; + + if ( isset( $wp_filter[ $hook ]->callbacks ) ) { + array_walk( $wp_filter[ $hook ]->callbacks, function ( $callbacks, $priority ) use ( &$hooks ) { + foreach ( $callbacks as $id => $callback ) { + $hooks[] = array_merge( array( 'id' => $id, 'priority' => $priority ), $callback ); + } + } ); + } else { + return array(); + } + + foreach ( $hooks as &$item ) { + // skip if callback does not exist + if ( ! is_callable( $item['function'] ) ) { + continue; + } + + // function name as string or static class method eg. 'Foo::Bar' + if ( is_string( $item['function'] ) ) { + $ref = strpos( $item['function'], '::' ) ? new \ReflectionClass( strstr( $item['function'], + '::', true ) ) : new \ReflectionFunction( $item['function'] ); + $item['file'] = $ref->getFileName(); + $item['line'] = $ref instanceof \ReflectionFunction + ? $ref->getStartLine() + : $ref->getMethod( substr( $item['function'], + strpos( $item['function'], '::' ) + 2 ) )->getStartLine(); + + // array( object, method ), array( string object, method ), array( string object, string 'parent::method' ) + } elseif ( is_array( $item['function'] ) ) { + + $ref = new \ReflectionClass( $item['function'][0] ); + + // $item['function'][0] is a reference to existing object + $item['function'] = array( + is_object( $item['function'][0] ) ? get_class( $item['function'][0] ) : $item['function'][0], + $item['function'][1] + ); + $item['file'] = $ref->getFileName(); + $item['line'] = strpos( $item['function'][1], '::' ) + ? $ref->getParentClass()->getMethod( substr( $item['function'][1], + strpos( $item['function'][1], '::' ) + 2 ) )->getStartLine() + : $ref->getMethod( $item['function'][1] )->getStartLine(); + + // closures + } elseif ( is_callable( $item['function'] ) ) { + $ref = new \ReflectionFunction( $item['function'] ); + $item['function'] = get_class( $item['function'] ); + $item['file'] = $ref->getFileName(); + $item['line'] = $ref->getStartLine(); + + } + } + + return $hooks; + } + + /** + * An utility function to remove any hook from a class + * + * @param $tag + * @param $class + * @param $method + * + * @return array|bool|null + */ + public static function ssc_remove_anonymous_object_filter( $tag, $class, $method ) { + if ( ! isset( $GLOBALS['wp_filter'][ $tag ] ) || empty( $GLOBALS['wp_filter'][ $tag ] ) ) { + return null; + } + + foreach ( $GLOBALS['wp_filter'][ $tag ] as $priority => $filter ) { + foreach ( $filter as $identifier => $function ) { + if ( is_array( $function ) + && is_a( $function['function'][0], $class ) + && $method === $function['function'][1] + ) { + + remove_filter( + $tag, + array( $function['function'][0], $method ), + $priority + ); + + return array( $tag, array( $function['function'][0], $method ), $priority ); + } + } + } + + return false; + } +} diff --git a/src/Loader.php b/src/Loader.php new file mode 100644 index 0000000..80ec76e --- /dev/null +++ b/src/Loader.php @@ -0,0 +1,146 @@ + + */ + +namespace Redbit\SimpleShop\WpPlugin; + +class Loader { + /** + * @var string + */ + private $secure_key; + /** + * @var string + */ + private $email; + + /** + * @var Settings + */ + private $settings; + + /** + * @var Access + */ + private $access; + + public function __construct() { + $this->init(); + + $this->secure_key = $this->load_api_key(); + $this->email = $this->load_email(); + + add_action( 'tgmpa_register', array( $this, 'register_required_plugins' ) ); + register_activation_hook( __FILE__, array( $this, 'ssc_activation_hook' ) ); + } + + private function init() { + $this->settings = new Settings( $this ); + $this->access = new Access(); + + new Admin( $this ); + new Rest( $this ); + new Cron( $this ); + new Metaboxes( $this ); + new Shortcodes(); + } + + public function generate_secure_key() { + return bin2hex( random_bytes( 22 ) ); + } + + public function save_secure_key( $key ) { + update_option( 'ssc_secure_key', $key ); + } + + public function has_credentials() { + return $this->email && $this->secure_key; + } + + protected function load_email() { + return $this->settings->ssc_get_option( 'ssc_api_email' ); + } + + public function get_api_email() { + return $this->email; + } + + protected function load_api_key() { + return $this->settings->ssc_get_option( 'ssc_api_key' ); + } + + public function get_api_key() { + return $this->secure_key; + } + + public function validate_secure_key( $key_to_validate ) { + return $key_to_validate == sha1( $this->secure_key ); + } + + public function get_settings() { + return $this->settings; + } + + public function get_access() { + return $this->access; + } + + /** + * Register the required plugins for this plugin. + */ + public function register_required_plugins() { + global $wp_version; + if ( $wp_version < '4.7' ) { + $plugins = array( + array( + 'name' => 'Wordpress Rest API', + 'slug' => 'rest-api', + 'required' => true, + ) + ); + + $config = array( + 'id' => 'ssc', + 'default_path' => '', + 'menu' => 'ssc-install-plugins', + 'parent_slug' => 'tools.php', + 'capability' => 'edit_theme_options', + 'has_notices' => true, + 'dismissable' => true, + 'dismiss_msg' => '', + 'is_automatic' => false, + 'message' => '', + ); + + tgmpa( $plugins, $config ); + } + } + + public function get_post_types() { + $args = array( + 'public' => true + ); + + return get_post_types( $args ); + } + + + public function ssc_activation_hook() { + if ( ! function_exists( 'curl_init' ) || ! function_exists( 'random_bytes' ) ) { + echo '

' . __( 'Aktivace se nezdařila. Kontaktuje prosím poskytovatele Vašeho hostingu a požádejte o instalaci rozšíření PHP - CURL a MCRYPT.', + 'ssc' ) . '

'; + + //Adding @ before will prevent XDebug output + @trigger_error( __( 'Aktivace se nezdařila. Kontaktuje prosím poskytovatele Vašeho hostingu a požádejte o instalaci rozšíření PHP - CURL a MCRYPT.', + 'ssc' ), E_USER_ERROR ); + } + + // Generate and save the secure key$this = new Loader(); + $key = $this->generate_secure_key(); + $this->save_secure_key( $key ); + } +} diff --git a/src/Membership.php b/src/Membership.php new file mode 100644 index 0000000..2a00c69 --- /dev/null +++ b/src/Membership.php @@ -0,0 +1,97 @@ + + */ + +namespace Redbit\SimpleShop\WpPlugin; + +class Membership { + private $user_id; + public $groups = array(); + + /** + * Membership constructor. + * Get user data if requested + * + * @param string $user_id + */ + public function __construct( $user_id = '' ) { + if ( $user_id ) { + $this->user_id = (int) $user_id; + $this->get(); + } + } + + /** + * Get membership data for a specific user + */ + public function get() { + $ssc_groups = new Group(); + $groups = $ssc_groups->get_user_groups( $this->user_id ); + + foreach ( $groups as $group ) { + $this->groups[ $group ] = array( + 'group_id' => $group, + 'subscription_date' => $this->get_subscription_date( $group ), + 'valid_to' => $this->get_valid_to( $group ) + ); + } + } + + /** + * Set the date until the memership is valid + * + * @param $group_id + * @param $valid_to + * + * @return bool|int + */ + public function set_valid_to( $group_id, $valid_to ) { + if ( ! $this->user_id ) { + return false; + } + + return update_user_meta( $this->user_id, '_ssc_group_subscription_valid_to_' . $group_id, $valid_to ); + } + + /** + * Set the date of user subscription to the group + * + * @param $group_id + * + * @return bool|int + */ + public function set_subscription_date( $group_id ) { + if ( ! $this->user_id ) { + return false; + } + + return update_user_meta( $this->user_id, '_ssc_group_subscription_date_' . $group_id, date( 'Y-m-d' ) ); + } + + /** + * Get group subscription date + * + * @param $group_id + * + * @return mixed + */ + public function get_subscription_date( $group_id ) { + return get_user_meta( $this->user_id, '_ssc_group_subscription_date_' . $group_id, true ); + } + + /** + * Get the date until the subscription is valid for specific group + * + * @param $group_id + * + * @return mixed + */ + public function get_valid_to( $group_id ) { + return get_user_meta( $this->user_id, '_ssc_group_subscription_valid_to_' . $group_id, true ); + } +} + diff --git a/src/Metaboxes.php b/src/Metaboxes.php new file mode 100644 index 0000000..ad4eeeb --- /dev/null +++ b/src/Metaboxes.php @@ -0,0 +1,185 @@ + + */ + +namespace Redbit\SimpleShop\WpPlugin; + +class Metaboxes { + public $prefix = '_ssc_'; + + /** + * @var Loader + */ + private $loader; + + public function __construct(Loader $loader) { + $this->loader = $loader; + add_action( 'cmb2_admin_init', array( $this, 'page_metaboxes' ) ); + add_action( 'cmb2_admin_init', array( $this, 'user_metaboxes' ) ); + } + + /** + * Add metabox to pages and posts + * TODO: find a way to add custom post types + */ + public function page_metaboxes() { + + $ssc_group = new Group(); + $groups = $ssc_group->get_groups(); + $ssc_access = $this->loader->get_access(); + $post_types = $this->loader->get_post_types(); + + + if ( $groups && $ssc_access->user_is_admin() ) { + /** + * Initiate the metabox + */ + $cmb = new_cmb2_box( array( + 'id' => 'ssc_page_groups', + 'title' => __( 'SimpleShop - členské sekce', 'ssc' ), + 'object_types' => $post_types, + 'context' => 'normal', + 'priority' => 'high', + 'show_names' => true, + ) ); + + $cmb->add_field( array( + 'name' => __( 'Členské sekce, které mají přístup na stránku', 'ssc' ), + 'desc' => __( 'Pouze uživatelé v zaškrtnutých členskéch sekcích mají přístup na tuto stránku. Pokud nic nezaškrtnete, stránku uvidí všichni uživatelé', + 'ssc' ), + 'id' => $this->prefix . 'groups', + 'type' => 'multicheck', + 'options' => $groups, + ) ); + + $tmp_post_types = $post_types; + unset( $tmp_post_types['attachment'] ); + + $cmb->add_field( array( + 'name' => __( 'ID stránky pro přesměrování', 'ssc' ), + 'desc' => __( 'Vyberte stránku, na kterou uživatel bude přesměrován, pokud je přihlášen, ale nemá oprávnění k přístupu. Toto je preferovaný způsob - přsměrování bude fungovat, i pokud se v budoucnu změní adresa stránky', + 'ssc' ), + 'id' => $this->prefix . 'no_access_redirect_post_id', + 'type' => 'post_search_text', + 'select_type' => 'radio', + 'select_behavior' => 'replace', + 'post_type' => $tmp_post_types + ) ); + + + $cmb->add_field( array( + 'name' => __( 'Manuální adresa přesměrování', 'ssc' ), + 'desc' => __( 'Zadejte ručně adresu, na kterou uživatel bude přesměrován, pokud je přihlášen, ale nemá oprávnění k přístupu. Pokud využijete tuto volbu, pole výše musí být prázdné.', + 'ssc' ), + 'id' => $this->prefix . 'no_access_redirect', + 'type' => 'text' + ) ); + +// $cmb->add_field(array( +// 'name' => __('Přesměrovat na přihlášení', 'ssc'), +// 'desc' => __('Zaškrtněte, pokud chcete uživatele přesměrovat na přihlašovací formulář. Po přihlášení bude uživatel přesměrován zpět na tuto stránku.', 'ssc'), +// 'id' => $this->prefix . 'no_access_redirect_to_login_form', +// 'type' => 'checkbox' +// )); + + + $cmb->add_field( array( + 'name' => __( 'Povolit přístup po X dnech od přiřazení do skupiny', 'ssc' ), + 'desc' => __( 'Zadejte počet dní, které musí uplynout od přihlášení uživatele do skupiny pro získání přístupu k tomuto obsahu. Pokud např. uživatel koupí produkt 1.ledna a nastavíte 5 dní, uživatel bude mít ke stránce přístup 6. ledna.', + 'ssc' ), + 'id' => $this->prefix . 'days_to_access', + 'type' => 'text' + ) ); + + $cmb->add_field( array( + 'name' => __( 'Povolit přístup OD data', 'ssc' ), + 'desc' => __( 'Zadejte datum, od kterého bude stránka přístupná (platí pro všechny skupiny)', + 'ssc' ), + 'id' => $this->prefix . 'date_to_access', + 'type' => 'text_date', + 'date_format' => 'Y-m-d', + ) ); + + $cmb->add_field( array( + 'name' => __( 'Povolit přístup DO data', 'ssc' ), + 'desc' => __( 'Zadejte datum, do kterého bude stránka přístupná (platí pro všechny skupiny)', + 'ssc' ), + 'id' => $this->prefix . 'date_until_to_access', + 'type' => 'text_date', + 'date_format' => 'Y-m-d', + ) ); + + $cmb->add_field( array( + 'name' => __( 'Předmět emailu při zpřístupnění obsahu', 'ssc' ), + 'desc' => __( 'Zadejte předmět emailu, který se uživateli automaticky odešlě v okamžiku, kdy získá přístup k tomuto obsahu na základě nastavení dní výše.', + 'ssc' ), + 'id' => $this->prefix . 'email_subject_user_can_access', + 'type' => 'text' + ) ); + + $cmb->add_field( array( + 'name' => __( 'Email při zpřístupnění obsahu', 'ssc' ), + 'desc' => __( 'Zadejte email, který se uživateli automaticky odešlě v okamžiku, kdy získá přístup k tomuto obsahu na základě nastavení dní výše.', + 'ssc' ), + 'id' => $this->prefix . 'email_user_can_access', + 'type' => 'wysiwyg' + ) ); + } + } + + /** + * Add metabox to user profile + */ + public function user_metaboxes() { + + /** + * Initiate the metabox + */ + $cmb = new_cmb2_box( array( + 'id' => 'ssc_user_groups', + 'title' => __( 'SimpleShop', 'ssc' ), + 'object_types' => array( 'user' ), + 'context' => 'normal', + 'priority' => 'high', + 'show_names' => true, + ) ); + + $ssc_group = new Group(); + $groups = $ssc_group->get_groups(); + + + $access = $this->loader->get_access(); + + if ( $access->user_is_admin() ) { + $cmb->add_field( array( + 'name' => __( 'SimpleShop - členské sekce
Vyberte, do kterých členských sekcí má mít uživatel přístup.', + 'ssc' ), +// 'desc' => __('Vyberte, do kterých členských sekcí má mít uživatel přístup','ssc'), + 'id' => $this->prefix . 'user_groups', + 'type' => 'multicheck', + 'options' => $groups, + ) ); + + foreach ( $groups as $key => $group ) { + $cmb->add_field( array( + 'name' => '' . sprintf( __( 'Datum registrace do skupiny %s.', + 'ssc' ), $group ) . '', + 'id' => $this->prefix . 'group_subscription_date_' . $key, + 'type' => 'text_date', + 'date_format' => 'Y-m-d', + ) ); + $cmb->add_field( array( + 'name' => '' . sprintf( __( 'Datum vypršení platnosti registrace do skupiny %s.', + 'ssc' ), $group ) . '', + 'id' => $this->prefix . 'group_subscription_valid_to_' . $key, + 'type' => 'text_date', + 'date_format' => 'Y-m-d', + ) ); + } + } + } +} diff --git a/src/Rest.php b/src/Rest.php new file mode 100644 index 0000000..2337f54 --- /dev/null +++ b/src/Rest.php @@ -0,0 +1,249 @@ + + */ + +namespace Redbit\SimpleShop\WpPlugin; + +class Rest extends \WP_REST_Controller { + + /** + * @var Loader + */ + private $loader; + + public function __construct( Loader $loader ) { + $this->loader = $loader; + + add_action( 'rest_api_init', array( $this, 'register_routes' ) ); + } + + /** + * Register the routes for the objects of the controller. + */ + public function register_routes() { + $version = '1'; + $namespace = 'simpleshop/v' . $version; + register_rest_route( $namespace, '/group', array( + array( + 'methods' => \WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_groups' ), + 'permission_callback' => array( $this, 'create_item_permissions_check' ), + 'args' => $this->get_endpoint_args_for_item_schema( true ), + ), + ) ); + + register_rest_route( $namespace, '/add-member', array( + array( + 'methods' => \WP_REST_Server::READABLE, + 'callback' => array( $this, 'create_item' ), + 'permission_callback' => array( $this, 'create_item_permissions_check' ), + 'args' => $this->get_endpoint_args_for_item_schema( true ), + ), + ) ); + +// register_rest_route($namespace,'/'.$.'/schema',array( +// 'methods' => \WP_REST_Server::READABLE, +// 'callback' => array($this,'get_public_item_schema'), +// )); + } + + public function get_groups() { + $ssc_group = new Group(); + + return new \WP_REST_Response( $ssc_group->get_groups(), 200 ); + } + + /** + * Create one item from the collection + * + * @param \WP_REST_Request $request Full data about the request. + * + * @return \WP_REST_Response + */ + public function create_item( $request ) { + // Check if we got all the needed params + $params_to_validate = array( 'email' ); + foreach ( $params_to_validate as $param ) { + if ( ! $request->get_param( $param ) ) { + return new \WP_Error( 'required-param-missing', + sprintf( __( 'Required parameter %s is missing', 'ssc' ), $param ), + array( 'status' => 500, 'plugin_version' => SIMPLESHOP_PLUGIN_VERSION ) ); + } + } + + // Check if we got valid email + $email = sanitize_email( $request->get_param( 'email' ) ); + if ( ! is_email( $email ) ) { + return new \WP_Error( 'wrong-email-format', __( 'The email is in wrong format', 'ssc' ), + array( 'status' => 500, 'plugin_version' => SIMPLESHOP_PLUGIN_VERSION ) ); + } + + // Check if user with this email exists, if not, create a new user + $_login = $email; + $_password = 'Změnit ho můžete zde'; + if ( ! email_exists( $email ) ) { + $_password = wp_generate_password( 8, false ); + $userdata = array( + 'user_login' => $email, + 'user_email' => $email, + 'first_name' => sanitize_text_field( $request->get_param( 'firstname' ) ), + 'last_name' => sanitize_text_field( $request->get_param( 'lastname' ) ), + 'user_pass' => $_password, + ); + + $user_id = wp_insert_user( $userdata ); +// wp_new_user_notification($user_id,$userdata['user_pass']); // poslani notifikacniho e-mailu + + if ( is_wp_error( $user_id ) ) { + return new \WP_Error( 'could-not-create-user', __( "The user couldn't be created", 'ssc' ), + array( 'status' => 500, 'plugin_version' => SIMPLESHOP_PLUGIN_VERSION ) ); + } + } else { + // Get user_by email + $user = get_user_by( 'email', $email ); + $user_id = $user->ID; + } + + // Check if group exists + $user_groups = array(); + foreach ( $request->get_param( 'user_group' ) as $group ) { + $ssc_group = new Group( $group ); + + // Add the user to group + if ( $ssc_group->group_exists() ) { + $ssc_group->add_user_to_group( $user_id ); + + // Set the membership valid_to param + $membership = new Membership( $user_id ); + $valid_to = $request->get_param( 'valid_to' ) ?: ''; + $membership->set_valid_to( $group, $valid_to ); + + $user_groups[] = $group; + } + } + + // If we are on multisite, add the user the site + if ( is_multisite() ) { + add_user_to_blog( get_current_blog_id(), $user_id, 'subscriber' ); + } + + // Get the posts that have some group assigned + global $wpdb; + $posts = $wpdb->get_results( "SELECT post_id, meta_value + FROM $wpdb->postmeta + WHERE meta_key = '_ssc_groups' + " ); + + // Get the post details + $links = array(); + $i = 0; + + // Foreach group from request + // foreach($request->get_param('user_group') as $group){ + // Foreach each group + $SSC_group = new Group(); + foreach ( $SSC_group->get_user_groups( $user_id ) as $group ) { + // Scrub through posts and check, if some of the posts has that group assigned + foreach ( $posts as $post ) { + $access = $this->loader->get_access(); + + if ( in_array( $group, unserialize( $post->meta_value ) ) ) { + // Check if the post can be accessed already, if not, continue + $specific_date = $access->get_post_date_to_access( $post->post_id ); + $days_to_access = $access->get_post_days_to_access( $post->post_id ); + + if ( $specific_date && date( 'Y-m-d' ) < $specific_date ) { + continue; + } + + if ( $days_to_access && $days_to_access > 0 ) { + continue; + } + + // If so, get the post details and add it to the links array + $post_details = get_post( $post->post_id ); + $links[ $group ][ $i ]['title'] = $post_details->post_title; + $links[ $group ][ $i ]['url'] = get_permalink( $post->post_id ); + $i ++; + } + } + } + + $email_enable = nl2br( $this->loader->get_settings()->ssc_get_option( 'ssc_email_enable' ) ); + + // It doesn't seem to make sense to send email without the links, so check first + if ( ( (string) $email_enable ) != '2' ) { // pokud nemame zakazano posilat mail novym clenum + $email_body = nl2br( $this->loader->get_settings()->ssc_get_option( 'ssc_email_text' ) ); + $email_subject = nl2br( $this->loader->get_settings()->ssc_get_option( 'ssc_email_subject' ) ); + $pages = ''; + foreach ( $links as $groupid => $linksInGroup ) { + $post_details = get_post( $groupid ); + $pages .= '
' . $post_details->post_title . '
' + . ''; + } + + $replaceArray = array(// pole ktera je mozne nahradit + '{pages}' => $pages,// zpetna kompatibilita s v1.1 + '{mail}' => $email,// zpetna kompatibilita s v1.1 + '{login}' => $_login, + '{password}' => $_password, + '{login_url}' => wp_login_url(), +// '{login}' => $_login, +// '{password}' => $_password, + ); + $email_body = str_replace( array_keys( $replaceArray ), array_values( $replaceArray ), $email_body ); +// $email_body = str_replace('{pages}',$pages,$email_body); +// $email_body = str_replace('{mail}',$email,$email_body); + $headers = array( 'Content-Type: text/html; charset=UTF-8' ); + + // Send the email + wp_mail( $email, $email_subject, $email_body, $headers ); + } + + return new \WP_REST_Response( array( 'status' => 'success', 'plugin_version' => SIMPLESHOP_PLUGIN_VERSION ), + 200 ); + } + + /** + * Check if a given request has access to create items + * + * @param \WP_REST_Request $request Full data about the request. + * + * @return \WP_Error|bool + */ + public function create_item_permissions_check( $request ) { + return $this->loader->validate_secure_key( $request->get_param( 'hash' ) ); + } + + /** + * Prepare the item for create or update operation + * + * @param \WP_REST_Request $request Request object + * + * @return array $prepared_item + */ + protected function prepare_item_for_database( $request ) { + return array(); + } + + /** + * Prepare the item for the REST response + * + * @param mixed $item WordPress representation of the item. + * @param \WP_REST_Request $request Request object. + * + * @return mixed + */ + public function prepare_item_for_response( $item, $request ) { + return array(); + } + +} diff --git a/src/Settings.php b/src/Settings.php new file mode 100644 index 0000000..ab17d24 --- /dev/null +++ b/src/Settings.php @@ -0,0 +1,311 @@ + + */ + +namespace Redbit\SimpleShop\WpPlugin; + +use Redbit\SimpleShop\WpPlugin\Vyfakturuj\VyfakturujAPI; + +/** + * CMB2 Theme Options + * @version 0.1.0 + * + * @property-read string $key + * @property-read string $metabox_id + * @property-read string $title + * @property-read string $options_page + */ +class Settings { + + /** + * Options Page title + * @var string + */ + protected $title = ''; + + /** + * Options Page hook + * @var string + */ + protected $options_page = ''; + + /** + * Option key, and option page slug + * @var string + */ + private $key = 'ssc_options'; + + /** + * Options page metabox id + * @var string + */ + private $metabox_id = 'ssc_option_metabox'; + /** + * @var Loader + */ + private $loader; + + /** + * Constructor + * @since 0.1.0 + * + * @param Loader $loader + */ + public function __construct(Loader $loader) { + // Set our title + $this->title = __( 'Nastavení', 'ssc' ); + $this->register_hooks(); + $this->loader = $loader; + } + + /** + * Initiate our hooks + * @since 0.1.0 + */ + public function register_hooks() { + add_action( 'admin_init', array( $this, 'init' ) ); + add_action( 'admin_menu', array( $this, 'add_options_page' ) ); + add_action( 'cmb2_admin_init', array( $this, 'add_options_page_metabox' ) ); + } + + /** + * Register our setting to WP + * @since 0.1.0 + */ + public function init() { + register_setting( $this->key, $this->key ); + } + + /** + * Add menu options page + * @since 0.1.0 + */ + public function add_options_page() { + + add_submenu_page( 'simple_shop_settings', $this->title, $this->title, 'manage_options', + 'admin.php?page=' . $this->key, array( $this, 'admin_page_display' ) ); + + $this->options_page = add_menu_page( $this->title, $this->title, 'manage_options', $this->key, + array( $this, 'admin_page_display' ) ); + remove_menu_page( $this->key ); + // Include CMB CSS in the head to avoid FOUC + add_action( "admin_print_styles-{$this->options_page}", array( 'CMB2_hookup', 'enqueue_cmb_css' ) ); + } + + /** + * Admin page markup. Mostly handled by CMB2 + * @since 0.1.0 + */ + public function admin_page_display() { + ?> +
+

+ metabox_id, $this->key ); ?> +
+ metabox_id}", array( $this, 'settings_notices' ), 10, 2 ); + + $cmb = new_cmb2_box( array( + 'id' => $this->metabox_id, + 'hookup' => false, + 'cmb_styles' => true, + 'show_on' => array( + // These are important, don't remove + 'key' => 'options-page', + 'value' => array( $this->key, ) + ), + ) ); + + # + # MAIL + # + $cmb->add_field( array( + 'name' => 'Nastavení e-mailu, který se posílá novým členům:', +// 'desc' => 'This is a title description', + 'classes_cb' => array( $this, 'is_valid_api_keys' ), + 'type' => 'title', + 'id' => 'ssc_email_title' + ) ); + $cmb->add_field( array( + 'name' => 'Poslat e-mail novému členovi?', +// 'desc' => 'Select an option', + 'id' => 'ssc_email_enable', + 'type' => 'select', + 'show_option_none' => false, + 'classes_cb' => array( $this, 'is_valid_api_keys' ), + 'default' => '1', + 'options' => array( + '1' => __( 'Ano, poslat každému novému členovi e-mail.', 'cmb2' ), + '2' => __( 'Ne, zakázat posílání e-mailu novým členům.', 'cmb2' ), + ), + ) ); + $cmb->add_field( array( + 'name' => __( 'Předmět e-mailu', 'ssc' ), +// 'desc' => __('Najdete ho ve svém SimpleShop účtu v Nastavení -> WP Plugin','ssc'), + 'id' => 'ssc_email_subject', + 'classes_cb' => array( $this, 'is_valid_api_keys' ), + 'type' => 'text', + 'default' => 'Byl Vám udělen přístup do členské sekce', + ) ); + + + $cmb->add_field( array( + 'name' => __( 'Text emailu', 'ssc' ), + 'desc' => __( 'Povolené zástupné znaky:
' + . '
{login} = login
' + . '{password} = heslo
' + . '{login_url} = adresa, na které je možné se přihlásit
' + . '{pages} = seznam stránek, do kterých má uživatel zakoupený přístup
' + . '{mail} = e-mail uživatele (většinou stejný jako login)
' + . '
' + . '', 'ssc' ), + 'id' => 'ssc_email_text', + 'type' => 'wysiwyg', + 'classes_cb' => array( $this, 'is_valid_api_keys' ), + 'default' => 'Dobrý den, +byl udělen přístup do členské sekce. + +Login: {login} +Heslo: {password} + +Přihlásit se můžete na: ' . wp_login_url() . ' + +Váš zakoupený obsah: +{pages} + +S pozdravem a přáním pěkného dne, +SimpleShop.cz - S námi zvládne prodávat každý' + ) ); + + + # + # API + # + $cmb->add_field( array( + 'name' => 'Nastavení API - propojení s aplikací SimpleShop:', +// 'desc' => 'This is a title description', +// 'show_on_cb' => array($this,'is_valid_api_keys'), + 'type' => 'title', + 'id' => 'ssc_api_title' + ) ); + + // Set our CMB2 fields + $cmb->add_field( array( + 'name' => __( 'Přihlašovací email', 'ssc' ), + 'desc' => __( 'Zadejte email, který používáte pro přihlášení do služby SimpleShop', 'ssc' ), + 'id' => 'ssc_api_email', + 'type' => 'text', + ) ); + + $cmb->add_field( array( + 'name' => __( 'SimpleShop API Klíč', 'ssc' ), + 'desc' => __( 'Najdete ho ve svém SimpleShop účtu v Nastavení -> WP Plugin', 'ssc' ), + 'id' => 'ssc_api_key', + 'type' => 'text', + ) ); + } + + /** + * Register settings notices for display + * + * @since 0.1.0 + * + * @param int $object_id Option key + * @param array $updated Array of updated fields + * + * @return void + */ + public function settings_notices( $object_id, $updated ) { + $vyfakturuj_api = new VyfakturujAPI( $this->loader->get_api_email(), $this->loader->get_api_key() ); + $result = $vyfakturuj_api->initWPPlugin( site_url() ); + if ( isset( $result['status'] ) && $result['status'] == 'success' ) { + update_option( 'ssc_valid_api_keys', 1 ); + } else { + update_option( 'ssc_valid_api_keys', 0 ); + } + + if ( $object_id !== $this->key || empty( $updated ) ) { + return; + } + + add_settings_error( $this->key . '-notices', '', __( 'Settings updated.', 'ssc' ), 'updated' ); + settings_errors( $this->key . '-notices' ); + } + + /** + * Check, if we already got valid API key, if not, add 'hidden' class to the settings that are not needed in the first step + * We switched to this approach, because by just hiding the fields the default values are saved even on the first save, + * but previously the fields were removed completely from the form, so the default values were not saved until the API keys were in place + * @return array + */ + function is_valid_api_keys() { + // If the keys are valid, do nothing + if ( get_option( 'ssc_valid_api_keys' ) == 1 ) { + return array(); + } + + return array( 'hidden' ); + } + + /** + * Public getter method for retrieving protected/private variables + * @since 0.1.0 + * + * @param string $field Field to retrieve + * + * @return mixed Field value or exception is thrown + * @throws \Exception + */ + public function __get( $field ) { + // Allowed fields to retrieve + if ( in_array( $field, array( 'key', 'metabox_id', 'title', 'options_page' ), true ) ) { + return $this->{$field}; + } + + throw new \Exception( 'Invalid property: ' . $field ); + } + + /** + * Wrapper function around cmb2_get_option + * @since 0.1.0 + * + * @param string $key Options array key + * @param mixed $default Optional default value + * + * @return mixed Option value + */ + public function ssc_get_option( $key = '', $default = null ) { + if ( function_exists( 'cmb2_get_option' ) ) { + // Use cmb2_get_option as it passes through some key filters. + return cmb2_get_option( $this->key, $key, $default ); + } + + // Fallback to get_option if CMB2 is not loaded yet. + $opts = get_option( $this->key, $key, $default ); + + $val = $default; + + if ( 'all' == $key ) { + $val = $opts; + } elseif ( is_array( $opts ) && array_key_exists( $key, $opts ) && false !== $opts[ $key ] ) { + $val = $opts[ $key ]; + } + + return $val; + } + + +} \ No newline at end of file diff --git a/src/Shortcodes.php b/src/Shortcodes.php new file mode 100644 index 0000000..d587787 --- /dev/null +++ b/src/Shortcodes.php @@ -0,0 +1,107 @@ + + */ + +namespace Redbit\SimpleShop\WpPlugin; + +class Shortcodes { + + public function __construct() { + add_action( 'init', array( $this, 'initialize' ) ); + } + + public function initialize() { + add_shortcode( 'SimpleShop-form', array( $this, 'simple_shop_form' ) ); + add_shortcode( 'SimpleShop-content', array( $this, 'simple_shop_content' ) ); + } + + + public function simple_shop_form( $atts ) { + $url = substr( $_SERVER['SERVER_NAME'], + - 2 ) === 'lc' ? 'http://form.simpleshop.czlc' : 'https://form.simpleshop.cz'; + + return ''; + } + + public function simple_shop_content( $atts, $content = '' ) { + $atts = shortcode_atts( array( + 'group_id' => '', + 'is_member' => '', + 'days_to_view' => '', + 'specific_date_from' => '', + 'specific_date_to' => '', + + ), $atts, 'SimpleShop-content' ); + + $group_id = $atts['group_id']; + $is_member = $atts['is_member']; + $specific_date_from = $atts['specific_date_from']; + $specific_date_to = $atts['specific_date_to']; + $days_to_view = $atts['days_to_view']; + + if ( ! empty( $specific_date_from ) ) { + // Check against the from date, this has nothing to do with groups or other settings + if ( date( 'Y-m-d' ) < $specific_date_from ) { + return ''; + } + } + + if ( ! empty( $specific_date_to ) ) { + // Check against the to date, this has nothing to do with groups or other settings + if ( date( 'Y-m-d' ) > $specific_date_to ) { + return ''; + } + } + + // Stop if there's no group_id or is_member, and no specific date is set + if ( empty( $group_id ) || ( empty( $is_member ) && empty( $specific_date_from ) && empty( $specific_date_to ) ) ) { + return ''; + } + + $group = new Group( $group_id ); + + if ( $is_member == 'yes' ) { + // Check, if the user is logged in and is member of the group, if not, bail + if ( ! is_user_logged_in() || ! $group->user_is_member_of_group( get_current_user_id() ) ) { + return ''; + } + } elseif ( $is_member == 'no' ) { + // Check, if the user is NOT a member of specific group. This includes non-logged-in users + if ( is_user_logged_in() && $group->user_is_member_of_group( get_current_user_id() ) ) { + return ''; + } + } else { + // If the is_member isn't 'yes' or 'no', the parameter is wrong, so stop here + return ''; + } + + // Group check done, check if there are some days set and if is_member is yes + // it doesn't make sense to check days condition for users who should NOT be members of a group + if ( ! empty( $days_to_view ) && $is_member == 'yes' ) { + $membership = new Membership( get_current_user_id() ); + $subscription_date = $membership->groups[ $group_id ]['subscription_date']; + // Compare against today's date + if ( date( 'Y-m-d' ) < date( 'Y-m-d', strtotime( "$subscription_date + $days_to_view days" ) ) ) { + return ''; + } + } + + // Support shortcodes inside shortcodes + + // Fix for MioWEB + $hook = Helpers::ssc_remove_anonymous_object_filter( 'the_content', 'visualEditorPage', 'create_content' ); + + $content = apply_filters( 'the_content', $content ); + + // Add the filter back if needed + if ( $hook ) { + add_filter( $hook[0], $hook[1], $hook[2] ); + } + + return $content; + } +} diff --git a/src/Vyfakturuj/VyfakturujAPI.php b/src/Vyfakturuj/VyfakturujAPI.php index 200f6e1..81626e1 100644 --- a/src/Vyfakturuj/VyfakturujAPI.php +++ b/src/Vyfakturuj/VyfakturujAPI.php @@ -1,14 +1,12 @@ - * @author Ing. Martin Dostál */ -namespace SSC\Vyfakturuj; - +namespace Redbit\SimpleShop\WpPlugin\Vyfakturuj; /** * Rozšíření třídy \VyfakturujAPI o metody, které SSC potřebuje @@ -17,7 +15,7 @@ class VyfakturujAPI extends \VyfakturujAPI { public function initWPPlugin( $domain ) { return $this->fetchPost( 'wpplugin/init/', - array( 'domain' => $domain, 'plugin_version' => SSC_PLUGIN_VERSION ) + array( 'domain' => $domain, 'plugin_version' => SIMPLESHOP_PLUGIN_VERSION ) ); } }