public function bildThePage($options = array(), $alias = '', $module = array(), $showForm = true)
        {
            global $psp;
            // reset as array, this will stock all the html content, and at the end return it
            $html = array();
            if (count($options) == 0) {
                return __('Please fill whit some options content first!', $psp->localizationName);
            }
            $noRowElements = array('message', 'html', 'app');
            foreach ($options as $theBoxs) {
                // loop the all the boxs
                foreach ($theBoxs as $box_id => $box) {
                    $box_id = $alias . "_" . $box_id;
                    $settings = array();
                    // get the values from DB
                    $dbValues = get_option($box_id);
                    // check if isset and string have content
                    if (isset($dbValues) && @trim($dbValues) != "") {
                        $settings = maybe_unserialize($dbValues);
                    }
                    // create defalt setup for each header, prevent php notices
                    if (!isset($box['header'])) {
                        $box['header'] = false;
                    }
                    if (!isset($box['toggler'])) {
                        $box['toggler'] = false;
                    }
                    if (!isset($box['buttons'])) {
                        $box['buttons'] = false;
                    }
                    if (!isset($box['style'])) {
                        $box['style'] = 'panel';
                    }
                    // container setup
                    $html[] = '<div class="psp-' . $box['size'] . '">
                        	<div class="psp-' . $box['style'] . '">';
                    // hide panel header only if it's requested
                    if ($box['header'] == true) {
                        $html[] = '<div class="psp-panel-header">
							<span class="psp-panel-title">
								' . (isset($box['icon']) ? '<img src="' . $box['icon'] . '" />' : '') . '
								' . $box['title'] . '
							</span>
							 ' . ($box['toggler'] == true ? '<span class="psp-panel-toggler"></span>' : '') . '
						</div>';
                    }
                    $html[] = '<div class="psp-panel-content">';
                    if ($showForm) {
                        $html[] = '<form class="psp-form" id="' . $box_id . '" action="#save_with_ajax">';
                    }
                    // create a hidden input for sending the prefix
                    $html[] = '<input type="hidden" id="box_id" name="box_id" value="' . $box_id . '" />';
                    $html[] = '<input type="hidden" id="box_nonce" name="box_nonce" value="' . wp_create_nonce($box_id . '-nonce') . '" />';
                    $html[] = $this->tabsHeader($box);
                    // tabs html header
                    // loop the box elements
                    if (count($box['elements']) > 0) {
                        // loop the box elements now
                        foreach ($box['elements'] as $elm_id => $value) {
                            // some helpers. Reset an each loop, prevent collision
                            $val = '';
                            $select_value = '';
                            $checked = '';
                            $option_name = isset($option_name) ? $option_name : '';
                            // Set default value to $val
                            if (isset($value['std'])) {
                                $val = $value['std'];
                            }
                            // If the option is already saved, ovveride $val
                            if ($value['type'] != 'info') {
                                if (isset($settings[$elm_id]) && @trim($settings[$elm_id]) != "") {
                                    $val = $settings[$elm_id];
                                    // Striping slashes of non-array options
                                    if (!is_array($val)) {
                                        $val = stripslashes($val);
                                        //if($val == '') $val = true;
                                    }
                                }
                            }
                            // If there is a description save it for labels
                            $explain_value = '';
                            if (isset($value['desc'])) {
                                $explain_value = $value['desc'];
                            }
                            if (!in_array($value['type'], $noRowElements)) {
                                // the row and the label
                                $html[] = '<div class="psp-form-row' . $this->tabsElements($box, $elm_id) . '">
									   <label for="' . $elm_id . '">' . (isset($value['title']) ? $value['title'] : '') . '</label>
									   <div class="psp-form-item' . (isset($value['size']) ? " " . $value['size'] : '') . '">';
                            }
                            // the element description
                            if (isset($value['desc'])) {
                                $html[] = '<span class="formNote">' . $value['desc'] . '</span>';
                            }
                            switch ($value['type']) {
                                // Basic text input
                                case 'text':
                                    $html[] = '<input ' . (isset($value['readonly']) && $value['readonly'] == true ? 'readonly' : '') . ' ' . (isset($value['force_width']) ? "style='width:" . $value['force_width'] . "px;'" : '') . ' id="' . esc_attr($elm_id) . '" name="' . esc_attr($option_name . $elm_id) . '" type="text" value="' . esc_attr($val) . '" />';
                                    break;
                                    // Basic checkbox input
                                // Basic checkbox input
                                case 'checkbox':
                                    if ($val == '') {
                                        $val = true;
                                    }
                                    $html[] = '<input ' . (isset($value['force_width']) ? "style='width:" . $value['force_width'] . "px;'" : '') . ' ' . ($val == true ? 'checked' : '') . ' id="' . esc_attr($elm_id) . '" name="' . esc_attr($option_name . $elm_id) . '" type="checkbox" value="" />';
                                    break;
                                    // Basic upload_image
                                // Basic upload_image
                                case 'upload_image':
                                    $html[] = '<table border="0">';
                                    $html[] = '<tr>';
                                    $html[] = '<td>';
                                    $html[] = '<input class="upload-input-text" name="' . $elm_id . '" id="' . $elm_id . '_upload" type="text" value="' . $val . '" />';
                                    $html[] = '<script type="text/javascript">
										jQuery("#' . $elm_id . '_upload").data({
											"w": ' . $value['thumbSize']['w'] . ',
											"h": ' . $value['thumbSize']['h'] . ',
											"zc": ' . $value['thumbSize']['zc'] . '
										});
									</script>';
                                    $html[] = '</td>';
                                    $html[] = '<td>';
                                    $html[] = '<a href="#" class="button upload_button" id="' . $elm_id . '">' . $value['value'] . '</a> ';
                                    //$html[] = 		'<a href="#" class="button reset_button ' . $hide . '" id="reset_' . ( $elm_id ) . '" title="' . ( $elm_id ) . '">Remove</a> ';
                                    $html[] = '</td>';
                                    $html[] = '</tr>';
                                    $html[] = '</table>';
                                    $html[] = '<a class="thickbox" id="uploaded_image_' . $elm_id . '" href="' . $val . '" target="_blank">';
                                    if (!empty($val)) {
                                        $imgSrc = $psp->image_resize($val, $value['thumbSize']['w'], $value['thumbSize']['h'], $value['thumbSize']['zc']);
                                        $html[] = '<img style="border: 1px solid #dadada;" id="image_' . $elm_id . '" src="' . $imgSrc . '" />';
                                    }
                                    $html[] = '</a>';
                                    $html[] = '<script type="text/javascript">
										loadAjaxUpload( jQuery("#' . $elm_id . '") );
									</script>';
                                    break;
                                    // Basic upload_image
                                // Basic upload_image
                                case 'upload_image_wp':
                                    $preview_size = isset($value['preview_size']) ? $value['preview_size'] : 'thumbnail';
                                    if ((int) $val > 0) {
                                        $image = wp_get_attachment_image_src($val, $preview_size);
                                        $image_full = wp_get_attachment_image_src($val, 'full');
                                        if (count($image) > 0) {
                                            $image = $image[0];
                                        }
                                        if (count($image_full) > 0) {
                                            $image_full = $image_full[0];
                                        }
                                    }
                                    $html[] = '<div class="psp-upload-image-wp-box">';
                                    $html[] = '<a data-previewsize="' . $preview_size . '" class="upload_image_button_wp psp-button blue" ' . (isset($value['force_width']) ? "style='" . (trim($val) != "" ? 'display:none;' : '') . "width:" . $value['force_width'] . "px;'" : '') . ' href="#">' . $value['value'] . '</a>';
                                    $html[] = '<input type="hidden" name="' . $elm_id . '" value="' . $val . '">';
                                    $html[] = '<a href="' . $image_full . '" target="_blank" class="upload_image_preview" style="display: ' . (trim($val) == "" ? 'none' : 'block') . '">';
                                    $html[] = '<img src="' . $image . '" style="display: ' . (trim($val) == "" ? 'none' : 'inline-block') . '">';
                                    $html[] = '</a>';
                                    $html[] = '<div class="psp-prev-buttons" style="display: ' . (trim($val) == "" ? 'none' : 'inline-block') . '">';
                                    $html[] = '<span class="change_image_button_wp psp-button green">Change Image</span>';
                                    $html[] = '<span class="remove_image_button_wp psp-button red">Remove Image</span>';
                                    $html[] = '</div>';
                                    $html[] = '</div>';
                                    break;
                                    // Basic textarea
                                // Basic textarea
                                case 'textarea':
                                    $cols = "120";
                                    if (isset($value['cols'])) {
                                        $cols = $value['cols'];
                                    }
                                    $height = "style='height:120px;'";
                                    if (isset($value['height'])) {
                                        $height = "style='height:{$value['height']};'";
                                    }
                                    $html[] = '<textarea id="' . esc_attr($elm_id) . '" ' . $height . ' cols="' . $cols . '" name="' . esc_attr($option_name . $elm_id) . '">' . esc_attr($val) . '</textarea>';
                                    break;
                                    // Basic html/text message
                                // Basic html/text message
                                case 'message':
                                    $html[] = '<div class="psp-message psp-' . (isset($value['status']) ? $value['status'] : '') . ' ' . $this->tabsElements($box, $elm_id) . '">' . $value['html'] . '</div>';
                                    break;
                                    // buttons
                                // buttons
                                case 'buttons':
                                    // buttons for each box
                                    if (count($value['options']) > 0) {
                                        foreach ($value['options'] as $key => $value) {
                                            $html[] = '<input
												type="' . (isset($value['type']) ? $value['type'] : '') . '"
												style="width:' . (isset($value['width']) ? $value['width'] : '') . '"
												value="' . (isset($value['value']) ? $value['value'] : '') . '"
												class="psp-button ' . (isset($value['color']) ? $value['color'] : '') . ' ' . (isset($value['pos']) ? $value['pos'] : '') . ' ' . (isset($value['action']) ? $value['action'] : '') . '"
											/>';
                                        }
                                    }
                                    break;
                                    // Basic html/text message
                                // Basic html/text message
                                case 'html':
                                    $html[] = $value['html'];
                                    break;
                                    // Basic app, load the path of this file
                                // Basic app, load the path of this file
                                case 'app':
                                    $tryLoadInterface = str_replace("{plugin_folder_path}", $module["folder_path"], $value['path']);
                                    if (is_file($tryLoadInterface)) {
                                        // Turn on output buffering
                                        ob_start();
                                        require_once $tryLoadInterface;
                                        //copy current buffer contents into $message variable and delete current output buffer
                                        $html[] = ob_get_clean();
                                    }
                                    break;
                                    // Select Box
                                // Select Box
                                case 'select':
                                    $html[] = '<select ' . (isset($value['force_width']) ? "style='width:" . $value['force_width'] . "px;'" : '') . ' name="' . esc_attr($elm_id) . '" id="' . esc_attr($elm_id) . '">';
                                    foreach ($value['options'] as $key => $option) {
                                        $selected = '';
                                        if ($val != '') {
                                            if ($val == $key || $val == $option) {
                                                $selected = ' selected="selected"';
                                            }
                                        }
                                        $html[] = '<option' . $selected . ' value="' . esc_attr($key) . '">' . esc_html($option) . '</option>';
                                    }
                                    $html[] = '</select>';
                                    break;
                                    // multiselect Box
                                // multiselect Box
                                case 'multiselect':
                                    $html[] = '<select ' . (isset($value['force_width']) ? "style='width:" . $value['force_width'] . "px;'" : '') . ' multiple="multiple" size="6" name="' . esc_attr($elm_id) . '[]" id="' . esc_attr($elm_id) . '">';
                                    if (count($value['options']) > 0) {
                                        foreach ($value['options'] as $key => $option) {
                                            $selected = '';
                                            if ($val != '') {
                                                if (in_array($key, $val)) {
                                                    $selected = ' selected="selected"';
                                                }
                                            }
                                            $html[] = '<option' . $selected . ' value="' . esc_attr($key) . '">' . esc_html($option) . '</option>';
                                        }
                                    }
                                    $html[] = '</select>';
                                    break;
                                    // Basic authorization facebook button
                                // Basic authorization facebook button
                                case 'authorization_button':
                                    // load the facebook SDK
                                    require_once $this->cfg['paths']['scripts_dir_path'] . '/facebook/facebook.php';
                                    $fb_details = $psp->getAllSettings('array', 'facebook_planner');
                                    if (isset($fb_details['app_id']) && trim($fb_details['app_id']) != '' && (isset($fb_details['app_secret']) && trim($fb_details['app_secret']) != '')) {
                                        $facebook = new psp_Facebook(array('appId' => $fb_details['app_id'], 'secret' => $fb_details['app_secret']));
                                    }
                                    if (isset($facebook)) {
                                        $validAuth = false;
                                        $state = isset($_REQUEST['state']) ? trim($_REQUEST['state']) : '';
                                        $dbToken = get_option('psp_fb_planner_token');
                                        if (trim($dbToken) != "" && $state == "") {
                                            $facebook->setAccessToken($dbToken);
                                            try {
                                                // get user profile
                                                $user_profile = $facebook->api('/me');
                                                if (count($user_profile) > 0) {
                                                    $validAuth = true;
                                                    $html[] = '<p>This plugin is <b>authorized</b> for: <a target="_blank" href="' . $user_profile['link'] . '">' . $user_profile['name'] . '</a></p>';
                                                    // login url
                                                    $loginUrl = $facebook->getLoginUrl(array('scope' => 'email,publish_stream,manage_pages,user_groups,offline_access', 'redirect_uri' => admin_url('admin-ajax.php?action=psp_facebookAuth')));
                                                    $html[] = '<a href="' . $loginUrl . '" style="width: 133px;" class="psp-button blue">' . __('Authorize this app again', $psp->localizationName) . '</a>';
                                                }
                                            } catch (psp_FacebookApiException $e) {
                                                // clean token
                                                //update_option('psp_fb_planner_token', $token);
                                            }
                                        }
                                        if ($validAuth == false) {
                                            // login url
                                            $loginUrl = $facebook->getLoginUrl(array('scope' => 'email,publish_stream,user_groups,manage_pages,offline_access', 'redirect_uri' => admin_url('admin-ajax.php?action=psp_facebookAuth')));
                                            $html[] = '<a href="' . $loginUrl . '" style="width: 84px;" type="button" class="psp-button blue">' . __('Authorizate app', $psp->localizationName) . '</a>';
                                        }
                                    }
                                    break;
                                case 'date':
                                    $html[] = '<input ' . (isset($value['readonly']) && $value['readonly'] == true ? 'readonly' : '') . ' ' . (isset($value['force_width']) ? "style='width:" . $value['force_width'] . "px;'" : '') . ' id="' . esc_attr($elm_id) . '" name="' . esc_attr($option_name . $elm_id) . '" type="text" value="' . esc_attr($val) . '" />';
                                    $html[] = '<input type="hidden" id="' . esc_attr($elm_id) . '-format" value="" />';
                                    $defaultDate = '';
                                    if (isset($value['std']) && !empty($value['std'])) {
                                        $defaultDate = $value['std'];
                                    }
                                    if (isset($value['defaultDate']) && !empty($value['defaultDate'])) {
                                        $defaultDate = $value['defaultDate'];
                                    }
                                    $html[] = "<script type='text/javascript'>\n\t\t\t\t\t\t\t\t\t\tjQuery(document).ready(function(\$){\n\t\t\t\t\t\t\t\t\t\t \t// datepicker\n\t\t\t\t\t\t\t\t\t\t \tvar atts = {\n\t\t\t\t\t\t\t\t\t\t\t\tchangeMonth:\ttrue,\n\t\t\t\t\t\t\t\t\t\t\t\tchangeYear:\t\ttrue,\n\t\t\t\t\t\t\t\t\t\t\t\tonClose: function() {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\$('input#" . $elm_id . "').trigger('change');\n\t\t\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t\t\t\tatts.dateFormat \t= '" . (isset($value['format']) && !empty($value['format']) ? $value['format'] : 'yy-mm-dd') . "';\n\t\t\t\t\t\t\t\t\t\t\tatts.defaultDate \t= '" . (isset($defaultDate) && !empty($defaultDate) ? $defaultDate : null) . "';\n\t\t\t\t\t\t\t\t\t\t\tatts.altField\t\t= 'input#" . $elm_id . "-format';\n\t\t\t\t\t\t\t\t\t\t\tatts.altFormat\t\t= 'yy-mm-dd';";
                                    if (isset($value['yearRange']) && !empty($value['yearRange'])) {
                                        $html[] = "atts.yearRange\t= '" . $value['yearRange'] . "';";
                                    }
                                    $html[] = "\$( 'input#" . $elm_id . "' ).datepicker( atts ); // end datepicker\n\t\t\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t\t</script>";
                                    break;
                                case 'time':
                                    $__hourmin_init = array();
                                    if (isset($value['std']) && !empty($value['std'])) {
                                        $__hourmin_init = $this->getTimeDefault($value['std']);
                                    }
                                    if (isset($value['defaultDate']) && !empty($value['defaultDate'])) {
                                        $__hourmin_init = $this->getTimeDefault($value['defaultDate']);
                                    }
                                    $__hour_range = array();
                                    if (isset($value['hour_range']) && !empty($value['hour_range'])) {
                                        $__hour_range = $this->getTimeDefault($value['hour_range']);
                                    }
                                    $__min_range = array();
                                    if (isset($value['min_range']) && !empty($value['min_range'])) {
                                        $__min_range = $this->getTimeDefault($value['min_range']);
                                    }
                                    $html[] = '<input ' . (isset($value['readonly']) && $value['readonly'] == true ? 'readonly' : '') . ' ' . (isset($value['force_width']) ? "style='width:" . $value['force_width'] . "px;'" : '') . ' id="' . esc_attr($elm_id) . '" name="' . esc_attr($option_name . $elm_id) . '" type="text" value="' . esc_attr($val) . '" />';
                                    $html[] = "<script type='text/javascript'>\n\t\t\t\t\t\t\t\t\t\tjQuery(document).ready(function(\$){\n\t\t\t\t\t\t\t\t\t\t \t// timepicker\n\t\t\t\t\t\t\t\t\t\t \tvar atts = {};";
                                    if (isset($value['ampm']) && ($value['ampm'] || $value['ampm'] == 'true')) {
                                        $html[] = "atts.ampm\t= true;";
                                    } else {
                                        $html[] = "atts.ampm\t= false;";
                                    }
                                    if (isset($__hourmin_init) && !empty($__hourmin_init)) {
                                        $html[] = "atts.defaultValue\t= '" . $value['std'] . "';";
                                    }
                                    if (isset($__hourmin_init) && !empty($__hourmin_init)) {
                                        $html[] = "atts.hour\t= " . $__hourmin_init[0] . ";";
                                    }
                                    if (isset($__hourmin_init) && !empty($__hourmin_init)) {
                                        $html[] = "atts.minute\t= " . $__hourmin_init[1] . ";";
                                    }
                                    if (isset($__hour_range) && !empty($__hour_range)) {
                                        $html[] = "atts.hourMin\t= " . $__hour_range[0] . ";";
                                    }
                                    if (isset($__hour_range) && !empty($__hour_range)) {
                                        $html[] = "atts.hourMax\t= " . $__hour_range[1] . ";";
                                    }
                                    if (isset($__min_range) && !empty($__min_range)) {
                                        $html[] = "atts.minuteMin\t= " . $__min_range[0] . ";";
                                    }
                                    if (isset($__min_range) && !empty($__min_range)) {
                                        $html[] = "atts.minuteMax\t= " . $__min_range[1] . ";";
                                    }
                                    $html[] = "\$( 'input#" . $elm_id . "' ).timepicker( atts ); // end timepicker\n\t\t\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t\t</script>";
                                    break;
                                case 'ratestar':
                                    $html[] = '<input id="' . esc_attr($elm_id) . '" name="' . esc_attr($option_name . $elm_id) . '" type="hidden" value="' . esc_attr($val) . '" />';
                                    $html[] = '<div id="rateit-' . esc_attr($elm_id) . '"></div>';
                                    $html[] = "<script type='text/javascript'>\n\t\t\t\t\t\t\t\t\t\t\t jQuery(document).ready(function(\$){\n\t\t\t\t\t\t\t\t\t\t\t\t\$('#rateit-" . $elm_id . "').rateit({ max: " . (isset($value['nbstars']) && !empty($value['nbstars']) ? $value['nbstars'] : 10) . ", step: 1, backingfld: '#" . $elm_id . "' });\n\t\t\t\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t\t</script>";
                                    break;
                            }
                            if (!in_array(isset($value['type']) ? $value['type'] : '', $noRowElements)) {
                                // close: .psp-form-row
                                $html[] = '</div>';
                                // close: .psp-form-item
                                $html[] = '</div>';
                            }
                        }
                    }
                    // psp-message use for status message, default it's hidden
                    $html[] = '<div class="psp-message" id="psp-status-box" style="display:none;"></div>';
                    if ($box['buttons'] == true && !is_array($box['buttons'])) {
                        // buttons for each box
                        $html[] = '<div class="psp-button-row">
							<input type="reset" value="' . __('Reset to default value', $psp->localizationName) . '" class="psp-button gray left" />
							<input type="submit" value="' . __('Save the settings', $psp->localizationName) . '" class="psp-button green psp-saveOptions" />
						</div>';
                    } elseif (is_array($box['buttons'])) {
                        // buttons for each box
                        $html[] = '<div class="psp-button-row">';
                        foreach ($box['buttons'] as $key => $value) {
                            $html[] = '<input type="submit" value="' . $value['value'] . '" class="psp-button ' . $value['color'] . ' ' . $value['action'] . '" />';
                        }
                        $html[] = '</div>';
                    }
                    if ($showForm) {
                        // close: form
                        $html[] = '</form>';
                    }
                    // close: box size div
                    $html[] = '</div>';
                    // close: .psp-panel
                    $html[] = '</div>';
                    // close: .psp-panel-content
                    $html[] = '</div>';
                }
            }
            // return the $html
            return implode("\n", $html);
        }
Пример #2
0
 public function fbAuth()
 {
     $facebook = new psp_Facebook(array('appId' => $this->fb_details['app_id'], 'secret' => $this->fb_details['app_secret']));
     $state = isset($_REQUEST['state']) ? trim($_REQUEST['state']) : '';
     // check if redirect from facebook to page
     $token = $facebook->getAccessToken();
     if (trim($token) != "" && trim($state) != "") {
         // saving offline session into DB
         update_option('psp_fb_planner_token', $token);
         // get user profile
         $user_accounts = $facebook->api('me/accounts');
         $userPages = array();
         foreach ($user_accounts['data'] as $key => $value) {
             if ($value['category'] != 'Application') {
                 $__key = (string) $value['id'];
                 $userPages['pages']["{$__key}"] = $value;
             }
         }
         // get user profile
         $user_groups = $facebook->api('me/groups');
         foreach ($user_groups['data'] as $key => $value) {
             $__key = (string) $value['id'];
             $userPages['groups']["{$__key}"] = $value;
         }
         if (count($userPages) > 0) {
             update_option('psp_fb_planner_user_pages', json_encode($userPages));
             header('location: ' . admin_url('admin.php?page=psp#facebook_planner'));
             exit;
         }
     }
 }