public function outputHeader()
    {
        ?>
        <div class="wrap">
            <table width="100%">
                <tbody>
                <tr>
                    <td align="left"><h2><?php 
        _e('Import / Export', 'add-actions-and-filters');
        ?>
</h2></td>
                    <td align="right">
                        <a href="<?php 
        echo $this->plugin->getAdminPageUrl();
        ?>
">
                            <img width="128" height="50"
                                 src="<?php 
        echo $this->plugin->getPluginFileUrl('img/icon-256x100.png');
        ?>
">
                        </a>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
        <?php 
    }
 /**
  * Wrapper function for the shortcode code.
  * @param $atts array shortcode attributes
  * @param null|string $content shortcode content
  * @return string
  */
 public function handle_shortcode($atts, $content = null)
 {
     if (isset($this->codeItem['capability']) && $this->codeItem['capability'] && !current_user_can($this->codeItem['capability'])) {
         // if a capability is required and the user doesn't have it,
         // then let the short code do nothing.
         return '';
     }
     $buffer = isset($this->codeItem['buffer']) && $this->codeItem['buffer'];
     if ($buffer) {
         ob_start();
     }
     $result = eval($this->codeItem['code']);
     if ($result === FALSE) {
         $url = $this->plugin->getAdminPageUrl() . "&id={$this->codeItem['id']}&action=edit";
         $result = sprintf("<p>%s Plugin: Error in shortcode [<u><a href='%s' target='_blank'>%s</a></u>]</p>", $this->plugin->getPluginDisplayName(), $url, $this->codeItem['name']);
     }
     if ($buffer) {
         $output = ob_get_contents();
         ob_end_clean();
     } else {
         $output = '';
     }
     return $output . $result;
 }
 /**
  * @param $codeItem array
  */
 public function printErrorMessage($codeItem, $msg = null)
 {
     echo '<div style="background-color: white; text-decoration-color: black">';
     echo '<p>&nbsp;</p>';
     // helps prevent header from covering first line of output
     $url = $this->plugin->getAdminPageUrl() . "&id={$codeItem['id']}&action=edit";
     $name = $codeItem['name'] ? $codeItem['name'] : '(unamed)';
     echo '<p>';
     printf("%s Plugin: Error in user-provided code item named \"%s\". ", $this->plugin->getPluginDisplayName(), $name);
     echo '<br/>';
     if ($msg) {
         echo $msg;
         echo '<br/>';
     }
     printf("<u><a href='%s' target='_blank'>%s</a></u>", $url, __('Fix the code here', 'add-actions-and-filters'));
     echo '</p>';
     echo '<p>&nbsp;</p>';
     echo '</div>';
 }
 public function importScepShortCodes()
 {
     $dataModel = new AddActionsAndFilters_DataModel($this->plugin, null);
     foreach ($_REQUEST as $key => $value) {
         if ($value == 'true') {
             $shortCode = array();
             $shortCode['shortcode'] = true;
             $shortCode['name'] = $key;
             $shortCode['description'] = get_option("scep_description_{$key}");
             $shortCode['enabled'] = get_option("scep_enabled_{$key}");
             $shortCode['buffer'] = get_option("scep_buffer_{$key}");
             $shortCode['code'] = get_option("scep_phpcode_{$key}");
             $shortCode['inadmin'] = 0;
             $shortCode['capability'] = '';
             //$buffer = get_option("scep_buffer_$key");
             //$param = get_option("scep_param_$key");
             $id = $dataModel->saveItem($shortCode);
             $url = $this->plugin->getAdminPageUrl() . "&id={$id}&action=edit";
             echo __('Imported', 'add-actions-and-filters') . " <a target='_blank' href='{$url}'>{$key}</a></br>";
             // Deactivate SCEP shortcode
             update_option("scep_enabled_{$key}", 0);
         }
     }
 }
    public function display()
    {
        $optionMetaData = $this->plugin->getOptionMetaData();
        // Save Posted Options
        if ($optionMetaData != null) {
            foreach ($optionMetaData as $aOptionKey => $aOptionMeta) {
                if (isset($_POST[$aOptionKey])) {
                    $this->plugin->updateOption($aOptionKey, $_POST[$aOptionKey]);
                }
            }
        }
        // HTML for the page
        $settingsGroup = get_class($this->plugin) . '-settings-group';
        ?>
        <style type="text/css">
            table.asaf-options-table {
                width: 100%
            }

            table.asaf-options-table tr:nth-child(even) {
                background: #f9f9f9
            }

            table.asaf-options-table tr:nth-child(odd) {
                background: #FFF
            }

            table.asaf-options-table td {
                width: 350px
            }

            table.asaf-options-table td + td {
                width: auto
            }
        </style>
        <div class="wrap">
            <table width="100%">
                <tbody>
                <tr>
                    <td align="left"><h2><?php 
        _e('System Settings', 'add-actions-and-filters');
        ?>
</h2></td>
                    <td align="right">
                        <a href="<?php 
        echo $this->plugin->getAdminPageUrl();
        ?>
">
                        <img width="128" height="50"
                             src="<?php 
        echo $this->plugin->getPluginFileUrl('img/icon-256x100.png');
        ?>
">
                        </a>
                    </td>
                </tr>
                </tbody>
            </table>

            <table class="asaf-options-table">
                <tbody>
                <tr>
                    <td><?php 
        _e('System', 'add-actions-and-filters');
        ?>
</td>
                    <td><?php 
        echo php_uname();
        ?>
</td>
                </tr>
                <tr>
                    <td><?php 
        _e('PHP Version', 'add-actions-and-filters');
        ?>
</td>
                    <td><?php 
        echo phpversion();
        ?>
                        <?php 
        if (version_compare('5.2', phpversion()) > 0) {
            echo '&nbsp;&nbsp;&nbsp;<span style="background-color: #ffcc00;">';
            _e('(WARNING: This plugin may not work properly with versions earlier than PHP 5.2)', 'add-actions-and-filters');
            echo '</span>';
        }
        ?>
                    </td>
                </tr>
                <tr>
                    <td><?php 
        _e('MySQL Version', 'add-actions-and-filters');
        ?>
</td>
                    <td><?php 
        echo $this->getMySqlVersion();
        ?>
                        <?php 
        echo '&nbsp;&nbsp;&nbsp;<span style="background-color: #ffcc00;">';
        if (version_compare('5.0', $this->getMySqlVersion()) > 0) {
            _e('(WARNING: This plugin may not work properly with versions earlier than MySQL 5.0)', 'add-actions-and-filters');
        }
        echo '</span>';
        ?>
                    </td>
                </tr>
                </tbody>
            </table>

            <h2><?php 
        echo $this->plugin->getPluginDisplayName();
        echo ' ';
        _e('Settings', 'add-actions-and-filters');
        ?>
</h2>

            <form method="post" action="">
                <?php 
        settings_fields($settingsGroup);
        ?>
                <table class="asaf-options-table">
                    <tbody>
                    <?php 
        if ($optionMetaData != null) {
            foreach ($optionMetaData as $aOptionKey => $aOptionMeta) {
                $displayText = is_array($aOptionMeta) ? $aOptionMeta[0] : $aOptionMeta;
                ?>
                            <tr valign="top">
                                <th scope="row"><p><label
                                            for="<?php 
                echo $aOptionKey;
                ?>
"><?php 
                echo $displayText;
                ?>
</label></p></th>
                                <td>
                                    <?php 
                $this->createFormControl($aOptionKey, $aOptionMeta, $this->plugin->getOption($aOptionKey));
                ?>
                                </td>
                            </tr>
                            <?php 
            }
        }
        ?>
                    </tbody>
                </table>
                <p class="submit">
                    <input type="submit" class="button-primary"
                           value="<?php 
        _e('Save Settings', 'add-actions-and-filters');
        ?>
"/>
                </p>
            </form>
        </div>
        <?php 
    }
    /**
     * Output the main contents of the page including the code editor and metadata fields
     * @param $item array created by AddActionsAndFilters_DataModel
     */
    public function outputCodeEditor($item)
    {
        ?>
        <div class="wrap">

            <table width="100%">
                <tbody>
                <tr>
                    <td valign="top">
                        <label for="name"><?php 
        _e('Name');
        ?>
</label>
                    </td>
                    <td valign="top" nowrap>
                        <span id="sc_info_open" style="display: none;">[</span>
                        <input id="name" type="text" value="<?php 
        echo isset($item['name']) ? $item['name'] : '';
        ?>
" size="25"/>
                        <span id="sc_info_close" style="display: none;">]</span>
                    </td>
                    <td valign="top">
                        <label for="description"><?php 
        _e('Description');
        ?>
</label>
                    </td>
                    <td valign="top">
                        <textarea title="description" id="description"
                                  cols="80"><?php 
        echo isset($item['description']) ? $item['description'] : '';
        ?>
</textarea>
                    </td>
                </tr>
                <tr>
                    <td valign="top" colspan="2">
                        <input type="checkbox" id="activated" name="activated"
                               value="true" <?php 
        if (isset($item['enabled']) && $item['enabled']) {
            echo 'checked';
        }
        ?>
>
                        <label for="enabled"><?php 
        _e('Activated');
        ?>
</label>
                        &nbsp;&nbsp;&nbsp;&nbsp;
                        <input type="checkbox" id="shortcode" name="shortcode"
                               value="true" <?php 
        if (isset($item['shortcode']) && $item['shortcode']) {
            echo 'checked';
        }
        ?>
>
                        <label for="shortcode"><?php 
        _e('Shortcode');
        ?>
</label>
                    </td>
                    <td valign="top">
                        <label for="capability"><?php 
        _e('Execute only for', 'add-actions-and-filters');
        ?>
</label>
                    </td>
                    <td valign="top">
                        <select id="capability">
                            <option value=""></option>
                            <?php 
        $cap_found = false;
        foreach ($this->getCapabilityToRolesList() as $cap => $roles) {
            $roles = implode(', ', $roles);
            $selected = '';
            if (isset($item['capability']) && $cap == $item['capability']) {
                $selected = 'selected';
                $cap_found = true;
            }
            echo "<option value=\"{$cap}\" {$selected}>{$cap} ({$roles})</option>";
        }
        if (isset($item['capability']) && !$cap_found) {
            echo "<option value=\"{$item['capability']}\" selected>{$item['capability']}</option>";
        }
        ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td colspan="3">
                    </td>
                    <td>
                        <div id="af_info_inadmin" style="display: none;">
                            <input type="checkbox" id="inadmin" name="inadmin"
                                   value="true" <?php 
        if (isset($item['inadmin']) && $item['inadmin']) {
            echo 'checked';
        }
        ?>
>
                            <label for="inadmin"><?php 
        _e('Execute also on Dashboard Pages', 'add-actions-and-filters');
        ?>
</label>
                        </div>
                        <div id="sc_info_buffer" style="display: none;">
                            <input type="checkbox" id="buffer" name="buffer"
                                   value="true" <?php 
        if (!isset($item['buffer']) || isset($item['buffer']) && $item['buffer']) {
            echo 'checked';
        }
        ?>
>
                            <label for="buffer"><?php 
        _e('Code echoes output');
        ?>
</label>
                        </div>
                    </td>
                </tr>
                </tbody>
            </table>


            <div id="sc_info_instructions_open" style="display: none;">
                <code>function handle_shortcode ( $atts, $content = null ) {</code><br/>
            </div>
            <textarea title="code" id="code"><?php 
        echo isset($item['code']) ? $item['code'] : '';
        ?>
</textarea>
            <div id="sc_info_instructions_close" style="display: none;">
                <code>}</code>
            </div>

            <script>
                var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
                    lineNumbers: true,
                    matchBrackets: true,
                    mode: "text/x-php",
                    indentUnit: 4,
                    indentWithTabs: true
                });
            </script>


            <div id="codesavestatus">&nbsp;</div>
            <?php 
        submit_button('Save', 'primary', 'savecode');
        ?>
            <script>
                jQuery(document).ready(function () {
                    jQuery('#savecode').click(function () {
                        var item = {
                            <?php 
        if (isset($item['id'])) {
            echo '"id": ' . $item['id'] . ',';
        }
        ?>
                            "name": jQuery('#name').val(),
                            "description": jQuery('#description').val(),
                            "enabled": jQuery('#activated').is(':checked'),
                            "shortcode": jQuery('#shortcode').is(':checked'),
                            "buffer": jQuery('#buffer').is(':checked'),
                            "inadmin": jQuery('#inadmin').is(':checked'),
                            "capability": jQuery('#capability').val(),
                            "code": editor.getValue()
                        };
                        jQuery.ajax(
                            {
                                "url": "<?php 
        echo admin_url('admin-ajax.php');
        ?>
?action=addactionsandfilters_save",
                                "type": "POST",
                                "data": item,
                                "success": function (data, textStatus) {
                                    window.location.replace('<?php 
        echo $this->plugin->getAdminPageUrl();
        ?>
&id=' + data + '&action=edit');
                                },
                                "error": function (textStatus, errorThrown) {
                                    jQuery("#codesavestatus").html(textStatus.statusText);
                                    console.log(textStatus);
                                    console.log(errorThrown);
                                },
                                "beforeSend": function () {
                                    jQuery("#codesavestatus").html('<img src="<?php 
        echo plugins_url('img/load.gif', __FILE__);
        ?>
">');
                                }
                            }
                        );
                    })
                });
            </script>

            <div id="sc_info_shortcode_instructions" style="display: none;">
                <table width="350px">
                    <tbody>
                    <tr>
                        <td><code>[shortcode arg="value"]</code></td>
                        <td><code>$atts['arg']</code></td>
                    </tr>
                    <tr>
                        <td><code>[shortcode]content[/shortcode]</code></td>
                        <td><code>$content</code></td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <div id="af_info_instructions" style="display: none;">

            <?php 
        $action_example = 'function email_friends( $post_ID ) {
    $friends = \'bob@example.org, susie@example.org\';
    wp_mail( $friends, "sally\'s blog updated", \'I just put something on my blog: http://blog.example.com\' );
    return $post_ID;
}
add_action( \'publish_post\', \'email_friends\' );';
        $filter_example = 'function prefix_title( $title, $id = null ) {
    return "The title is: $title";
}
add_filter( \'the_title\', \'prefix_title\', 10, 2 );';
        ?>
            <table>
                <tbody>
                <tr>
                    <td><h3 style="margin:0">Add Action</h3></td>
                    <td><a href="https://codex.wordpress.org/Function_Reference/add_action" target="_blank">add_action(
                            $hook, $function_to_add, $priority, $accepted_args );</a></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <pre><code><?php 
        echo $action_example;
        ?>
</code></pre>
                    </td>
                </tr>
                <tr>
                    <td><h3 style="margin:0">Add Filter</h3></td>
                    <td><a href="https://codex.wordpress.org/Function_Reference/add_filter" target="_blank">add_filter(
                            $tag, $function_to_add, $priority, $accepted_args );</a></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <pre><code><?php 
        echo $filter_example;
        ?>
</code></pre>
                    </td>
                </tr>
                </tbody>
            </table>

        </div>

        <script>
            function displayShortCodeToggle() {
                if (jQuery('#shortcode').is(':checked')) {
                    jQuery('[id^=sc_info]').show();
                    jQuery('[id^=af_info]').hide();
                } else {
                    jQuery('[id^=sc_info]').hide();
                    jQuery('[id^=af_info]').show();
                }
            }
            displayShortCodeToggle();

            jQuery('#shortcode').click(function () {
                displayShortCodeToggle();
            });
            jQuery('#name').keyup(function () {
                displayShortCodeToggle();
            });
        </script>

        <?php 
    }