public function display($pid)
 {
     if (!isset($this->optObj)) {
         if (!$this->loadProfileOptions()) {
             return '';
         }
     }
     erpPROPaths::requireOnce(erpPROPaths::$erpPROTheme);
     if (!is_int($pid) || erpPROTheme::areOthersSuppressed()) {
         $this->setSuppressOthers();
         return '';
     }
     erpPROPaths::requireOnce(erpPROPaths::$VPluginThemeFactory);
     erpPROPaths::requireOnce(erpPROPaths::$erpProRelated);
     $relatedObj = erpProRelated::get_instance($this->optObj);
     $result = $relatedObj->getRelated($pid);
     $ratings = $relatedObj->getRatingsFromRelDataObj();
     if (empty($result) || empty($result->posts)) {
         return '';
     }
     VPluginThemeFactory::registerThemeInPathRecursive(erpPROPaths::getAbsPath(erpPROPaths::$scThemesFolder), $this->optObj->getDsplLayout());
     $theme = VPluginThemeFactory::getThemeByName($this->optObj->getDsplLayout(), 'shortcode');
     if (!$theme) {
         return '';
     }
     $theme->setOptions($this->optObj->getOptions());
     $theme->formPostData($result, $this->optObj, $ratings);
     $relContent = $theme->render();
     $this->setSuppressOthers();
     return $relContent;
 }
 public function saveOptions($newOptions)
 {
     // Validate template options
     $dsplLayout = isset($newOptions['dsplLayout']) ? $newOptions['dsplLayout'] : 'Grid';
     erpPROPaths::requireOnce(erpPROPaths::$VPluginThemeFactory);
     VPluginThemeFactory::registerThemeInPathRecursive(erpPROPaths::getAbsPath(erpPROPaths::$scThemesFolder), $dsplLayout);
     $theme = VPluginThemeFactory::getThemeByName($dsplLayout);
     $templateOptions = array();
     if ($theme) {
         $templateOptions = $theme->saveSettings($newOptions);
         foreach ($theme->getDefOptions() as $key => $value) {
             unset($newOptions[$key]);
         }
     }
     $this->options = $templateOptions + $this->validateCommonOptions($newOptions) + $this->validateShortcodeOptions($newOptions);
     if (isset($this->profileName)) {
         $this->profiles[$this->profileName] = array_merge(erpPRODefaults::$comOpts + erpPRODefaults::$shortCodeOpts, $this->options);
         return update_option(self::$shortCodeProfilesArrayName, $this->profiles);
     } else {
         return array_merge(erpPRODefaults::$comOpts + erpPRODefaults::$shortCodeOpts, $this->options);
     }
 }
foreach ($templates as $key => $val) {
    echo '<option value="' . $val . '"' . selected($options['dsplLayout'], $val, FALSE) . '>' . $val . '</option>';
}
?>
            </select>
        </td>
    </tr>
</table>
<p class="wid-inst-<?php 
echo $widgetInstance->get_field_id('dsplLayout');
?>
" style="border: 1px solid lightblue;padding: 10px;border-radius: 5px;">
    <span style="position: relative; top: -21px; float: left; background-color: white;"> Theme options </span><br>
    <?php 
foreach ($templates as $key => $value) {
    $temp = VPluginThemeFactory::getThemeByName($value);
    echo '<span class="templateSettings" data-template="' . $value . '" hidden="hidden">';
    $temp->setOptions($options);
    echo $temp->renderSettings($widgetInstance);
    echo '</span>';
}
?>

</p>
<script type="text/javascript">
    var templateRoot = "<?php 
