| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /** |
|---|
| 4 | * Post Fields - A plugin to display additional fields on the publish page |
|---|
| 5 | * @package postfields |
|---|
| 6 | **/ |
|---|
| 7 | class postfields extends Plugin |
|---|
| 8 | { |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | const VERSION= '1.1'; |
|---|
| 12 | |
|---|
| 13 | /** |
|---|
| 14 | * Add update beacon support |
|---|
| 15 | **/ |
|---|
| 16 | public function action_update_check() |
|---|
| 17 | { |
|---|
| 18 | Update::add( 'Postfields', '228D6060-38F0-11DD-AE16-0800200C9A66', $this->info->version ); |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | /** |
|---|
| 23 | * Add actions to the plugin page for this plugin |
|---|
| 24 | * |
|---|
| 25 | * @param array $actions An array of actions that apply to this plugin |
|---|
| 26 | * @param string $plugin_id The string id of a plugin, generated by the system |
|---|
| 27 | * @return array The array of actions to attach to the specified $plugin_id |
|---|
| 28 | */ |
|---|
| 29 | public function filter_plugin_config($actions, $plugin_id) |
|---|
| 30 | { |
|---|
| 31 | if ($plugin_id == $this->plugin_id()){ |
|---|
| 32 | $types = Post::list_active_post_types(); |
|---|
| 33 | unset($types['any']); |
|---|
| 34 | foreach($types as $type => $id) { |
|---|
| 35 | $actions['config-' . $id] = _t('%s fields', array($type)); |
|---|
| 36 | } |
|---|
| 37 | $actions['plugin'] = _t('Build Plugin'); |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | return $actions; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * Respond to the user selecting an action on the plugin page |
|---|
| 45 | * |
|---|
| 46 | * @param string $plugin_id The string id of the acted-upon plugin |
|---|
| 47 | * @param string $action The action string supplied via the filter_plugin_config hook |
|---|
| 48 | */ |
|---|
| 49 | public function action_plugin_ui($plugin_id, $action) |
|---|
| 50 | { |
|---|
| 51 | if ($plugin_id == $this->plugin_id()) { |
|---|
| 52 | switch($action) { |
|---|
| 53 | case 'plugin': |
|---|
| 54 | |
|---|
| 55 | $ui = new FormUI('postfields'); |
|---|
| 56 | $ui->append('static', 'typelabel', _t('Add this code to a plugin to implement the currently configured fields.')); |
|---|
| 57 | $ui->append('textarea', 'plugincode', 'null:null', _t('Plugin code:'))->value = $this->get_code(); |
|---|
| 58 | $ui->out(); |
|---|
| 59 | |
|---|
| 60 | break; |
|---|
| 61 | |
|---|
| 62 | default: |
|---|
| 63 | $types = array_flip(Post::list_active_post_types()); |
|---|
| 64 | $key = substr($action, 7); |
|---|
| 65 | |
|---|
| 66 | $ui = new FormUI('postfields'); |
|---|
| 67 | $ui->append('static', 'typelabel', _t('Adding fields to the "%s" post type.', array($types[$key]))); |
|---|
| 68 | $ui->append('textmulti', 'fields', 'postfields__fields_' . $key, 'Additional Fields:'); |
|---|
| 69 | $ui->append('submit', 'submit', 'Submit'); |
|---|
| 70 | $ui->out(); |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | * Add additional controls to the publish page tab |
|---|
| 77 | * |
|---|
| 78 | * @param FormUI $form The form that is used on the publish page |
|---|
| 79 | * @param Post $post The post being edited |
|---|
| 80 | **/ |
|---|
| 81 | public function action_form_publish($form, $post) |
|---|
| 82 | { |
|---|
| 83 | $fields = Options::get('postfields__fields_' . $post->content_type); |
|---|
| 84 | if(!is_array($fields) || count($fields) == 0) { |
|---|
| 85 | return; |
|---|
| 86 | } |
|---|
| 87 | $output = ''; |
|---|
| 88 | $control_id = 0; |
|---|
| 89 | $postfields = $form->publish_controls->append('fieldset', 'postfields', 'Additional Fields'); |
|---|
| 90 | foreach($fields as $field) { |
|---|
| 91 | $control_id = md5($field); |
|---|
| 92 | $fieldname = "postfield_{$control_id}"; |
|---|
| 93 | $customfield = $postfields->append('text', $fieldname, 'null:null', $field); |
|---|
| 94 | $customfield->value = isset($post->info->{$field}) ? $post->info->{$field} : ''; |
|---|
| 95 | $customfield->template = 'tabcontrol_text'; |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | /** |
|---|
| 101 | * Modify a post before it is updated |
|---|
| 102 | * |
|---|
| 103 | * @param Post $post The post being saved, by reference |
|---|
| 104 | * @param FormUI $form The form that was submitted on the publish page |
|---|
| 105 | */ |
|---|
| 106 | public function action_publish_post($post, $form) |
|---|
| 107 | { |
|---|
| 108 | $fields = Options::get('postfields__fields_' . $post->content_type); |
|---|
| 109 | if(!is_array($fields) || count($fields) == 0) { |
|---|
| 110 | return; |
|---|
| 111 | } |
|---|
| 112 | foreach($fields as $field) { |
|---|
| 113 | $control_id = md5($field); |
|---|
| 114 | $fieldname = "postfield_{$control_id}"; |
|---|
| 115 | $customfield = $form->$fieldname; |
|---|
| 116 | $post->info->{$field} = $customfield->value; |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | public function get_code() |
|---|
| 121 | { |
|---|
| 122 | $cases_form = ''; |
|---|
| 123 | |
|---|
| 124 | $types = Post::list_active_post_types(); |
|---|
| 125 | unset($types['any']); |
|---|
| 126 | foreach($types as $type => $id) { |
|---|
| 127 | $fields = Options::get('postfields__fields_' . $id); |
|---|
| 128 | if(!is_array($fields) || count($fields) == 0) { |
|---|
| 129 | continue; |
|---|
| 130 | } |
|---|
| 131 | $fieldlist = array(); |
|---|
| 132 | foreach($fields as $field) { |
|---|
| 133 | $fieldlist[] = "'" . addslashes($field) . "'"; |
|---|
| 134 | } |
|---|
| 135 | $fieldlist = implode(', ', $fieldlist); |
|---|
| 136 | $cases_form .= "\t\t\tcase {$id}:\n\t\t\t\t\$fields = array({$fieldlist});\n\t\t\t\tbreak;\n"; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | $code = <<< PLUGIN_CODE_1 |
|---|
| 140 | |
|---|
| 141 | /** |
|---|
| 142 | * Add additional controls to the publish page tab |
|---|
| 143 | * |
|---|
| 144 | * @param FormUI \$form The form that is used on the publish page |
|---|
| 145 | * @param Post \$post The post being edited |
|---|
| 146 | **/ |
|---|
| 147 | public function action_form_publish(\$form, \$post) |
|---|
| 148 | { |
|---|
| 149 | switch(\$post->content_type) { |
|---|
| 150 | {$cases_form} |
|---|
| 151 | default: |
|---|
| 152 | return; |
|---|
| 153 | } |
|---|
| 154 | foreach(\$fields as \$field) { |
|---|
| 155 | \$control_id = md5(\$field); |
|---|
| 156 | \$fieldname = "postfield_{\$control_id}"; |
|---|
| 157 | \$customfield = \$postfields->append('text', \$fieldname, 'null:null', \$field); |
|---|
| 158 | \$customfield->value = isset(\$post->info->{\$field}) ? \$post->info->{\$field} : ''; |
|---|
| 159 | \$customfield->template = 'tabcontrol_text'; |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | /** |
|---|
| 165 | * Modify a post before it is updated |
|---|
| 166 | * |
|---|
| 167 | * @param Post \$post The post being saved, by reference |
|---|
| 168 | * @param FormUI \$form The form that was submitted on the publish page |
|---|
| 169 | */ |
|---|
| 170 | public function action_publish_post(\$post, \$form) |
|---|
| 171 | { |
|---|
| 172 | switch(\$post->content_type) { |
|---|
| 173 | {$cases_form} |
|---|
| 174 | default: |
|---|
| 175 | return; |
|---|
| 176 | } |
|---|
| 177 | foreach(\$fields as \$field) { |
|---|
| 178 | \$control_id = md5(\$field); |
|---|
| 179 | \$fieldname = "postfield_{\$control_id}"; |
|---|
| 180 | \$customfield = \$form->\$fieldname; |
|---|
| 181 | \$post->info->{\$field} = \$customfield->value; |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | PLUGIN_CODE_1; |
|---|
| 186 | |
|---|
| 187 | return $code; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | } |
|---|
| 194 | ?> |
|---|