<%= get_flash(@conn, level) %>
- <% end %> - <% end %> - <%= render "_side_menu.html", assigns %> @@ -25,6 +19,8 @@ + <%= flash_block(@conn) %> + <%= @inner_content %>diff --git a/apps/auth_web/lib/auth_web/templates/pow/session/new.html.eex b/apps/auth_web/lib/auth_web/templates/pow/session/new.html.eex index 04582bda..c9f48d74 100644 --- a/apps/auth_web/lib/auth_web/templates/pow/session/new.html.eex +++ b/apps/auth_web/lib/auth_web/templates/pow/session/new.html.eex @@ -23,6 +23,7 @@ <%= error_tag f, :password, class: "ui error message" %> + <%= hidden_input f, :persistent_session, value: "true" %> <%= submit "Login", class: "ui fluid large teal submit button" %> <% end %> diff --git a/apps/content/lib/content/attachment.ex b/apps/content/lib/content/attachment.ex index db1b8d50..56aed128 100644 --- a/apps/content/lib/content/attachment.ex +++ b/apps/content/lib/content/attachment.ex @@ -11,7 +11,7 @@ defmodule Content.Attachment do |> Post.metas_map deserialization_results = - meta["_wp_attachment_metadata"] + meta["attachment_metadata"] |> PhpSerializer.unserialize case deserialization_results do diff --git a/apps/content/lib/content/commands/update_menu.ex b/apps/content/lib/content/commands/update_menu.ex deleted file mode 100644 index 433a0006..00000000 --- a/apps/content/lib/content/commands/update_menu.ex +++ /dev/null @@ -1,178 +0,0 @@ -defmodule Content.UpdateMenu do - alias Content.{Menu, Post, Postmeta, Repo, TermRelationship} - alias Ecto.Multi - - import Ecto.Query - - def run(id, new_menu_params) do - current_posts = Menu.nav_menu_items_for_id(id) - post_ids_in_new_menu = recursive_post_ids(new_menu_params) - deleted_post_ids = - current_posts - |> Enum.reject(& &1.id in post_ids_in_new_menu) - |> Enum.map(& &1.id) - - Multi.new() - |> process_nodes(id, 0, new_menu_params |> add_order()) - |> Multi.delete_all(:stale_nodes, from(p in Post, where: p.id in ^deleted_post_ids)) - |> Repo.transaction() - end - - def add_order(tree) do - {_next_order, nodes} = add_order_starting_at(tree, 1) - - nodes - end - - def add_order_starting_at(tree, starting_at) do - tree - |> Enum.reduce({starting_at, []}, fn node, {order, new_nodes} -> - {next_order, new_child_nodes} = add_order_starting_at(node["children"], order + 1) - new_node = - node - |> Map.merge(%{ - "order" => order, - "children" => new_child_nodes, - }) - {next_order, new_nodes ++ [new_node]} - end) - end - - defp process_nodes(multi, menu_id, parent_id, nodes) do - nodes - |> Enum.reduce(multi, fn node, m -> - case node["post_id"] do - nil -> - create_node(m, menu_id, parent_id, node) - _id -> - update_node(m, menu_id, parent_id, node) - end - end) - end - - defp create_node(multi, menu_id, parent_id, node) do - post = - Post.changeset( - %Post{}, - %{ - post_author: 1, - post_title: node["title"], - post_status: "publish", - comment_status: "closed", - ping_status: "closed", - menu_order: node["order"], - post_type: "nav_menu_item", - comment_count: 0, - } - ) - - step_name = "#{parent_id}.create_node.#{node["order"]}" - - multi - |> Multi.insert(step_name, post) - |> Multi.run("#{step_name}.term_relationship", fn _repo, %{^step_name => post} -> - tr = - TermRelationship.changeset( - %TermRelationship{}, - %{ - object_id: post.id, - term_taxonomy_id: menu_id, - term_order: 0, - } - ) - - Repo.insert(tr) - end) - |> Multi.merge(fn %{^step_name => post} -> - Multi.new() - |> insert_metas(type_of_node(node), post, parent_id, node) - end) - |> Multi.merge(fn %{^step_name => post} -> - Multi.new() - |> process_nodes(menu_id, post.id, node["children"]) - end) - end - - defp insert_metas(multi, "post", post, parent_id, node) do - multi - |> update_meta(post.id, "_menu_item_type", "post_type") - |> update_meta(post.id, "_menu_item_object", "page") - |> update_meta(post.id, "_menu_item_object_id", node["target_id"]) - |> update_meta(post.id, "_menu_item_menu_item_parent", parent_id) - end - - defp insert_metas(multi, "category", post, parent_id, node) do - multi - |> update_meta(post.id, "_menu_item_type", "taxonomy") - |> update_meta(post.id, "_menu_item_object", "category") - |> update_meta(post.id, "_menu_item_object_id", node["target_id"]) - |> update_meta(post.id, "_menu_item_menu_item_parent", parent_id) - end - - defp insert_metas(multi, "link", post, parent_id, node) do - multi - |> update_meta(post.id, "_menu_item_type", "custom") - |> update_meta(post.id, "_menu_item_object", "custom") - |> update_meta(post.id, "_menu_item_object_id", post.id) - |> update_meta(post.id, "_menu_item_url", node["url"]) - |> update_meta(post.id, "_menu_item_menu_item_parent", parent_id) - end - - defp type_of_node(%{"url" => url}) when url != nil, do: "link" - defp type_of_node(%{"related_item" => %{"resource" => "posts"}}), do: "post" - defp type_of_node(%{"related_item" => %{"resource" => "category"}}), do: "category" - - defp update_node(multi, menu_id, parent_id, node) do - multi - |> update_meta(node["post_id"], "_menu_item_menu_item_parent", parent_id) - |> update_order(node["post_id"], node["order"]) - |> process_nodes(menu_id, node["post_id"], node["children"]) - end - - defp update_meta(multi, post_id, meta_key, new_value) when is_integer(new_value), - do: update_meta(multi, post_id, meta_key, new_value |> Integer.to_string()) - - defp update_meta(multi, post_id, meta_key, new_value) do - step_name = "#{post_id}.update_meta.#{meta_key}" - type = Postmeta.__schema__(:type, :meta_value) - cast_value = Ecto.Type.cast(type, new_value) - - Postmeta - |> where([pm], pm.meta_key == ^meta_key) - |> where([pm], pm.post_id == ^post_id) - |> Repo.one() - |> case do - nil -> - multi - |> Multi.insert( - step_name, - Postmeta.changeset( - %Postmeta{}, - %{ - post_id: post_id, - meta_key: meta_key, - meta_value: new_value - } - ) - ) - %{meta_value: ^cast_value} -> - # No change needed - multi - meta -> - multi - |> Multi.update(step_name, Postmeta.changeset(meta, %{meta_value: new_value})) - end - end - - defp update_order(multi, post_id, new_order) do - step_name = "#{post_id}.update_order" - - multi - |> Multi.update_all(step_name, from(p in Post, where: p.id == ^post_id), [set: [menu_order: new_order]]) - end - - defp recursive_post_ids(params) do - params - |> Enum.flat_map(& [&1["post_id"]|recursive_post_ids(&1["children"])]) - end -end diff --git a/apps/content/lib/content/comment.ex b/apps/content/lib/content/comment.ex index 3a49024c..a6f96a3f 100644 --- a/apps/content/lib/content/comment.ex +++ b/apps/content/lib/content/comment.ex @@ -6,49 +6,47 @@ defmodule Content.Comment do import Ecto.Changeset alias Content.{Post} - @primary_key {:comment_id, :id, autogenerate: true} - @derive {Phoenix.Param, key: :comment_id} - schema "wp_comments" do - belongs_to :post, Post, foreign_key: :comment_post_id, references: :id - field :comment_author, :string - field :comment_author_email, :string - field :comment_author_url, :string - field :comment_author_IP, :string - field :comment_date, :naive_datetime - field :comment_date_gmt, :naive_datetime - field :comment_content, :string - field :comment_karma, :integer - field :comment_approved, :string - field :comment_agent, :string - field :comment_type, :string - field :comment_parent, :integer, default: 0 + schema "comments" do + belongs_to :post, Post + field :author, :string + field :author_email, :string + field :author_url, :string + field :author_IP, :string + field :date, :naive_datetime + field :date_gmt, :naive_datetime + field :content, :string + field :karma, :integer + field :approved, :string + field :agent, :string + field :type, :string + field :parent, :integer, default: 0 field :user_id, :integer end def changeset(struct, params \\ %{}) do struct |> Map.merge(%{ - comment_date: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second), - comment_date_gmt: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second), - comment_approved: "1" + date: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second), + date_gmt: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second), + approved: "1" }) |> cast(params, [ - :comment_id, - :comment_post_id, - :comment_author, - :comment_author_email, - :comment_author_url, - :comment_author_IP, - :comment_date, - :comment_date_gmt, - :comment_content, - :comment_karma, - :comment_approved, - :comment_agent, - :comment_type, - :comment_parent, + :id, + :post_id, + :author, + :author_email, + :author_url, + :author_IP, + :date, + :date_gmt, + :content, + :karma, + :approved, + :agent, + :type, + :parent, :user_id ]) - |> validate_required([:comment_content]) + |> validate_required([:content]) end end diff --git a/apps/content/lib/content/comment_admin.ex b/apps/content/lib/content/comment_admin.ex new file mode 100644 index 00000000..9b9fc2cf --- /dev/null +++ b/apps/content/lib/content/comment_admin.ex @@ -0,0 +1,26 @@ +defmodule Content.CommentAdmin do + def index(_) do + [ + id: nil, + author: nil, + author_email: nil, + author_url: nil, + content: nil, + approved: nil, + date: nil, + date_gmt: nil, + ] + end + + def form_fields(_) do + [ + author: nil, + author_email: nil, + author_url: nil, + content: %{type: :textarea, rows: 8}, + date: nil, + date_gmt: nil, + approved: nil, + ] + end +end diff --git a/apps/content/lib/content/commentmeta.ex b/apps/content/lib/content/commentmeta.ex index cad8e1cc..f4b0c9bb 100644 --- a/apps/content/lib/content/commentmeta.ex +++ b/apps/content/lib/content/commentmeta.ex @@ -5,15 +5,14 @@ defmodule Content.Commentmeta do use Ecto.Schema import Ecto.Changeset - @primary_key {:meta_id, :id, autogenerate: true} - schema "wp_commentmeta" do + schema "commentmeta" do field :comment_id, :integer - field :meta_key, :string - field :meta_value, :string + field :key, :string + field :value, :string end def changeset(struct, params \\ %{}) do struct - |> cast(params, [:meta_id, :comment_id, :meta_key, :meta_value]) + |> cast(params, [:id, :comment_id, :key, :value]) end end diff --git a/apps/content/lib/content/comments.ex b/apps/content/lib/content/comments.ex index 76782f76..98f36129 100644 --- a/apps/content/lib/content/comments.ex +++ b/apps/content/lib/content/comments.ex @@ -6,9 +6,9 @@ defmodule Content.Comments do alias Content.Comment alias Content.Repo - def children(parent_comment_id, array_of_comments) do + def children(parent_id, array_of_comments) do array_of_comments - |> Enum.filter(&(&1.comment_parent == parent_comment_id)) + |> Enum.filter(&(&1.parent == parent_id)) end @doc """ diff --git a/apps/content/lib/content/link.ex b/apps/content/lib/content/link.ex index 4baa64bd..d9858b5f 100644 --- a/apps/content/lib/content/link.ex +++ b/apps/content/lib/content/link.ex @@ -5,38 +5,37 @@ defmodule Content.Link do use Ecto.Schema import Ecto.Changeset - @primary_key {:link_id, :id, autogenerate: true} - schema "wp_links" do - field :link_url, :string - field :link_name, :string - field :link_image, :string - field :link_target, :string - field :link_description, :string - field :link_visible, :string - field :link_owner, :integer - field :link_rating, :integer - field :link_updated, :naive_datetime - field :link_rel, :string - field :link_notes, :string - field :link_rss, :string + schema "links" do + field :url, :string + field :name, :string + field :image, :string + field :target, :string + field :description, :string + field :visible, :string + field :owner, :integer + field :rating, :integer + field :updated, :naive_datetime + field :rel, :string + field :notes, :string + field :rss, :string end def changeset(struct, params \\ %{}) do struct |> cast(params, [ - :link_id, - :link_url, - :link_name, - :link_image, - :link_target, - :link_description, - :link_visible, - :link_owner, - :link_rating, - :link_updated, - :link_rel, - :link_notes, - :link_rss + :id, + :url, + :name, + :image, + :target, + :description, + :visible, + :owner, + :rating, + :updated, + :rel, + :notes, + :rss ]) end end diff --git a/apps/content/lib/content/menu.ex b/apps/content/lib/content/menu.ex deleted file mode 100644 index b8b6f237..00000000 --- a/apps/content/lib/content/menu.ex +++ /dev/null @@ -1,167 +0,0 @@ -defmodule Content.Menu do - @moduledoc """ - Module for retrieving, manipulating, and processing navigation menus. - """ - alias Content.{Option, Post, Repo, Term, TermRelationship} - import Ecto.Query - - def put_menu_option(option_name, location_name, menu_id) do - option = - Option - |> where(option_name: ^option_name) - |> Repo.one() - |> Kernel.||(%Option{option_name: option_name, option_value: "a:0:{}"}) - - value = - option - |> Option.parse_option_value - - nav_menu_locations = - value - |> Enum.find(fn {key, _value} -> key == "nav_menu_locations" end) - |> Kernel.||({"nav_menu_locations", []}) - |> elem(1) - - new_nav_menu_locations = - nav_menu_locations - |> Enum.filter(fn {key, _value} -> key != location_name end) - |> Kernel.++([{location_name, menu_id}]) - - new_value = - value - |> Enum.filter(fn {key, _value} -> key != "nav_menu_locations" end) - |> Kernel.++([{"nav_menu_locations", new_nav_menu_locations}]) - - option - |> Option.put_new_value(new_value) - end - - def get_menu_from_option_and_location(option_name, location_name) do - option = - Option - |> where(option_name: ^option_name) - |> Repo.one() - |> Kernel.||(%Option{option_name: option_name, option_value: "a:0:{}"}) - - value = - option - |> Option.parse_option_value - - menu_pair = - value - |> Enum.find(fn {key, _value} -> key == "nav_menu_locations" end) - |> Kernel.||({"nav_menu_locations", []}) - |> elem(1) - |> Enum.find(fn {key, _value} -> key == location_name end) - - case menu_pair do - {^location_name, menu_id} -> - menu_id |> get_menu_from_id() - nil -> - nil - end - end - - def get_menu_from_id(menu_id) do - menu_id - |> nav_menu_items_for_id() - |> arrange_menu_item_posts() - end - - def nav_menu_items_for_id(menu_id) do - Post - |> join( - :inner, - [p], - tr in TermRelationship, - on: p.id == tr.object_id - ) - |> order_by(:menu_order) - |> preload(:metas) - |> where([p, tr], tr.term_taxonomy_id == ^menu_id) - |> Repo.all() - end - - defp arrange_menu_item_posts(nav_posts, parent_id \\ "0", nav_to_post_map \\ nil) do - nav_to_post_map = nav_to_post_map || make_nav_to_post_map(nav_posts) - - nav_posts - |> Enum.filter(fn post -> - meta_map = post |> Post.metas_map - meta_map["_menu_item_menu_item_parent"] == parent_id - end) - |> Enum.map(fn post -> - meta_map = post |> Post.metas_map - related_item = - if meta_map["_menu_item_object"] == "category" do - item = nav_to_post_map["category/#{meta_map["_menu_item_object_id"]}"] || %Term{} - - %{ - title: item.name, - slug: item.slug, - resource: "category", - } - else - item = nav_to_post_map["post/#{meta_map["_menu_item_object_id"]}"] || %Post{} - - %{ - title: item.post_title, - slug: item.post_name, - resource: "posts", - } - end - - %{ - post_id: post.id, - type: meta_map["_menu_item_object"], - target_id: meta_map["_menu_item_object_id"], - parent_id: meta_map["_menu_item_menu_item_parent"], - url: meta_map["_menu_item_url"], - related_item: related_item, - children: arrange_menu_item_posts(nav_posts, Integer.to_string(post.id), nav_to_post_map), - } - end) - end - - defp make_nav_to_post_map(nav_posts) do - nav_post_meta_map = nav_posts |> Post.metas_map() - - linked_post_ids = - nav_post_meta_map - |> Enum.filter(fn {_key, value} -> - value["_menu_item_object"] != "category" - end) - |> Enum.map(fn {_key, value} -> - value["_menu_item_object_id"] - end) - - nav_to_post_map = - Post - |> where([p], p.id in ^linked_post_ids) - |> Repo.all() - |> Enum.map(fn post -> - {"post/#{post.id}", post} - end) - |> Map.new - - linked_category_ids = - nav_post_meta_map - |> Enum.filter(fn {_key, value} -> - value["_menu_item_object"] == "category" - end) - |> Enum.map(fn {_key, value} -> - value["_menu_item_object_id"] - end) - - nav_to_category_map = - Term - |> where([t], t.term_id in ^linked_category_ids) - |> Repo.all() - |> Enum.map(fn category -> - {"category/#{category.term_id}", category} - end) - |> Map.new - - nav_to_post_map |> Map.merge(nav_to_category_map) - end -end diff --git a/apps/content/lib/content/option.ex b/apps/content/lib/content/option.ex index c9ee60bd..85aad75b 100644 --- a/apps/content/lib/content/option.ex +++ b/apps/content/lib/content/option.ex @@ -5,20 +5,19 @@ defmodule Content.Option do use Ecto.Schema import Ecto.Changeset - @primary_key {:option_id, :id, autogenerate: true} - schema "wp_options" do - field :option_name, :string + schema "options" do + field :name, :string field :autoload, :string - field :option_value, :string + field :value, :string end def changeset(struct, params \\ %{}) do struct - |> cast(params, [:option_id, :option_name, :option_value, :autoload]) + |> cast(params, [:id, :name, :value, :autoload]) end - def parse_option_value(struct) do - case PhpSerializer.unserialize(struct.option_value) do + def parse_value(struct) do + case PhpSerializer.unserialize(struct.value) do {:ok, values} -> values end @@ -26,6 +25,6 @@ defmodule Content.Option do def put_new_value(struct, value) do struct - |> change(%{option_value: PhpSerializer.serialize(value)}) + |> change(%{value: PhpSerializer.serialize(value)}) end end diff --git a/apps/content/lib/content/options.ex b/apps/content/lib/content/options.ex index fddafe8a..8da81095 100644 --- a/apps/content/lib/content/options.ex +++ b/apps/content/lib/content/options.ex @@ -8,13 +8,13 @@ defmodule Content.Options do def put(key, value) do %Option{} |> Option.changeset(%{ - option_name: key, - option_value: value, + name: key, + value: value, }) |> Repo.insert() end - def get(key), do: Option |> Repo.get_by(option_name: key) + def get(key), do: Option |> Repo.get_by(name: key) def get_value(key) do case get(key) do @@ -22,7 +22,7 @@ defmodule Content.Options do nil opt -> opt - |> (&(&1.option_value)).() + |> (&(&1.value)).() end end diff --git a/apps/content/lib/content/post.ex b/apps/content/lib/content/post.ex index c0c60234..025083a7 100644 --- a/apps/content/lib/content/post.ex +++ b/apps/content/lib/content/post.ex @@ -6,38 +6,37 @@ defmodule Content.Post do import Ecto.Changeset alias Content.Slugs - @primary_key {:id, :id, autogenerate: true} - @derive {Phoenix.Param, key: :post_name} - schema "wp_posts" do - field :post_date, :naive_datetime - field :post_date_gmt, :naive_datetime - field :post_content, :string, default: "" - field :post_title, :string - field :post_excerpt, :string - field :post_status, :string + @derive {Phoenix.Param, key: :name} + schema "posts" do + field :date, :naive_datetime + field :date_gmt, :naive_datetime + field :content, :string, default: "" + field :title, :string + field :excerpt, :string + field :status, :string field :comment_status, :string field :ping_status, :string - field :post_password, :string, default: "" - field :post_name, :string + field :password, :string, default: "" + field :name, :string field :to_ping, :string, default: "" field :pinged, :string, default: "" - field :post_modified, :naive_datetime - field :post_modified_gmt, :naive_datetime - field :post_content_filtered, :string, default: "" - field :post_parent, :integer + field :modified, :naive_datetime + field :modified_gmt, :naive_datetime + field :content_filtered, :string, default: "" + field :parent, :integer field :guid, :string field :menu_order, :integer - field :post_type, :string - field :post_mime_type, :string + field :type, :string + field :mime_type, :string field :comment_count, :integer field :sticky, :boolean, [virtual: true, default: false] - has_many :metas, Content.Postmeta, foreign_key: :post_id - has_many :comments, Content.Comment, foreign_key: :comment_post_id + has_many :metas, Content.Postmeta + has_many :comments, Content.Comment has_many :term_relationships, Content.TermRelationship, foreign_key: :object_id has_many :categories, through: [:term_relationships, :category, :term] has_many :tags, through: [:term_relationships, :tag, :term] - has_one :post_format, through: [:term_relationships, :post_format, :term] - belongs_to :author, Auth.User, foreign_key: :post_author + has_one :format, through: [:term_relationships, :format, :term] + belongs_to :author, Auth.User end def changeset(struct, params \\ %{}) do @@ -46,35 +45,35 @@ defmodule Content.Post do params, [ :id, - :post_author, - :post_date, - :post_date_gmt, - :post_content, - :post_title, - :post_excerpt, - :post_status, + :author_id, + :date, + :date_gmt, + :content, + :title, + :excerpt, + :status, :comment_status, :ping_status, - :post_password, - :post_name, + :password, + :name, :to_ping, :pinged, - :post_content_filtered, - :post_parent, + :content_filtered, + :parent, :menu_order, - :post_type, - :post_mime_type, + :type, + :mime_type, :comment_count, :sticky, ]) - |> put_default(:post_date, NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)) - |> put_default(:post_date_gmt, NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)) - |> put_change(:post_modified, NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)) - |> put_change(:post_modified_gmt, NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)) + |> put_default(:date, NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)) + |> put_default(:date_gmt, NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)) + |> put_change(:modified, NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)) + |> put_change(:modified_gmt, NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)) |> Slugs.ensure_post_has_slug() |> maybe_put_guid() - |> validate_required([:post_name, :post_status]) - |> validate_inclusion(:post_status, ["publish", "future", "draft", "pending", "private", "trash", "auto-draft", "inherit"]) + |> validate_required([:name, :status]) + |> validate_inclusion(:status, ["publish", "future", "draft", "pending", "private", "trash", "auto-draft", "inherit"]) end def put_default(changeset, key, value) do @@ -93,14 +92,14 @@ defmodule Content.Post do end def content_page(struct, page) do - (struct.post_content || "") + (struct.content || "") |> String.split("") |> Enum.at(page - 1) |> Kernel.||("") end def content_page_count(struct) do - (struct.post_content || "") + (struct.content || "") |> String.split("") |> Enum.count end @@ -118,13 +117,13 @@ defmodule Content.Post do end def metas_map(%Content.Post{} = struct) do struct.metas - |> Enum.map(&({&1.meta_key, &1.meta_value})) + |> Enum.map(&({&1.key, &1.value})) |> Map.new end def maybe_put_guid(changeset) do import Content.Router.Helpers, only: [posts_url: 3] - slug = changeset |> get_field(:post_name) + slug = changeset |> get_field(:name) case slug do nil -> changeset diff --git a/apps/content/lib/content/post_admin.ex b/apps/content/lib/content/post_admin.ex index ca94d9a3..77e04b74 100644 --- a/apps/content/lib/content/post_admin.ex +++ b/apps/content/lib/content/post_admin.ex @@ -20,12 +20,12 @@ defmodule Content.PostAdmin do def index(_) do [ id: nil, - post_type: nil, - post_name: nil, - post_title: nil, - post_status: nil, - post_date_gmt: nil, - post_modified_gmt: nil, + type: nil, + name: nil, + title: nil, + status: nil, + date_gmt: nil, + modified_gmt: nil, ] end @@ -43,17 +43,17 @@ defmodule Content.PostAdmin do end) [ - post_type: %{choices: [{"Blog Post", :post}, {"Page", :page}]}, - post_name: %{label: "Slug"}, - post_title: nil, - post_content: %{type: :textarea, rows: 32}, - post_status: %{choices: [{"Publish", :publish}, {"Draft", :draft}]}, - post_author: %{choices: authors}, - post_excerpt: %{type: :textarea, rows: 4}, + type: %{choices: [{"Blog Post", :post}, {"Page", :page}]}, + name: %{label: "Slug"}, + title: nil, + content: %{type: :textarea, rows: 32}, + status: %{choices: [{"Publish", :publish}, {"Draft", :draft}]}, + author_id: %{choices: authors}, + excerpt: %{type: :textarea, rows: 4}, sticky: nil, comment_status: %{choices: [{"open", :open}, {"closed", :closed}]}, ping_status: %{choices: [{"open", :open}, {"closed", :closed}]}, - post_password: nil, + password: nil, menu_order: nil, ] end diff --git a/apps/content/lib/content/postmeta.ex b/apps/content/lib/content/postmeta.ex index 1c1c9bda..51c8d889 100644 --- a/apps/content/lib/content/postmeta.ex +++ b/apps/content/lib/content/postmeta.ex @@ -5,15 +5,14 @@ defmodule Content.Postmeta do use Ecto.Schema import Ecto.Changeset - @primary_key {:meta_id, :id, autogenerate: true} - schema "wp_postmeta" do + schema "postmeta" do belongs_to :post, Content.Post - field :meta_key, :string - field :meta_value, :string + field :key, :string + field :value, :string end def changeset(struct, params \\ %{}) do struct - |> cast(params, [:meta_id, :post_id, :meta_key, :meta_value]) + |> cast(params, [:id, :post_id, :key, :value]) end end diff --git a/apps/content/lib/content/posts.ex b/apps/content/lib/content/posts.ex index 5b6d1ded..4acc1dd1 100644 --- a/apps/content/lib/content/posts.ex +++ b/apps/content/lib/content/posts.ex @@ -12,7 +12,7 @@ defmodule Content.Posts do alias Content.Post alias Ecto.Changeset - @preloads [:metas, :author, :categories, :tags, :comments, :post_format] + @preloads [:metas, :author, :categories, :tags, :comments, :format] @doc """ Returns the lisdpt of posts for admin interface. @@ -23,14 +23,14 @@ defmodule Content.Posts do [%Post{}, ...] """ - def list_admin_posts(page, post_type \\ "post") do - post_type = post_type || "post" + def list_admin_posts(page, type \\ "post") do + type = type || "post" Repo.all( from p in Post, - where: p.post_type == ^post_type, - where: p.post_status in ["publish", "future", "draft", "pending", "private", "inherit"], + where: p.type == ^type, + where: p.status in ["publish", "future", "draft", "pending", "private", "inherit"], preload: ^@preloads, - order_by: [desc: p.post_date], + order_by: [desc: p.date], limit: @page_size, offset: ^(@page_size * (String.to_integer(page) - 1)) ) @@ -59,12 +59,12 @@ defmodule Content.Posts do end def post_scope_for_params(params) do - post_type = params |> Map.get("post_type", "post") + type = params |> Map.get("type", "post") category = params |> Map.get("category") query = post_scope() - |> where([p], p.post_type == ^post_type) + |> where([p], p.type == ^type) if category do query |> join(:inner, [p], term in assoc(p, :categories), on: term.slug == ^category) @@ -75,15 +75,15 @@ defmodule Content.Posts do def post_scope do from p in Post, - where: p.post_status == "publish", + where: p.status == "publish", preload: ^@preloads, - order_by: [desc: p.post_date] + order_by: [desc: p.date] end def post_scope_with_drafts do from p in Post, preload: ^@preloads, - order_by: [desc: p.post_date] + order_by: [desc: p.date] end def sticky_posts_for_page(%{"page" => "1"} = params) do @@ -103,12 +103,12 @@ defmodule Content.Posts do def sticky_posts_for_page(_), do: [] defp sticky_ids do - case Repo.one(from opt in Option, where: opt.option_name == "sticky_posts") do + case Repo.one(from opt in Option, where: opt.name == "sticky_posts") do nil -> [] option -> option - |> Option.parse_option_value + |> Option.parse_value |> Enum.map(&(elem(&1, 1))) end end @@ -175,14 +175,14 @@ defmodule Content.Posts do case Integer.parse(id, 10) do :error -> - scope |> where([p], p.post_name == ^id) + scope |> where([p], p.name == ^id) {int_id, _} -> scope |> where([p], p.id == ^int_id) end end post_scope() - |> where([p], p.post_type != "nav_menu_item") + |> where([p], p.type != "nav_menu_item") |> id_filter.(slug) end @@ -205,16 +205,16 @@ defmodule Content.Posts do case Integer.parse(id, 10) do :error -> - scope |> where([p], p.post_name == ^id) + scope |> where([p], p.name == ^id) {int_id, ""} -> scope |> where([p], p.id == ^int_id) {_int_id, _} -> - scope |> where([p], p.post_name == ^id) + scope |> where([p], p.name == ^id) end end post_scope_with_drafts() - |> where([p], p.post_type != "nav_menu_item") + |> where([p], p.type != "nav_menu_item") |> id_filter.(slug) |> Repo.one!() end @@ -244,7 +244,7 @@ defmodule Content.Posts do %Post{} |> Repo.preload(@preloads) |> Post.changeset(attrs) - |> Changeset.put_change(:post_name, "preview") + |> Changeset.put_change(:name, "preview") |> Changeset.apply_changes() end diff --git a/apps/content/lib/content/slugs.ex b/apps/content/lib/content/slugs.ex index 92cffbee..01b8db93 100644 --- a/apps/content/lib/content/slugs.ex +++ b/apps/content/lib/content/slugs.ex @@ -7,14 +7,14 @@ defmodule Content.Slugs do def ensure_post_has_slug(changeset) do cond do - !is_nil(changeset |> get_field(:post_name)) -> + !is_nil(changeset |> get_field(:name)) -> changeset - is_nil(changeset |> get_field(:post_title)) -> + is_nil(changeset |> get_field(:title)) -> changeset |> put_change( - :post_name, + :name, changeset - |> get_field(:post_date) + |> get_field(:date) |> Kernel.||(NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)) |> Timex.format!("%F", :strftime) |> Slugger.slugify_downcase() @@ -23,16 +23,16 @@ defmodule Content.Slugs do true -> changeset |> put_change( - :post_name, + :name, changeset - |> get_field(:post_title) + |> get_field(:title) |> Slugger.slugify_downcase() |> unique_slug(changeset |> get_field(:id)) ) end end - defp unique_slug(proposed_slug, post_id, postfix_number \\ 0) do + defp unique_slug(proposed_slug, id, postfix_number \\ 0) do proposed_slug_with_postfix = if postfix_number == 0 do proposed_slug @@ -44,8 +44,8 @@ defmodule Content.Slugs do Repo.aggregate( ( Post - |> where([post], post.post_name == ^proposed_slug_with_postfix) - |> post_id_match(post_id) + |> where([post], post.name == ^proposed_slug_with_postfix) + |> post_id_match(id) ), :count, :id @@ -54,7 +54,7 @@ defmodule Content.Slugs do if competition_count == 0 do proposed_slug_with_postfix else - unique_slug(proposed_slug, post_id, postfix_number + 1) + unique_slug(proposed_slug, id, postfix_number + 1) end end diff --git a/apps/content/lib/content/term.ex b/apps/content/lib/content/term.ex index 44a2221f..ae6abde4 100644 --- a/apps/content/lib/content/term.ex +++ b/apps/content/lib/content/term.ex @@ -5,8 +5,7 @@ defmodule Content.Term do use Ecto.Schema import Ecto.Changeset - @primary_key {:term_id, :id, autogenerate: true} - schema "wp_terms" do + schema "terms" do field :name, :string field :slug, :string field :term_group, :integer @@ -14,6 +13,6 @@ defmodule Content.Term do def changeset(struct, params \\ %{}) do struct - |> cast(params, [:term_id, :name, :slug, :term_group]) + |> cast(params, [:id, :name, :slug, :term_group]) end end diff --git a/apps/content/lib/content/term_relationship.ex b/apps/content/lib/content/term_relationship.ex index d55b8b18..2381d40b 100644 --- a/apps/content/lib/content/term_relationship.ex +++ b/apps/content/lib/content/term_relationship.ex @@ -8,30 +8,26 @@ defmodule Content.TermRelationship do @primary_key {:object_id, :integer, []} @primary_key {:term_taxonomy_id, :integer, []} - schema "wp_term_relationships" do + schema "term_relationships" do field :term_order, :integer belongs_to :post, Post, foreign_key: :object_id, references: :id belongs_to :term_taxonomy, Content.TermTaxonomy, foreign_key: :term_taxonomy_id, - references: :term_taxonomy_id, define_field: false belongs_to :category, Content.TermTaxonomy, foreign_key: :term_taxonomy_id, - references: :term_taxonomy_id, define_field: false, where: [taxonomy: "category"] belongs_to :tag, Content.TermTaxonomy, foreign_key: :term_taxonomy_id, - references: :term_taxonomy_id, define_field: false, where: [taxonomy: "post_tag"] - belongs_to :post_format, + belongs_to :format, Content.TermTaxonomy, foreign_key: :term_taxonomy_id, - references: :term_taxonomy_id, define_field: false, where: [taxonomy: "post_format"] end diff --git a/apps/content/lib/content/term_taxonomy.ex b/apps/content/lib/content/term_taxonomy.ex index cf642ff8..8d7d3965 100644 --- a/apps/content/lib/content/term_taxonomy.ex +++ b/apps/content/lib/content/term_taxonomy.ex @@ -5,17 +5,16 @@ defmodule Content.TermTaxonomy do use Ecto.Schema import Ecto.Changeset - @primary_key {:term_taxonomy_id, :id, autogenerate: true} - schema "wp_term_taxonomy" do + schema "term_taxonomy" do field :taxonomy, :string field :description, :string field :parent, :integer field :count, :integer - belongs_to :term, Content.Term, foreign_key: :term_id, references: :term_id + belongs_to :term, Content.Term end def changeset(struct, params \\ %{}) do struct - |> cast(params, [:term_taxonomy_id, :term_id, :taxonomy, :description, :parent, :count]) + |> cast(params, [:id, :term_id, :taxonomy, :description, :parent, :count]) end end diff --git a/apps/content/lib/content/termmeta.ex b/apps/content/lib/content/termmeta.ex index 1d50e3dd..5c2a57a7 100644 --- a/apps/content/lib/content/termmeta.ex +++ b/apps/content/lib/content/termmeta.ex @@ -5,15 +5,14 @@ defmodule Content.Termmeta do use Ecto.Schema import Ecto.Changeset - @primary_key {:meta_id, :id, autogenerate: true} - schema "wp_termmeta" do + schema "termmeta" do field :term_id, :integer - field :meta_key, :string - field :meta_value, :string + field :key, :string + field :value, :string end def changeset(struct, params \\ %{}) do struct - |> cast(params, [:meta_id, :term_id, :meta_key, :meta_value]) + |> cast(params, [:id, :term_id, :key, :value]) end end diff --git a/apps/content/lib/content/terms.ex b/apps/content/lib/content/terms.ex index b53f0bfd..fbbed673 100644 --- a/apps/content/lib/content/terms.ex +++ b/apps/content/lib/content/terms.ex @@ -9,7 +9,7 @@ defmodule Content.Terms do def categories do from t in Content.Term, join: tt in Content.TermTaxonomy, - on: t.term_id == tt.term_id, + on: t.id == tt.term_id, where: tt.taxonomy == "category" end end diff --git a/apps/content/lib/content_web/controllers/comment_controller.ex b/apps/content/lib/content_web/controllers/comment_controller.ex index ed5b52da..703a47af 100644 --- a/apps/content/lib/content_web/controllers/comment_controller.ex +++ b/apps/content/lib/content_web/controllers/comment_controller.ex @@ -15,7 +15,7 @@ defmodule Content.CommentController do {:ok, comment} -> post = Post - |> where([p], p.id == ^comment.comment_post_id) + |> where([p], p.id == ^comment.post_id) |> Repo.one() conn @@ -24,7 +24,7 @@ defmodule Content.CommentController do {:error, _} -> post = Post - |> where([p], p.id == ^comment_params["comment_post_id"]) + |> where([p], p.id == ^comment_params["post_id"]) |> Repo.one() conn @@ -39,7 +39,7 @@ defmodule Content.CommentController do {:ok, comment} -> post = Post - |> where([p], p.id == ^comment.comment_post_id) + |> where([p], p.id == ^comment.post_id) |> Repo.one() conn @@ -48,7 +48,7 @@ defmodule Content.CommentController do {:error, _} -> post = Post - |> where([p], p.id == ^comment_params["comment_post_id"]) + |> where([p], p.id == ^comment_params["post_id"]) |> Repo.one() conn @@ -61,7 +61,7 @@ defmodule Content.CommentController do {:ok, comment} = Comments.delete_comment(comment) post = Post - |> where([p], p.id == ^comment.comment_post_id) + |> where([p], p.id == ^comment.post_id) |> Repo.one() conn diff --git a/apps/content/lib/content_web/controllers/menus_controller.ex b/apps/content/lib/content_web/controllers/menus_controller.ex deleted file mode 100644 index 9cfdf2ab..00000000 --- a/apps/content/lib/content_web/controllers/menus_controller.ex +++ /dev/null @@ -1,39 +0,0 @@ -defmodule Content.MenusController do - use Content, :controller - - alias Content.Repo - - def edit(conn, %{"id" => id}) do - menu = id |> Content.Menu.get_menu_from_id() - posts = - Content.Posts.post_scope - |> Repo.all() - |> Enum.map(fn post -> - post |> Map.take([:id, :post_title, :post_name]) - end) - categories = - Content.Terms.categories - |> Repo.all() - |> Enum.map(fn cat -> - cat |> Map.take([:name, :slug, :term_group, :term_id]) - end) - - conn - |> render( - "edit.html", - [ - id: id, - menu: menu, - posts: posts, - categories: categories, - ] - ) - end - - def update(conn, %{"id" => id, "menu" => menu}) do - Content.UpdateMenu.run(id, menu |> Phoenix.json_library().decode!()) - - conn - |> redirect(to: Routes.menus_path(conn, :edit, id)) - end -end diff --git a/apps/content/lib/content_web/controllers/posts_controller.ex b/apps/content/lib/content_web/controllers/posts_controller.ex index b90da69d..a9ac60d3 100644 --- a/apps/content/lib/content_web/controllers/posts_controller.ex +++ b/apps/content/lib/content_web/controllers/posts_controller.ex @@ -1,8 +1,7 @@ defmodule Content.PostsController do use Content, :controller - alias Auth.User - alias Content.{Options, Post, Posts, Repo} + alias Content.{Options, Posts} plug :put_layout, false when action in [:preview] @@ -44,34 +43,6 @@ defmodule Content.PostsController do show(conn, %{"id" => page_id}) end - def new(conn, params) do - changeset = Posts.change_posts(%Post{}) - render( - conn, - "new.html", - changeset: changeset, - post_type: params["post_type"] || "post", - author_options: User |> Repo.all() - ) - end - - def create(conn, %{"post" => post_params}) do - case Posts.create_posts(post_params) do - {:ok, post} -> - conn - |> put_flash(:info, "Posts created successfully.") - |> redirect(to: Routes.posts_path(conn, :show, post)) - {:error, %Ecto.Changeset{} = changeset} -> - render( - conn, - "new.html", - changeset: changeset, - post_type: post_params["post_type"] || "post", - author_options: User |> Repo.all() - ) - end - end - def preview(conn, %{"post" => post_params}) do post = Posts.preview_post(post_params) @@ -84,8 +55,6 @@ defmodule Content.PostsController do end def show(conn, %{"id" => id, "page" => page_string}) do - {page_id_for_posts, _} = Options.get_value_as_int("page_for_posts") - post = Posts.get_post(id) if is_nil(post) do @@ -126,57 +95,15 @@ defmodule Content.PostsController do page = String.to_integer(page_string) thumbs = [post] |> Posts.thumbs_for_posts() - case post.post_type do + case post.type do "attachment" -> - {:ok, decoded} = post.post_content |> Base.decode64 + {:ok, decoded} = post.content |> Base.decode64 conn - |> put_resp_content_type(post.post_mime_type, "binary") + |> put_resp_content_type(post.mime_type, "binary") |> send_resp(conn.status || 200, decoded) _ -> render(conn, template, post: post, page: page, thumbs: thumbs) end end - - def edit(conn, %{"id" => id}) do - posts = Posts.get_post_with_drafts!(id) - changeset = Posts.change_posts(posts) - render( - conn, - "edit.html", - posts: posts, - changeset: changeset, - post_type: posts.post_type || "post", - author_options: User |> Repo.all() - ) - end - - def update(conn, %{"id" => id, "post" => posts_params}) do - posts = Posts.get_post_with_drafts!(id) - - case Posts.update_posts(posts, posts_params) do - {:ok, posts} -> - conn - |> put_flash(:info, "Posts updated successfully.") - |> redirect(to: Routes.posts_path(conn, :edit, posts)) - {:error, %Ecto.Changeset{} = changeset} -> - render( - conn, - "edit.html", - posts: posts, - changeset: changeset, - post_type: posts.post_type || "post", - author_options: User |> Repo.all() - ) - end - end - - def delete(conn, %{"id" => id}) do - posts = Posts.get_post_with_drafts!(id) - {:ok, _posts} = Posts.delete_posts(posts) - - conn - |> put_flash(:info, "Posts deleted successfully.") - |> redirect(to: Routes.admin_posts_path(conn, :index)) - end end diff --git a/apps/content/lib/content_web/controllers/sitemap_controller.ex b/apps/content/lib/content_web/controllers/sitemap_controller.ex index 7f8ad57c..fb21b20e 100644 --- a/apps/content/lib/content_web/controllers/sitemap_controller.ex +++ b/apps/content/lib/content_web/controllers/sitemap_controller.ex @@ -8,7 +8,7 @@ defmodule Content.SitemapController do def index(conn, _params) do posts = Posts.post_scope - |> where([p], p.post_type not in ["nav_menu_item", "attachment"]) + |> where([p], p.type not in ["nav_menu_item", "attachment"]) |> Repo.all() categories = diff --git a/apps/content/lib/content_web/templates/admin_home/index.html.eex b/apps/content/lib/content_web/templates/admin_home/index.html.eex deleted file mode 100644 index 464f78bb..00000000 --- a/apps/content/lib/content_web/templates/admin_home/index.html.eex +++ /dev/null @@ -1 +0,0 @@ -
Title | -Author | -Categories | -Tags | -Comments | -Date | -
---|---|---|---|---|---|
- <%= link post.post_title, to: Routes.posts_path(@conn, :edit, post) %> - | -- <%= if !is_nil(post.author) do %> - <%= post.author.display_name %> - <% end %> - | -- <%= post.categories |> Enum.map(&(&1.name)) |> Enum.join(", ") %> - | -- <%= post.tags |> Enum.map(&(&1.name)) |> Enum.join(", ") %> - | -- <%= post.comments |> Enum.count() %> - | -
- <%= case post.post_status do %>
- <% "publish" -> %>
- <%= "Published" %>
- <% "future" -> %>
- <%= "Scheduled" %>
- <% "draft" -> %>
- <%= "Last Modified" %>
- <% "pending" -> %>
- <%= "Scheduled" %>
- <% "private" -> %>
- <%= "Published Privately" %>
- <% "inherit" -> %>
- <%= "Inherit" %>
- <% end %>
-
- <%= post.post_date |> Timex.format!("%F", :strftime) %>
-
- |
-
<%= get_flash(@conn, level) %>
- <% end %> - <% end %> - <%= render "_side_menu.html", assigns %> @@ -25,6 +19,8 @@ + <%= flash_block(@conn) %> + <%= @inner_content %>Oops, something went wrong! Please check the errors below.
-<%= link "Keep Reading", to: Routes.posts_path(@conn, :show, post) %>
diff --git a/apps/content/lib/content_web/templates/posts/new.html.eex b/apps/content/lib/content_web/templates/posts/new.html.eex deleted file mode 100644 index f7ce9a9b..00000000 --- a/apps/content/lib/content_web/templates/posts/new.html.eex +++ /dev/null @@ -1 +0,0 @@ -<%= render "form.html", Map.put(assigns, :action, Routes.posts_path(@conn, :create)) %> diff --git a/apps/content/lib/content_web/templates/posts/reply_form.html.eex b/apps/content/lib/content_web/templates/posts/reply_form.html.eex index 0617d623..0383676f 100644 --- a/apps/content/lib/content_web/templates/posts/reply_form.html.eex +++ b/apps/content/lib/content_web/templates/posts/reply_form.html.eex @@ -8,23 +8,23 @@ <%= changeset_error_block(@comment_changeset) %>