echo erpPaths::getAbsPath(erpPaths::$widgetThemesFolder);
?>
";
    jQuery(document).ready(function($) {
 private static function delTemplateOptions()
 {
     erpPaths::requireOnce(erpPaths::$VPluginThemeFactory);
     VPluginThemeFactory::registerThemeInPathRecursive(erpPaths::getAbsPath(erpPaths::$mainThemesFolder));
     $themes = VPluginThemeFactory::getRegisteredThemes();
     foreach ((array) $themes as $key => $value) {
         delete_option($value->getOptionsArrayName());
     }
 }
                    </tr>
                    <tr>
                        <td>
                            <label for="dsplLayout">Theme :</label>
                        </td>
                        <td>
                            <select 
                                class="dsplLayout"  
                                name="dsplLayout"
                                data-tooltip 
                                title="From the dropdown you can define the appearance of the plugin in the main content area. When a theme is selected the additional options will show up bellow theme selection dropdown"
                                >
                                    <?php 
erpPROPaths::requireOnce(erpPROPaths::$VPluginThemeFactory);
VPluginThemeFactory::registerThemeInPathRecursive(erpPROPaths::getAbsPath(erpPROPaths::$scThemesFolder));
$templates = VPluginThemeFactory::getThemesNames();
foreach ($templates as $key => $val) {
    echo '<option value="' . $val . '"' . selected($erpPROOptions['dsplLayout'], $val, FALSE) . '>' . $val . '</option>';
}
?>
                            </select>
                        </td>
                    </tr>
                </table>
                <div class="templateSettings"></div>
            </div>
        </form>
    </div>
    <!-- 	</form> -->
    <script type="text/javascript">
        (function($) {
 /**
  * Sanitize widget form values as they are saved.
  *
  * @see WP_Widget::update()
  *
  * @param array $new_instance
  *        	Values just sent to be saved.
  * @param array $old_instance
  *        	Previously saved values from database.
  * @return array Updated safe values to be saved.
  * @since 1.0.0
  * @author Panagiotis Vagenas <*****@*****.**>
  */
 public function update($new_instance, $old_instance)
 {
     /* #? Verify nonce */
     if (!isset($_POST['erpPRO_meta_box_nonce']) || !wp_verify_nonce($_POST['erpPRO_meta_box_nonce'], 'erpPRO_meta_box_nonce')) {
         return;
     }
     erpPROPaths::requireOnce(erpPROPaths::$erpPROWidOpts);
     // get an instance to validate options
     $widOpts = new erpPROWidOpts($old_instance);
     // validate wid options
     $widOptsValidated = $widOpts->saveOptions($new_instance, $old_instance);
     // validate template options
     if (isset($new_instance['dsplLayout'])) {
         erpPROPaths::requireOnce(erpPROPaths::$VPluginThemeFactory);
         VPluginThemeFactory::registerThemeInPathRecursive(erpPROPaths::getAbsPath(erpPROPaths::$widgetThemesFolder), $new_instance['dsplLayout']);
         $theme = VPluginThemeFactory::getThemeByName($new_instance['dsplLayout']);
         if ($theme) {
             $themeValidated = $theme->saveSettings($new_instance);
             foreach ($theme->getDefOptions() as $key => $value) {
                 unset($new_instance[$key]);
             }
         } else {
             // TODO Set notices class
             //                $message = new WP_Error_Notice('Theme ' . $new_instance ['dsplLayout'] . ' not found. Theme options discarded');
             //                WP_Admin_Notices::getInstance()->addNotice($message);
         }
     }
     // save updated options
     return $widOptsValidated + $themeValidated;
 }
 /**
  * This is called through ajax hook and returns the plugin options as defined in template settings file
  *
  * @author Panagiotis Vagenas <*****@*****.**>
  * @since 2.0.0
  */
 public function loadTemplateOptions()
 {
     if (!isset($_POST['template']) || !isset($_POST['templateRoot'])) {
         echo json_encode(false);
         die;
     }
     erpPaths::requireOnce(erpPaths::$VPluginThemeFactory);
     VPluginThemeFactory::registerThemeInPathRecursive(erpPaths::getAbsPath(erpPaths::$mainThemesFolder), $_POST['template']);
     $theme = VPluginThemeFactory::getThemeByName($_POST['template']);
     $data = array();
     if ($theme) {
         $data = array('content' => $theme->renderSettings('', false), 'optionValues' => $theme->getOptions());
     }
     echo json_encode($data);
     die;
 }
 /**
  * This is called through ajax hook and returns the plugin options as defined in template settings file
  *
  * @author Panagiotis Vagenas <*****@*****.**>
  * @since 1.0.0
  */
 public function loadSCTemplateOptions()
 {
     if (!isset($_POST['template'])) {
         echo json_encode(false);
         die;
     }
     erpPROPaths::requireOnce(erpPROPaths::$VPluginThemeFactory);
     if (isset($_POST['profileName'])) {
         $profileName = $_POST['profileName'];
         erpPROPaths::requireOnce(erpPROPaths::$erpPROShortCodeOpts);
         $profilesOptionsArray = get_option(erpPROShortCodeOpts::$shortCodeProfilesArrayName);
         $profileOpts = isset($profilesOptionsArray[$profileName]) ? $profilesOptionsArray[$profileName] : null;
     }
     if (empty($profileOpts)) {
         $profileOpts = erpPRODefaults::$comOpts + erpPRODefaults::$shortCodeOpts;
         $profileName = 'default';
     }
     VPluginThemeFactory::registerThemeInPathRecursive(erpPROPaths::getAbsPath(erpPROPaths::$scThemesFolder), $_POST['template']);
     $templateObj = VPluginThemeFactory::getThemeByName($_POST['template']);
     if (!$templateObj) {
         echo json_encode(false);
         die;
     }
     $templateObj->setOptions($profileOpts);
     $data = array('content' => $templateObj->renderSettings('', FALSE), 'optionValues' => $profilesOptionsArray);
     echo json_encode($data);
     die;
 }
 /**
  * Decides if content should be modified, if yes calls content modifier
  *
  * @param string $content
  * @return string
  * @author Panagiotis Vagenas <*****@*****.**>
  * @since 1.0.0
  */
 public function contentFilter($content)
 {
     global $post;
     /**
      * Check if is time to take action
      */
     if ($this->isShowTime($post) && !$this->isInExcludedPostTypes($post) && !$this->isInExcludedTaxonomies($post) && (bool) $this->mainOpts->getValue('activate')) {
         erpPROPaths::requireOnce(erpPROPaths::$erpPROTheme);
         if (erpPROTheme::areOthersSuppressed()) {
             return $content;
         }
         erpPROPaths::requireOnce(erpPROPaths::$VPluginThemeFactory);
         erpPROPaths::requireOnce(erpPROPaths::$erpProRelated);
         $relatedObj = erpProRelated::get_instance($this->mainOpts);
         $result = $relatedObj->getRelated($post->ID);
         $ratings = $relatedObj->getRatingsFromRelDataObj();
         if (empty($result) || empty($result->posts)) {
             return $content;
         }
         VPluginThemeFactory::registerThemeInPathRecursive(erpPROPaths::getAbsPath(erpPROPaths::$mainThemesFolder), $this->mainOpts->getDsplLayout());
         $theme = VPluginThemeFactory::getThemeByName($this->mainOpts->getDsplLayout(), 'main');
         if (!$theme) {
             return $content;
         }
         $theme->formPostData($result, $this->mainOpts, $ratings);
         $relContent = $theme->render();
         return $this->mainOpts->getPosition() == 'top' ? $relContent . $content : $content . $relContent;
     }
     return $content;
 }