/**
 * Write to the Genesis Extender Custom Functions file if it exists.
 *
 * @since 1.0
 *
 */
function genesis_extender_write_custom_functions($code = '')
{
    genesis_extender_folders_open_permissions();
    if (!file_exists(genesis_extender_get_custom_functions_path())) {
        genesis_extender_create_custom_functions_file();
    }
    $handle = @fopen(genesis_extender_get_custom_functions_path(), 'w+');
    @fwrite($handle, stripslashes($code));
    @fclose($handle);
    genesis_extender_folders_close_permissions();
}
/**
 * Perform Genesis Extender activation actions.
 *
 * @since 1.0
 */
function genesis_extender_activate()
{
    if (!get_option('genesis_extender_version_number')) {
        update_option('genesis_extender_version_number', '1.2.2');
    }
    if (!get_option('genesis_extender_settings')) {
        update_option('genesis_extender_settings', genesis_extender_settings_defaults());
    }
    if (!get_option('genesis_extender_custom_css')) {
        update_option('genesis_extender_custom_css', genesis_extender_custom_css_options_defaults());
    }
    if (!get_option('genesis_extender_custom_functions')) {
        update_option('genesis_extender_custom_functions', genesis_extender_custom_functions_options_defaults());
    }
    if (!get_option('genesis_extender_custom_js')) {
        update_option('genesis_extender_custom_js', genesis_extender_custom_js_options_defaults());
    }
    if (!get_option('genesis_extender_custom_templates')) {
        update_option('genesis_extender_custom_templates', array());
    }
    if (!get_option('genesis_extender_custom_labels')) {
        update_option('genesis_extender_custom_labels', array());
    }
    if (!get_option('genesis_extender_custom_conditionals')) {
        update_option('genesis_extender_custom_conditionals', array());
    }
    if (!get_option('genesis_extender_custom_widget_areas')) {
        update_option('genesis_extender_custom_widget_areas', array());
    }
    if (!get_option('genesis_extender_custom_hook_boxes')) {
        update_option('genesis_extender_custom_hook_boxes', array());
    }
    if (!is_dir(CHILD_DIR . '/my-templates')) {
        mkdir(CHILD_DIR . '/my-templates');
        @chmod(CHILD_DIR . '/my-templates', 0755);
    }
    if (!is_dir(genesis_extender_get_stylesheet_location('path', $root = true))) {
        mkdir(genesis_extender_get_stylesheet_location('path', $root = true));
        @chmod(genesis_extender_get_stylesheet_location('path', $root = true), 0755);
    }
    if (!is_dir(genesis_extender_get_stylesheet_location('path', $root = true) . 'plugin')) {
        mkdir(genesis_extender_get_stylesheet_location('path', $root = true) . 'plugin');
        @chmod(genesis_extender_get_stylesheet_location('path', $root = true) . 'plugin', 0755);
    }
    if (!is_dir(genesis_extender_get_stylesheet_location('path') . 'default-images')) {
        mkdir(genesis_extender_get_stylesheet_location('path') . 'default-images');
        @chmod(genesis_extender_get_stylesheet_location('path') . 'default-images', 0755);
        mkdir(genesis_extender_get_stylesheet_location('path') . 'default-images/post-formats');
        @chmod(genesis_extender_get_stylesheet_location('path') . 'default-images/post-formats', 0755);
        $handle = opendir(GENEXT_PATH . 'images');
        while (false !== ($file = readdir($handle))) {
            $ext = strtolower(substr(strrchr($file, '.'), 1));
            if ($ext == 'jpg' || $ext == 'gif' || $ext == 'png') {
                copy(GENEXT_PATH . 'images/' . $file, genesis_extender_get_stylesheet_location('path') . 'default-images/' . $file);
            }
        }
        closedir($handle);
        $handle2 = opendir(GENEXT_PATH . 'images/post-formats');
        while (false !== ($file = readdir($handle2))) {
            $ext = strtolower(substr(strrchr($file, '.'), 1));
            if ($ext == 'jpg' || $ext == 'gif' || $ext == 'png') {
                copy(GENEXT_PATH . 'images/post-formats/' . $file, genesis_extender_get_stylesheet_location('path') . 'default-images/post-formats/' . $file);
            }
        }
        closedir($handle2);
    }
    if (!is_dir(genesis_extender_get_stylesheet_location('path') . 'images')) {
        mkdir(genesis_extender_get_stylesheet_location('path') . 'images');
        @chmod(genesis_extender_get_stylesheet_location('path') . 'images', 0755);
    }
    if (!is_dir(genesis_extender_get_stylesheet_location('path') . 'images/adminthumbnails')) {
        mkdir(genesis_extender_get_stylesheet_location('path') . 'images/adminthumbnails');
        @chmod(genesis_extender_get_stylesheet_location('path') . 'images/adminthumbnails', 0755);
    }
    if (!is_dir(genesis_extender_get_stylesheet_location('path') . 'tmp')) {
        mkdir(genesis_extender_get_stylesheet_location('path') . 'tmp');
        @chmod(genesis_extender_get_stylesheet_location('path') . 'tmp', 0755);
    }
    if (!is_dir(genesis_extender_get_stylesheet_location('path') . 'tmp/images')) {
        mkdir(genesis_extender_get_stylesheet_location('path') . 'tmp/images');
        @chmod(genesis_extender_get_stylesheet_location('path') . 'tmp/images', 0755);
    }
    if (!is_dir(genesis_extender_get_stylesheet_location('path') . 'tmp/images/adminthumbnails')) {
        mkdir(genesis_extender_get_stylesheet_location('path') . 'tmp/images/adminthumbnails');
        @chmod(genesis_extender_get_stylesheet_location('path') . 'tmp/images/adminthumbnails', 0755);
    }
    genesis_extender_write_files();
    genesis_extender_create_custom_functions_file();
    $genesis_extender_folders = array(genesis_extender_get_stylesheet_location('path'), genesis_extender_get_stylesheet_location('path') . 'images', genesis_extender_get_stylesheet_location('path') . 'adminthumbnails', genesis_extender_get_stylesheet_location('path') . 'tmp', genesis_extender_get_stylesheet_location('path') . 'tmp/images', genesis_extender_get_stylesheet_location('path') . 'tmp/images/adminthumbnails');
    $genesis_extender_unwritable = false;
    foreach ($genesis_extender_folders as $genesis_extender_folder) {
        if (is_dir($genesis_extender_folder) && !genesis_extender_writable($genesis_extender_folder)) {
            $genesis_extender_unwritable = true;
        }
    }
    if ($genesis_extender_unwritable) {
        wp_redirect(admin_url('admin.php?page=genesis-extender-settings&notice=genesis-extender-unwritable'));
    }
}